Skip to content

Linux Foundations / Understand the system underneath ยท Step 13 of 38

Lesson: The /dev directorySwitch
โ† Linux Foundations

System administration

The /dev directory

Learn what lives in /dev and how Linux lets programs use hardware through files.

Linux lets programs talk to many devices the same way they talk to files: open, read, write, close. The special files that make this possible live in /dev.

What is in /dev

Each entry is a device node. It stores no data of its own. It is a doorway to a driver in the kernel.

bash
ls /dev
console  disk  full  null  pts  random  sda  sda1  shm  tty  urandom  zero

Your list will differ, because it depends on your hardware. A virtual machine or container often shows a much shorter or differently named set. Some entries are directories, such as /dev/disk, and some are symbolic links, such as /dev/stdin.

Who creates the entries

On most current distributions /dev is a devtmpfs filesystem held in memory. The kernel creates a node when a driver registers a device, and a service called udev then sets permissions and adds helpful links. Because it lives in memory, it is rebuilt at every boot.

bash
findmnt /dev
TARGET SOURCE FSTYPE   OPTIONS
/dev   udev   devtmpfs rw,nosuid,relatime

The source name and options vary between systems. Containers usually get a tmpfs instead.

Useful pseudo devices

Some nodes have no hardware behind them.

  • /dev/null discards everything written to it. Reading it returns end of file at once.
  • /dev/zero returns an endless stream of zero bytes.
  • /dev/urandom returns random bytes.
  • /dev/full fails every write with "No space left on device", which is handy for testing error handling.
bash
ls /nope 2>/dev/null
head -c 4 /dev/zero | od -An -tx1
 00 00 00 00

The first command prints nothing because its error message went to /dev/null. For more on redirection, try the streams and text lab.

Permissions matter

Device nodes have owners and permissions like other files.

bash
ls -l /dev/null /dev/sda
crw-rw-rw- 1 root root 1, 3 Nov 29 09:00 /dev/null
brw-rw---- 1 root disk 8, 0 Nov 29 09:00 /dev/sda

Anyone may use /dev/null. Only root and members of the disk group may open the raw disk here, because writing to it directly can destroy every filesystem on it. Group names can differ by distribution.

Try it

  1. Run ls -l /dev | head -n 20 and notice the first character of each line.
  2. Run echo hello > /dev/null and confirm nothing is printed.
  3. Run echo hello > /dev/full and read the error.
  4. Run head -c 8 /dev/urandom | od -An -tx1 twice and compare the output.

Check your understanding

The /dev directory

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.