Linux › Section C · Devices › Topic C.1

Device files

Linux lets programs reach most hardware through ordinary-looking files in /dev, so the same tools that read and write text files can talk to disks, terminals and special kernel devices. Reading a /dev listing tells you what kind of device each file is and which driver serves it.

Devices that look like files

The kernel presents many devices to programs as files, called device files or device nodes, kept in the /dev directory. A program opens, reads and writes them with the same system calls it uses for regular files, and the kernel passes each request to the right driver instead of to a filesystem. Try echo hello > /dev/null: the shell redirects output as usual, but /dev/null belongs to a kernel driver that simply accepts the data and throws it away. Because of this design, ordinary commands such as cat, head and dd work on devices. A file interface cannot express everything a device can do, so programs also use special control calls for some features.

Reading a /dev listing

ls -l on device files looks slightly different. An illustrative listing: brw-rw---- 1 root disk 8, 0 /dev/sda and crw-rw-rw- 1 root root 1, 5 /dev/zero. The first character of the mode gives the type: b for a block device and c for a character device. Where a regular file shows its size, a device file shows two numbers separated by a comma, the major and minor device numbers. The owner, group and permission bits work exactly as for any file, so they decide who may use the device. Here only root and members of the disk group can read the whole disk, while anyone may read or write /dev/zero.

Block and character devices

A block device holds a fixed amount of data organised in blocks, and a program can jump to any position and read or write there; the kernel buffers and caches the transfers. Disks and their partitions, such as /dev/sda and /dev/sda1, are block devices, as are loop devices, which make an ordinary file act like a disk. A character device handles a stream of bytes with no fixed size: data is passed straight to or from the driver, and the kernel cannot go back over a stream once it has passed. Terminals, serial ports, keyboards and mice, sound devices and pseudo-devices such as /dev/null are character devices.

Pipes and sockets

Two more file types also appear in long listings, though rarely in /dev. A named pipe, type p, works like the shell's | but has a name in the filesystem, so unrelated processes can use it: one process writes into it while another reads from the other end, and nothing is stored on disk. mkfifo name creates one. A socket, type s, is an endpoint for communication between processes; system services often create socket files under /run, such as the journal's. These have no kernel driver behind them: the other end is another process. Network sockets and Unix domain sockets covers sockets in depth.

Major and minor numbers

The kernel identifies a device by its type and two numbers, not by its name. The major number usually selects the driver: 8 is the SCSI disk driver, which also serves SATA and USB disks, and 1 is the driver for memory devices such as /dev/null. The minor number picks one device the driver manages: /dev/sda is 8,0, its first partition /dev/sda1 is 8,1, and /dev/sdb is 8,16. cat /proc/devices lists the major numbers of drivers currently loaded. Some devices, notably network interfaces such as eth0, have no device file at all, because a stream of bytes does not suit them; programs reach them through the network interfaces instead.

Creating device files

On a current system you do not create device files yourself. The kernel creates nodes for devices it detects in /dev, which is an in-memory filesystem called devtmpfs, and the udev service then sets their permissions and adds friendlier names, as the topic udev explains. The mknod command can still make one by hand, given the name, the type and both numbers: sudo mknod /dev/mydisk b 8 0 would create a second name for the first SCSI disk. Doing this is rarely useful and can expose hardware with the wrong permissions. Older systems kept a static /dev full of pre-made nodes, created by a script called MAKEDEV.

Pseudo-devices

Several character devices exist without any hardware. /dev/null discards everything written to it and returns end of file immediately when read. /dev/zero supplies an endless stream of zero bytes. /dev/full behaves like a full disk: every write fails with No space left on device, which is useful for testing how programs handle that error. /dev/random and /dev/urandom supply random bytes from the kernel's cryptographically secure generator; on current kernels both give output of the same quality once the generator has been initialised at boot. /dev/tty always means the controlling terminal of the process that opens it.

Worked example: trying pseudo-devices safely

These commands were run in GNU Bash 5.2 and change nothing on any disk. 1. echo hello > /dev/null prints nothing, and the exit status is 0: the write succeeded and the data vanished. 2. head -c 8 /dev/zero | od -An -tx1 prints eight bytes of 00; od shows bytes in hexadecimal, and head stops the endless stream. 3. head -c 4 /dev/urandom | od -An -tx1 prints four random bytes, different every time. 4. echo test > /dev/full fails with write error: No space left on device. 5. Notice that steps 2 and 3 needed head: reading /dev/zero or /dev/urandom without a limit would run forever.

Worked example: reading device entries

Take this illustrative output of ls -l /dev/sda /dev/sda2 /dev/ttyS0: brw-rw---- 1 root disk 8, 0 /dev/sda; brw-rw---- 1 root disk 8, 2 /dev/sda2; crw-rw---- 1 root dialout 4, 64 /dev/ttyS0. 1. The b marks the first two as block devices, and the c marks /dev/ttyS0, a serial port, as a character device. 2. Both disk entries share major number 8, so the same SCSI disk driver serves them. 3. Minor 0 is the whole disk and minor 2 its second partition. 4. The modes give read and write access to root and to the disk and dialout groups only. 5. So a user who needs the serial port is added to dialout, not given root.

Common mistakes to avoid

First, assuming the number shown in ls -l for a device is a size: it is the major and minor pair. Second, reading /dev/zero or /dev/urandom without a limit such as head -c or a dd count. Third, using chmod 666 on a disk device to let a program read it, which gives every user access to every file on that disk; add the user to the right group instead. Fourth, looking in /dev for network interfaces, which have no device files. Fifth, creating device nodes by hand when udev manages them.

Key takeaways

Devices appear as files in /dev, handled by kernel drivers rather than a filesystem. ls -l shows b for block devices, such as disks and partitions, and c for character devices, such as terminals and serial ports; p and s mark named pipes and sockets. The major number selects the driver and the minor number the device: /dev/sda is 8,0 and /dev/null 1,3. Permissions and groups such as disk and dialout control access. devtmpfs and udev create nodes automatically. /dev/null, /dev/zero, /dev/full, /dev/urandom and /dev/tty are pseudo-devices with no hardware.

Flashcards

Flashcard

Device file

A file in /dev through which programs reach a device

Also called a device node.

Test your understanding

Question 1 of 5

In ls -l /dev, a line reads brw-rw---- 1 root disk 8, 17 /dev/sdb1. What do 8 and 17 mean?

Question 2 of 5

Which of these is a character device?

Question 3 of 5

A tester wants a program's writes to fail as if the disk were full. Which file should the output go to?

Question 4 of 5

A user needs to use /dev/ttyUSB0, which is crw-rw---- root dialout. What is the right fix?

Question 5 of 5

Why does running cat /dev/zero > zeros.bin need care?

← Previous topicNext topic →