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.
ls /devconsole disk full null pts random sda sda1 shm tty urandom zeroYour 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.
findmnt /devTARGET SOURCE FSTYPE OPTIONS
/dev udev devtmpfs rw,nosuid,relatimeThe source name and options vary between systems. Containers usually get a tmpfs instead.
Useful pseudo devices
Some nodes have no hardware behind them.
/dev/nulldiscards everything written to it. Reading it returns end of file at once./dev/zeroreturns an endless stream of zero bytes./dev/urandomreturns random bytes./dev/fullfails every write with "No space left on device", which is handy for testing error handling.
ls /nope 2>/dev/null
head -c 4 /dev/zero | od -An -tx1 00 00 00 00The 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.
ls -l /dev/null /dev/sdacrw-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/sdaAnyone 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
- Run
ls -l /dev | head -n 20and notice the first character of each line. - Run
echo hello > /dev/nulland confirm nothing is printed. - Run
echo hello > /dev/fulland read the error. - Run
head -c 8 /dev/urandom | od -An -tx1twice 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.