Device file

Also known as: device node · special file

On this page 7

A device file is a special entry in the file system that represents a hardware device or kernel service, so that programs can reach it through the same open, read, write and close calls they use for regular files. In operating systems derived from Unix, including Linux, the BSD family and macOS, device files usually live under /dev, and they are also called a device node or a special file.

Device files embody the Unix principle that everything is a file. Within the Drivers & devices layer of an operating system, they form the boundary between user space and a device driver: the file itself stores no data, only a pointer to the driver that will handle every operation on it.

How a device file connects programs to drivers

A device file is an inode whose type marks it as a device and which carries a major number and a minor number instead of data blocks. When a process opens it, the kernel uses the major number to select the driver and passes the minor number to that driver, which uses it to identify a specific unit, partition or channel.

After the open succeeds, the kernel routes system calls to the driver’s file operations table. A read on /dev/ttyS0 becomes a call into the serial driver, and an ioctl request carries device-specific commands that do not fit the byte-stream model, such as setting a baud rate or ejecting media.

The table below lists the attributes stored in a device file and what each controls.

AttributeExamplePurpose
File typec or b in ls -l outputMarks a character or block device
Major number8 for SCSI disks on LinuxIdentifies the responsible driver
Minor number1 for the first partition of sdaIdentifies the unit or partition
Owner and grouproot:diskControls who may open the device
Permission bits0660Grants read and write rights
Path name/dev/sda1Stable name used by programs
Attributes of a device file

Character, block and pseudo device files

Device files fall into two kernel types: character devices and block devices. A character device transfers data as a stream of bytes without kernel buffering of fixed blocks, while a block device offers random access in fixed-size blocks through the page cache and an I/O scheduler, which suits storage.

Many device files correspond to no physical hardware at all. These pseudo devices expose kernel functions through the same interface, which makes them useful in scripts and testing. The comparison below summarizes the main categories.

CategoryTypical examplesAccess modelMain use
Character device/dev/ttyS0, /dev/input/event0Byte streamTerminals, serial ports, input
Block device/dev/sda, /dev/nvme0n1Fixed-size blocksDisks and partitions
Pseudo device/dev/null, /dev/zero, /dev/urandomByte streamDiscarding data, zeros, randomness
Virtual terminal/dev/tty1, /dev/pts/0Byte streamConsoles and terminal emulators
Loop device/dev/loop0Fixed-size blocksMounting disk image files
Categories of device files

Some systems blur the distinction. FreeBSD removed block device files and exposes storage only as character devices, relying on the file system layer for caching, while Linux keeps both types and also offers raw access through flags such as O_DIRECT.

How device files are created and managed

Early Unix systems used a static `/dev` directory filled with nodes created by the mknod command or a MAKEDEV script, often thousands of entries for hardware that was never present. Modern systems create device files dynamically as devices appear during device enumeration and plug and play events.

  • Linux mounts devtmpfs, where the kernel itself creates nodes, and the udev daemon (part of systemd) applies rules for permissions, ownership and persistent symbolic links such as /dev/disk/by-uuid/.
  • FreeBSD uses devfs, a kernel file system that shows only existing devices and applies rules through devfs.rules.
  • macOS also uses a devfs implementation, populated by the I/O Kit driver framework, with names such as /dev/disk2s1.

Persistent names matter because kernel names such as sda and sdb depend on detection order and can change between boots. Configuration files like /etc/fstab should therefore reference UUIDs or labels rather than raw kernel names.

Device files compared with other device interfaces

Device files are not the only way an operating system exposes hardware. The table below compares them with related mechanisms so that the right interface can be chosen for a task.

InterfacePlatformTypical contentBest suited for
Device file in /devUnix-like systemsData path to a driverReading and writing device data
sysfs in /sysLinuxAttributes and topologyInspecting and configuring devices
procfs in /procLinux and othersProcess and kernel stateMonitoring and diagnostics
Device object namespaceWindowsNamed kernel objects such as \\.\PhysicalDrive0Opening devices with CreateFile
Memory-mapped I/OAll platformsDevice registers in address spaceDriver-level hardware control
Device files and alternative interfaces

Windows has no `/dev` directory, but its object manager plays a similar role: drivers create named device objects, and user programs open them through symbolic links in the \\.\ namespace. The concept is equivalent even though the file system does not show the entries.

Security risks and common mistakes

Because a device file grants direct access to hardware, its permissions are a security boundary. Write access to a raw disk node bypasses every file permission on the file systems it contains, and access to /dev/mem historically allowed reading kernel memory, which is why modern kernels restrict it.

ProblemCauseRemedy
Permission denied on a serial portUser not in the dialout groupAdd the user to the group or write a udev rule
Wrong disk overwritten by ddKernel names changed orderVerify with lsblk and use persistent names
Device node missing after bootDriver not loaded or rule failedCheck dmesg and udev logs
Container sees host disksOverly broad device accessGrant only specific devices to the container
Mounted file system allows device nodesMissing nodev optionMount untrusted media with nodev
Frequent problems with device files

A common misconception is that deleting a device file removes the device. Removing the node only removes the name; the driver and hardware remain, and udev or devtmpfs will usually recreate the entry at the next event or boot.

In simple terms

A device file is like a labeled door in a hallway. Behind each door is not a room full of papers but a worker who handles a machine, such as a printer, a disk or a keyboard. Programs do not need to know how each machine works; they simply knock on the right door and hand over or collect data.

This is why so many tools work with hardware so easily. The same command that copies a normal file can copy data from a disk, because the device looks like a file from the outside.

A practical example

A system administrator needs to write a 4 GB Linux installer image to a USB stick on a laptop that also has an internal NVMe drive. Running lsblk shows the internal drive as /dev/nvme0n1 and the newly inserted 32 GB stick as /dev/sda, a block device created by udev seconds after insertion.

The administrator confirms the size and model, then runs dd with the image as input and /dev/sda as output, using a block size of 4 MB and the status=progress option. After about four minutes the copy finishes, sync flushes the remaining buffers, and the stick boots correctly on the target machine. Because the administrator verified the device file name before writing, the internal drive stayed untouched.