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.
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.

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


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-*


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-*


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


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


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.