mknod
Create device files (special files)
TLDR
Create a block device
Create a character device
Create a FIFO (queue) device
Create a device file with default SELinux security context
SYNOPSIS
mknod [OPTION]... NAME TYPE [MAJOR MINOR]
NAME: Pathname of the new special file.
TYPE: One of 'b' (block), 'c' (character), or 'p' (FIFO/pipe).
MAJOR: For 'b' or 'c' types, the major device number.
MINOR: For 'b' or 'c' types, the minor device number.
PARAMETERS
-m, --mode=MODE
Set file permissions (octal or symbolic MODE).
-Z, --context=CTX
Set the SELinux security context to CTX.
--help
Display help message and exit.
--version
Output version information and exit.
b
Create a block special file. Requires MAJOR and MINOR.
c
Create a character special file. Requires MAJOR and MINOR.
p
Create a FIFO (named pipe). No major/minor needed.
MAJOR
Major device number, identifies device type.
MINOR
Minor device number, identifies specific device instance.
DESCRIPTION
mknod (make node) is a command-line utility used to create special files on a Linux or Unix-like system. These special files are not regular files or directories; instead, they represent interfaces to hardware devices or act as named pipes for inter-process communication (IPC). The command supports creating three primary types of nodes: block special files (b), typically used for disk drives or partitions; character special files (c), used for streaming devices like terminals or printers; and FIFO (First-In, First-Out) special files, also known as named pipes (p), which facilitate communication between unrelated processes. Creating device files (block or character) usually requires superuser privileges and specific major and minor device numbers, which identify the device and its instance within the system. Named pipes, however, can generally be created by any user in their own writable directories. mknod is a fundamental tool for system administrators and developers managing the /dev directory or setting up complex IPC mechanisms.
CAVEATS
Creating block or character special files typically requires root privileges. Incorrect major and minor device numbers can lead to system instability or prevent devices from functioning. Modern Linux distributions often manage device nodes dynamically via systems like udev, making manual mknod usage for device files less common for everyday operations, though it remains crucial for specific scenarios or embedded systems. For creating named pipes, the mkfifo command is generally preferred due to its simpler syntax, though mknod can achieve the same result.
DEVICE NUMBERS (MAJOR & MINOR)
Major numbers identify the general class or type of device driver (e.g., all SCSI disks share a major number). Minor numbers identify specific instances of that device (e.g., sda1, sdb, etc.) or sub-units within a device. These numbers are crucial for the kernel to correctly route I/O operations to the appropriate device.
PERMISSIONS
By default, mknod creates files with permissions a=rw,u+X,go+X (0666 for a file) modified by the umask. The -m, --mode=MODE option allows specifying precise permission bits for the newly created special file, which is especially important for security and controlling access to device nodes.
HISTORY
The mknod command has been a fundamental part of Unix-like operating systems since their early development. Its original purpose was to provide a means to create device entries in the /dev directory, which were essential for the operating system to interact with hardware peripherals. Over time, as Unix evolved into various flavors like Linux, mknod remained a core utility. While its direct use for creating standard device nodes has diminished somewhat with the advent of dynamic device management systems (like devfs and more recently udev in Linux), it continues to be vital for specific purposes, such as creating custom device drivers, setting up chroot environments, or manually restoring device nodes in emergency situations. Its role in creating named pipes (FIFOs) for inter-process communication also remains highly relevant.