How to Create Device Files Using the Mknod Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

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

DescriptionMknod Command
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredNo

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.

OptionDefination
c, uIt indicates that the special file is a character-oriented (unbuffered) device.
bIt indicates that the special file is a block-oriented (buffered) device.
pCreates 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:

identify the block and character device

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.

finding the major and minor number of sda and sr files

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:

create block device in linux

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.

finding the major and minor number of some character devices

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:

create character device in linux

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:

accessing the created character device file

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:

creating FIFO pipe file

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.