Not every device behaves the same way. A keyboard delivers input as it arrives, while a disk lets you jump to any position. Linux marks this difference on each node in /dev.
Read the first character
ls -l /dev/null /dev/sda /dev/ttycrw-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
crw-rw-rw- 1 root tty 5, 0 Nov 29 09:00 /dev/ttyThe first character is the file type:
cis a character device. Data is handled as a stream of bytes, as with terminals and serial ports.bis a block device. Data is handled in fixed size blocks that can be read in any order, as with disks. The kernel normally caches this data.
You will also meet two special file types that are not hardware devices. p is a named pipe (FIFO), and s is a socket. Both are used by processes to pass data to each other.
mkfifo /tmp/mypipe
ls -l /tmp/mypipeprw-r--r-- 1 sam sam 0 Nov 29 09:05 /tmp/mypipeMajor and minor numbers
Where a normal file shows its size, a device node shows two numbers, such as 8, 0. The first is the major number and the second is the minor number.
The major number tells the kernel which driver handles the device. The minor number is passed to that driver so it can tell its devices apart.
ls -l /dev/sda /dev/sda1 /dev/sda2brw-rw---- 1 root disk 8, 0 Nov 29 09:00 /dev/sda
brw-rw---- 1 root disk 8, 1 Nov 29 09:00 /dev/sda1
brw-rw---- 1 root disk 8, 2 Nov 29 09:00 /dev/sda2The disk and its partitions share major 8 and have different minors.
The kernel lists the major numbers currently registered:
cat /proc/devicesCharacter devices:
1 mem
4 tty
5 /dev/tty
Block devices:
8 sdThis output is shortened.
Not everything is in /dev
Network interfaces such as eth0 have no device node. You manage them with tools such as ip, which the networking basics guide puts in context.
Try it
- Run
ls -l /dev/null /dev/zero /dev/urandomand compare their major and minor numbers. - Run
ls -l /dev | grep '^b'to list block devices. A container may show none. - Run
lsblkto see block devices as a tree. - Create a named pipe with
mkfifo /tmp/demo, check it withls -l, then remove it withrm /tmp/demo.
Check your understanding
Device types and numbers
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.