7.1 About Device Files

The /dev directory contains device files (also sometimes known as device special files and device nodes) that provide access to peripheral devices such as hard disks, to resources on peripheral devices such as disk partitions, and pseudo devices such as a random number generator.

The /dev directory has several subdirectory hierarchies, each of which holds device files that relate to a certain type of device. For example, the /dev/disk/id-by-uuid directory contains device files for hard disks named according to the universally unique identifier (UUID) for the disk. The device files in subdirectories such as these are actually implemented as symbolic links to device files in /dev. You can access the same device using the file in /dev or the corresponding link to the file listed in /dev/disk/id-by-uuid.

If you use the ls -l command to list the files under /dev, you see that some device files are shown as being either type b for block or type c for character. These devices have a pair of numbers associated with them instead of a file size. These major and minor numbers identify the device to the system.

# ls -l /dev
total 0
crw-rw----. 1 root    root     10,  56 Mar 17 08:17 autofs
drwxr-xr-x. 2 root    root         640 Mar 17 08:17 block
drwxr-xr-x. 2 root    root          80 Mar 17 08:16 bsg
drwxr-xr-x. 3 root    root          60 Mar 17 08:16 bus
lrwxrwxrwx. 1 root    root           3 Mar 17 08:17 cdrom -> sr0
drwxr-xr-x. 2 root    root        2880 Mar 17 08:17 char
crw-------. 1 root    root      5,   1 Mar 17 08:17 console
lrwxrwxrwx. 1 root    root          11 Mar 17 08:17 core -> /proc/kcore
drwxr-xr-x. 4 root    root         100 Mar 17 08:17 cpu
crw-rw----. 1 root    root     10,  61 Mar 17 08:17 cpu_dma_latency
drwxr-xr-x. 6 root    root         120 Mar 17 08:16 disk
brw-rw----. 1 root    disk    253,   0 Mar 17 08:17 dm-0
brw-rw----. 1 root    disk    253,   1 Mar 17 08:17 dm-1
...
crw-rw-rw-. 1 root    root      1,   3 Mar 17 08:17 /dev/null
...
drwxr-xr-x. 2 root     root          0 Mar 17 08:16 pts
...
crw-rw-rw-. 1 root     root     1,   8 Mar 17 08:17 random
...
brw-rw----. 1 root     disk     8,   0 Mar 17 08:17 sda
brw-rw----. 1 root     disk     8,   1 Mar 17 08:17 sda1
brw-rw----. 1 root     disk     8,   2 Mar 17 08:17 sda2
...
lrwxrwxrwx. 1 root     root         15 Mar 17 08:17 stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root     root         15 Mar 17 08:17 stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root     root         15 Mar 17 08:17 stdout -> /proc/self/fd/1
...
crw--w----. 1 root     tty      4,   0 Mar 17 08:17 tty0
crw--w----. 1 root     tty      4,   1 Mar 17 08:17 tty1
...
crw-rw-rw-. 1 root     root     1,   9 Mar 17 08:17 urandom
...
crw-rw-rw-. 1 root     root     1,   5 Mar 17 08:17 zero

Block devices support random access to data, seeking media for data, and usually allow data to be buffered while it is being written or read. Examples of block devices include hard disks, CD-ROM drives, flash memory, and other addressable memory devices. The kernel writes data to or reads data from a block device in blocks of a certain number of bytes. In the sample output, sda is the block device file that corresponds to the hard disk, and it has a major number of 8 and a minor number of 0. sda1 and sda2 are partitions of this disk, and they have the same major number as sda (8), but their minor numbers are 1 and 2.

Character devices support streaming of data to or from a device, and data is not usually buffered nor is random access permitted to data on a device. The kernel writes data to or reads data from a character device one byte at a time. Examples of character devices include keyboards, mice, terminals, pseudo-terminals, and tape drives. tty0 and tty1 are character device files that correspond to terminal devices that allow users to log in from serial terminals or terminal emulators. These files have major number 4 and minor numbers 0 and 1.

Pseudo-terminals slave devices emulate real terminal devices to interact with software. For example, a user might log in on a terminal device such as /dev/tty1, which then uses the pseudo-terminal master device /dev/pts/ptmx to interact with an underlying pseudo-terminal device. The character device files for pseudo-terminal slaves and master are located in the /dev/pts directory:

# ls -l /dev/pts
total 0
crw--w----. 1 guest tty  136, 0 Mar 17 10:11 0
crw--w----. 1 guest tty  136, 1 Mar 17 10:53 1
crw--w----. 1 guest tty  136, 2 Mar 17 10:11 2
c---------. 1 root  root   5, 2 Mar 17 08:16 ptmx

Some device entries, such as stdin for the standard input, are symbolically linked via the self subdirectory of the proc file system. The pseudo-terminal device file to which they actually point depends on the context of the process.

# ls -l /proc/self/fd/[012]
total 0
lrwx------. 1 root root 64 Mar 17 10:02 0 -> /dev/pts/1
lrwx------. 1 root root 64 Mar 17 10:02 1 -> /dev/pts/1
lrwx------. 1 root root 64 Mar 17 10:02 2 -> /dev/pts/1

Character devices such as null, random, urandom, and zero are examples of pseudo-devices that provide access to virtual functionality implemented in software rather than to physical hardware.

/dev/null is a data sink. Data that you write to /dev/null effectively disappears but the write operation succeeds.

/dev/zero is a data source of a stream of zero-value bytes.

/dev/random and /dev/urandom are data sources of streams of pseudo-random bytes. To maintain high-entropy output, /dev/random blocks if its entropy pool does not contains sufficient bits of noise. /dev/urandom does not block and, as a result, the entropy of its output might not be as consistently high as that of /dev/random. However, neither /dev/random nor /dev/urandom are considered to be truly random enough for the purposes of secure cryptography.

You can find out the size of the entropy pool and the entropy value for /dev/random from virtual files under /proc/sys/kernel/random:

# cat /proc/sys/kernel/random/poolsize 
4096
# cat /proc/sys/kernel/random/entropy_avail 
3467

For more information, see the null(4), pts(4), and random(4) manual pages.