The βmknod
β (or make node) command was originally used to create the character and block devices that reside in the β/devβ virtual filesystem. However, these devices are not regular data files but special files, and modern systems can now create or delete them by detecting the corresponding hardware.
You might have definitely heard of some popular special files such as β/dev/zeroβ, β/dev/sdaβ, β/dev/nullβ, and β/dev/fullβ, and there are more that you can list by using the βls /dev
β command.
The difference between character and block devices is that character devices process one character at a time without buffering, whereas block devices process multiple characters at once and provide buffered access to hardware devices.
While there is no requirement to handle these devices manually, in rare cases where the content of β/devβ is not loaded, accidentally deleted, or something else happens, you can use the βmknod
β command to repopulate them; however, the process can be stressful, so always take backups.
In this article, Iβll show you how to create a character and block devices using the βmknod
β command in Linux.
Tutorial Details
Description | Mknod Command |
Difficulty Level | Moderate |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | β |
Internet Required | No |
How to Use the Mknod Command in Linux
The βmknod
β command takes four arguments: the output file path, the option, the major number, and the minor number.
$ mknod </path/of/the/output/file> <option> <major-number> <minor-number>
The major and minor numbers are used to identify the device driver, with the major number specifying which device driver to use when interacting with the device; every type of device, such as hard drives, CD/DVD ROMs, floppy disks, etc., is associated with different major numbers.
The minor number is used to distinguish between different instances of the same type of device. For example, if you have multiple disk drives connected to your system, each drive would have a unique minor number.
Additionally, you can use an option to specify the type of device you are working with.
Option | Defination |
---|---|
c , u | It indicates that the special file is a character-oriented (unbuffered) device. |
b | It indicates that the special file is a block-oriented (buffered) device. |
p | Creates FIFOs (named pipelines). |
Now, letβs see some examples on the usage of the βmknod
β command.
1. Identify the Block and Character Devices in β/devβ Path
The β/devβ contains all the block and character devices in your system, but theyβre usually mixed together. To distinguish between block and character devices, you can check the first character of their file mode using the following command:
$ ls -l /dev
Output:
In the image above, the first entry beginning with βc
β denotes the character device, whereas the second entry starting with βb
β denotes the block device.
If you want to list a specific type of device, such as only listing block or character devices, you can use one of the appropriate commands.
# It will only list the block devices.
$ ls -l /dev | grep '^b'
# It will only list the character devices.
$ ls -l /dev | grep '^c'
2. Create a Block Device
To create a block device, you must first specify the path of the output file, assign the option βb
β to indicate it as a block device, along with major and minor numbers.
Almost everything is easy to understand, except you need to be a little extra careful when specifying the major and minor numbers. For example, in Linux, there are β/dev/sda*β and β/dev/sr*β devices representing the block devices, and you can follow the previous method to determine their major and minor numbers.
Here, the β/dev/sdaβ device has a major number β8
β and a minor number β0
β, while the β/dev/sr0β device has a major number β11
β and a minor number β0
β. Now, if you want to create a new block device with the same major and minor numbers, you can use the following command:
# Create a temporary "sda" device file.
$ sudo mknod /tmp/my-sda b 8 0
# Create a temporary "sr0" device file.
$ sudo mknod /tmp/my-sr0 b 11 0
# Check the file mode of device files.
$ ls -l /tmp/my-*
Output:
You can now treat these temporary block devices as actual ones, and if they are mountable, you can even mount them. If the need arises in the future, you can use this method to create actual block devices at the β/devβ location.
3. Create a Character Device
The character device can be created in the same way as a block device, where you need to first specify the path of the output file, assign the option βc
β to indicate it as a character device, and provide major and minor numbers.
The β/dev/fullβ, β/dev/nullβ, β/dev/ttyβ, and β/dev/zeroβ are some of the popular character devices in Linux whose major and minor numbers you can check to create a duplicate copy of them.
Letβs say you want to create a β/dev/fullβ device that always returns an βNo space left on the deviceβ error. Then you can use the β1
β that represents the major number and the β7
β that represents the minor number to create a new character device that behaves like a β/dev/fullβ device.
# Create a temporary "full" device file.
$ sudo mknod /tmp/my-full c 1 7
# Check the file mode of device file.
$ ls -l /tmp/my-*
Output:
Once you create the temporary device, you can use the chmod command to grant full permission. After that, when you try to redirect any data to the newly created device, you will encounter the βNo space left on the deviceβ error.
# Assign a full permission to the device file.
$ sudo chmod 666 /tmp/my-full
# Redirect some data to the newly device file.
$ echo "LinuxTLDR" > /tmp/my-full
Output:
This way, you can create a duplicate of the β/dev/fullβ or any other character devices by using the correct option and specifying the correct major and minor numbers.
4. Create a FIFO (queue) Device
Let me present a scenario: In Linux, when you create a file using the touch command, there may be instances where you also need to assign specific permissions using chmod. However, you can combine these multiple steps by using the βmknod
β command.
For example, you can use the β-m
β flag to set read permissions while creating a named pipe with the mknod βp
β option.
# Create a FIFO pipe file.
$ sudo mknod -m 444 /tmp/my-pipe-file p
# Check the file.
$ ls -l /tmp/my-pipe-file
Output:
Now, only the user can read this file; any attempt to edit it will result in a βpermission deniedβ error.
Final Word
This article has come to an end. Today, most of you wonβt bother about creating a device file, but learning about this tool could not harm you. However, if you know of any better use cases for this tool, then do let us know in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.