Skip to content

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

Lesson: Device types and numbersSwitch
โ† Linux Foundations

System administration

Device types and numbers

Tell character and block devices apart and read major and minor numbers.

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

bash
ls -l /dev/null /dev/sda /dev/tty
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
crw-rw-rw- 1 root tty  5, 0 Nov 29 09:00 /dev/tty

The first character is the file type:

  • c is a character device. Data is handled as a stream of bytes, as with terminals and serial ports.
  • b is 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.

bash
mkfifo /tmp/mypipe
ls -l /tmp/mypipe
prw-r--r-- 1 sam sam 0 Nov 29 09:05 /tmp/mypipe

Major 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.

bash
ls -l /dev/sda /dev/sda1 /dev/sda2
brw-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/sda2

The disk and its partitions share major 8 and have different minors.

The kernel lists the major numbers currently registered:

bash
cat /proc/devices
Character devices:
  1 mem
  4 tty
  5 /dev/tty

Block devices:
  8 sd

This 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

  1. Run ls -l /dev/null /dev/zero /dev/urandom and compare their major and minor numbers.
  2. Run ls -l /dev | grep '^b' to list block devices. A container may show none.
  3. Run lsblk to see block devices as a tree.
  4. Create a named pipe with mkfifo /tmp/demo, check it with ls -l, then remove it with rm /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.