Device File
Special files that represent hardware and kernel services, letting programs read a disk, a mouse, or a stream of random bytes with the same calls they use on ordinary files.
A device file is a special file that represents a piece of hardware or a kernel service. On Unix-like systems they live in the /dev directory. Reading /dev/sda reads the raw bytes of the first disk; writing to a terminal's device file prints on it. This is the clearest expression of the Unix idea that everything is a file: programs reach devices with the same open, read, and write calls they use for ordinary files; see System Call.
There are two main kinds:
- Block devices represent storage, read and written in fixed-size blocks at any position. The kernel buffers them in memory; see Page Cache. Disks and each of their partitions appear as block devices.
- Character devices carry a stream of bytes, such as terminals, serial ports, sound cards, and input devices; see Pseudoterminal.
A device file holds no data. Its Inode stores two numbers: a major number identifying the driver and a minor number identifying which device it handles. Opening the file hands the request to that driver inside the Kernel. Operations that don't fit reading and writing, such as ejecting a disc or setting a serial port's speed, go through a catch-all call named ioctl.
Some device files have no hardware behind them:
/dev/nulldiscards everything written to it and returns nothing when read, the standard place to throw away output./dev/zeroproduces endless zero bytes, used to fill files or wipe disks./dev/urandomproduces random bytes from the kernel's generator; see Pseudorandom Number Generator.
Early Unix systems created device files by hand, and distributions shipped thousands of them for hardware that might never exist. Linux's udev, from 2003, creates them dynamically as devices appear and applies rules to name them. Since detection order can change between boots, udev also adds stable names based on serial numbers and IDs, so a script can refer to the same disk even if it moves from sda to sdb.
Device files are also a security boundary, controlled by ordinary File Permissions. A program that can read the raw disk can bypass every file permission on it; a program that can read input devices can log every keystroke. Distributions grant access through groups, such as one for video hardware, and containers are usually given only a short list of safe devices; see Control Groups and Least Privilege.
Their power makes mistakes costly. A disk-copying command pointed at the wrong device file overwrites a drive without asking, and a stray redirection into a disk device can destroy a filesystem.
See also3
Graphics Driver
The software that translates a program's drawing commands into instructions a particular graphics card can execute, split between a kernel part that manages memory and displays and a large user-space part that compiles shaders and implements graphics interfaces.
Systems & Tooling17 connections
Sound Server
The background service that sits between applications and audio hardware, mixing many programs' sound together, routing it to the right speakers or headphones, and handling volume, device switching and latency.
Systems & Tooling8 connections
Serial Port
The old, simple connection that sends data one bit at a time, inherited from teleprinters and modems, gone from consumer computers but still the last-resort console for servers, network gear, and embedded boards.
Systems & Tooling10 connections
Linked from7
- Control GroupsSystems & Tooling
The Linux kernel feature that caps and accounts for a process group's CPU, memory, and I/O — the resource-limiting half of a container.
- InodeSystems & Tooling
The record a Unix filesystem keeps about a file's metadata and data location — everything except its name.
- Pseudorandom Number GeneratorComputation & Algorithms
An algorithm that produces a sequence of numbers that looks random but is fully determined by a starting seed, and the separate, stricter kind needed for security.
- Serial PortSystems & Tooling
The old, simple connection that sends data one bit at a time, inherited from teleprinters and modems, gone from consumer computers but still the last-resort console for servers, network gear, and embedded boards.
- Sound ServerSystems & Tooling
The background service that sits between applications and audio hardware, mixing many programs' sound together, routing it to the right speakers or headphones, and handling volume, device switching and latency.
- System CallSystems & Tooling
The doorway through which a program asks the operating system kernel to do anything privileged, from reading a file to starting a process.
- Universal Serial BusSystems & Tooling
The connector standard that replaced a drawer of serial, parallel, and keyboard ports in the late 1990s, built around a host that polls every device, and later a single reversible plug for data, video, and power.