
mknod Command in Linux
The mknod command in Linux is used to create special files. These files are essential for interacting with hardware devices, like hard drives and printers. They are typically found in the /dev directory. These files allow programs and users to communicate with physical hardware or kernel-managed resources. Special files are different from regular files, as they represent devices rather than data stored on a disk.
Table of Contents
Here is a comprehensive guide to the options available with the mknod command −
- What is mknod Command in Linux?
- Syntax of mknod Command
- Understanding Working of mknod Command
- Examples of mknod Command in Linux
- Why Use mknod Command in Linux?
- Common Use Cases of mknod Command
What is mknod Command in Linux?
The mknod command creates special files in Linux. These files represent hardware devices (like hard drives, printers, or terminals), and the system uses them to interact with the actual hardware. There are two types of special files −
Block devices
These represent devices that transfer data in blocks, such as hard drives and USB drives. They usually allow random access to data.
Character devices
Character devices represent devices that transfer data one character at a time, like keyboards or serial ports. With the mknod command, you can manually create these device files and assign numbers that help the system recognize the device type and the device itself.
Syntax of mknod Command
The basic syntax of the mknod command is demonstrated below −
mknod [OPTION] NAME TYPE MAJOR MINOR
Let's understand the syntax of the mknod command −
- NAME represents the name of the file you want to create. It's usually in the /dev directory.
- TYPE defines what kind of device you're creating: b is for block devices (like hard drives) and c is for character devices (like printers).
- MAJOR is a number that identifies the device driver.
- MINOR is a number that helps distinguish between different devices managed by the same driver.
- You can use -m MODE to set file permissions (default is 0666), --help to display help, or --version to show the mknod version.
Understanding Working of mknod Command
When you run the mknod command, it creates a file in a specified directory linked to a device. The system uses major and minor numbers to access and manage the device.
The major number identifies the device type (like disk drivers), and the minor number refers to a specific device instance (like the first hard drive, /dev/sda).
Here's how the kernel uses this information −
- The major number tells the kernel which driver to use for the device.
- The minor number helps the kernel know which specific device to interact with.
- After the device file is created, you can use commands like cat, dd, or mount a block device to interact with the device.
You can learn more about the mknod command by reading its manual page −
man mknod

Examples of mknod Command in Linux
Let's explore some practical examples to see how the mknod command works in Linux −
Creating a Block Device With mknod
Let's say we have to create a block device file for a hard drive with a major number 8 and a minor number 0. This block device represents a storage device like /dev/sda on our system.
To create this device, we will execute the mknod command as follows −
mknod /dev/sdb b 8 1
After this command, use the ls -l command to confirm the device creation −
ls -l /dev/sdb
The output confirms that a block device /dev/sdb has been created with major number 8 and minor number 1 −

Creating a Character Device With mknod
For a character device, like a serial port, which typically has a major number of 4 and a minor number of 64, you can create the device file like this −
mknod /dev/ttyS0 c 4 64
Creating a Named Pipe (FIFO) with mknod
A named pipe (FIFO) is a special file that lets different processes communicate with each other. You can create a named pipe using the mknod command by setting the type to p, as shown below −
mknod /tmp/fifoExample p
Setting Permissions with the -m Option
By default, device files created with mknod have permissions set to 0666. However, you can change these permissions using the -m option.
For example, you can run the following command to create a device file with read and write permissions for the owner and group, and no permissions for others −
mknod -m 0660 /dev/mydevice b 8 10
Removing a Device
If a device is no longer needed or if a device already exists but is broken, you can remove it using the following command −
sudo rm /dev/sdb
You can confirm the device deletion by executing the ls -l command −
ls -l /dev/sdb

Why Use mknod Command in Linux?
mknod is useful for creating device files manually, especially when dealing with custom or virtual hardware, containers, or devices not automatically recognized by Linux. The points below shows why we should use this command in Linux −
- While most device files are created automatically by the system during startup, mknod lets you manually create custom device files, which is helpful for virtual or uncommon hardware.
- In environments like Docker containers, where device files might not exist, mknod can be used to create them.
- If you're working with custom hardware or devices not automatically detected by Linux, mknod helps create device files to interact with those devices.
Common Use Cases of mknod Command
Here are some common use cases where mknod is particularly useful −
- Use mknod to add or test new hardware, like printers or hard drives, by creating their device files.
- If the /dev directory gets damaged, mknod can restore the missing device files.
- In virtual environments, where device files may be missing, mknod can be used to create them.
That's all about the use of the mknod command in Linux.
Conclusion
The mknod command in Linux is a useful tool for creating special files. It allows you to create special files that help the system communicate with hardware devices. These files, such as those for hard drives or printers, enable programs and the system to interact with the hardware.
By using mknod, you can manually create device files for devices that aren't automatically set up by the system. This is useful for custom hardware or virtual environments like Docker. It's especially helpful when you need to restore or add device files. In this article, we explained how to use the mknod command in Linux.