Kahibaro
Discord Login Register

1.5.2.5 `/dev`

The Purpose of `/dev`

The /dev directory is where Linux exposes hardware devices and some virtual devices as files. Instead of having special system calls for every piece of hardware, Linux treats many devices as ordinary files that you can open, read from, and write to. Programs then interact with hardware through these special files.

You do not usually create or manage /dev entries by hand on a modern system. A system component called udev detects hardware and populates /dev automatically.

Important: Files in /dev are not regular files. Reading or writing them can affect hardware, data, and the system state. Never experiment blindly with /dev on a system that contains important data.

Device Files as an Interface

A file in /dev is often called a device node or device file. It is a handle the kernel provides so that user programs can access a device.

When a program opens a device file and calls read or write, the kernel passes the request to the corresponding device driver, which talks to the hardware. From the program’s point of view, working with a device can look similar to working with an ordinary file, even though the underlying effect is very different.

This design allows tools like cp or cat to operate with both regular files and some devices, because they simply read from one file descriptor and write to another.

Common Device Categories in `/dev`

Within /dev you will see many entries, but beginners mostly encounter a few categories.

Character devices handle data as a stream of bytes, such as terminals and serial ports. Block devices handle data in blocks, such as disks. There are also purely virtual devices that represent system concepts rather than physical hardware.

Many systems also contain subdirectories under /dev, for example /dev/disk and /dev/pts, which group related devices together.

Terminals and Consoles

Text input and output in a terminal session is also exposed through /dev.

The current terminal session is usually available as /dev/tty. When you type into a shell and see text on the screen, the shell is reading from and writing to this device.

There are separate devices for virtual consoles such as /dev/tty1, /dev/tty2, and so on. On systems with a graphical environment and multiple terminal windows, you will also find pseudo terminals in /dev/pts/. Each terminal window corresponds to a device such as /dev/pts/0, /dev/pts/1, and so forth.

Disks and Storage Devices

Storage devices are presented as block devices. Physical disks typically appear as /dev/sdX, where X is a letter, such as /dev/sda or /dev/sdb. Partitions on these disks are given numbers, for example /dev/sda1 or /dev/sdb2. You can refer to these device files when partitioning, formatting, or mounting storage.

Other naming schemes also exist, for example /dev/nvme0n1 on systems with NVMe drives, with partitions like /dev/nvme0n1p1.

There are additional directories such as /dev/disk/by-uuid or /dev/disk/by-label which provide stable names for disks and partitions. These names are especially useful in configuration files, since they do not depend on changeable device letters.

Input and Output Devices

Human input devices, like keyboards and mice, are represented in /dev/input. Raw events generated by these devices appear through files such as /dev/input/event0 and similar. Normal desktop users rarely access these devices directly, but graphical environments and input libraries use them.

Sound devices also appear in /dev, often under names like /dev/snd/*. Audio software interacts with these to play or capture sound, sometimes through additional layers like ALSA or PulseAudio.

Printers, serial ports, and some other communication interfaces still appear with traditional style names on some systems, for example /dev/lp0 for a parallel printer or /dev/ttyS0 for a serial port.

Virtual Devices

Some entries in /dev do not correspond to physical hardware at all. They represent special functions provided by the kernel and are useful tools in many situations.

/dev/null is a write only sink. Anything you write to it is discarded. Reading from /dev/null returns immediate end of file. It is commonly used to ignore unwanted output. For example, redirecting output to /dev/null prevents it from appearing on the screen or being saved.

/dev/zero provides as many zero bytes as you read from it. Programs can use it when they need a stream of zero filled data, for example to quickly initialize storage or memory regions.

/dev/random and /dev/urandom provide random or pseudo random data generated by the kernel. They are used for cryptography, key generation, and other tasks that require unpredictability. The difference between them concerns how much they rely on collected entropy and how they behave if there is not enough of it, which is a more advanced topic.

There are other virtual devices with specialized purposes. While you may not interact with them directly as a beginner, they support many system level operations and tools behind the scenes.

Dynamic Management of `/dev`

On modern Linux systems /dev is not a static directory stored on disk. It is usually a temporary filesystem managed in memory and filled dynamically at boot and when hardware appears or disappears.

The kernel reports hardware events, and a user space service such as systemd-udevd reacts by creating or removing the corresponding entries in /dev. This is why a newly attached USB drive becomes visible as a new device file without a reboot.

You can see this dynamic behavior by observing how new names appear in /dev when you plug in storage devices or other peripherals. Administrators can influence naming and permissions through udev rules, but such customization belongs to more advanced topics.

Basic Safety Considerations

Because /dev provides a low level interface to hardware and special kernel functions, careless interaction with it can cause harm. For example, writing directly to a disk device file without understanding what you are doing can destroy partition tables or file systems.

Normal everyday use of Linux does not require manually writing to these device files. High level commands and tools that operate on disks, networks, or terminals use these devices on your behalf, in a controlled way.

If you see documentation that instructs you to run a command using a device from /dev, always pay close attention to which device name is used and what the command does. On multi disk systems, a single letter difference in a name can refer to a different drive entirely.

With this understanding, you can recognize /dev as the bridge between user space and devices. It is a critical part of how Linux exposes hardware and special kernel features while preserving the unified model of working with files.

Views: 60

Comments

Please login to add a comment.

Don't have an account? Register now!