In Linux, everything is a file, even physical devices such as disk drives, CD/DVD ROM, and floppy disks are represented using files. However, these files are not regular data files. Instead, these special files are called device files and they can generate or receive the data.
Usually, all the special files are present under the /dev directory. Some of the common examples of special files are /dev/null, /dev/zero, /dev/full, and /dev/sr0.
The special files can be of the type character or block. As the name suggests, the character files process one character at a time where as the block files process multiple characters at a time.
In this advanced guide, we will discuss how to create a device file using the mknod command. After following this guide, Linux users will be able to work with different kinds of device files confidently.
mknod Command Syntax
The syntax of the mknod command is slightly different than the other Linux commands. The main difference is that we need to specify the major and minor numbers while creating a character and block device files:
$ mknod [OPTIONS] [MAJOR] [MINOR] ...
In the above syntax, the square brackets ([])
represent the optional arguments whereas angular brackets (<>)
represent the mandatory arguments.
In Linux, the major number is used to specify the device driver that needs to be used. This number is assigned while registering a device driver.
In a similar way, the minor number is used to specify the actual device. We can find the reserved major and minor numbers in the Linux kernel’s documentation.
1. Create Unnamed Pipe in Linux
In Linux, we use pipes to enable inter-process communication. In Shell, the pipe is represented using the vertical line (|)
. It reads the output of the previous process and sends it as the input to the next process.
Let’s understand this with a simple example:
$ echo "tecmint.com" | wc -c
In this example, we are using the pipe to read the output of the echo command and send it as the input to the wc command for further processing.
Here, we have used the vertical line (|)
to create a pipe. However, this creates an unnamed pipe and its scope is limited to the current command only. In the next example, we will see how to create a named pipe.
2. Create a Named Pipe in Linux
We can use the mknod command to create a named pipe. The named pipe resides on the file system just like any other regular file. However, its main purpose is to allow inter-process communication between unrelated processes.
First, let’s specify the device type as p
to create a named pipe:
$ mknod /tmp/named-pipe p $ ls -l /tmp/named-pipe
Now, let’s try to read the file:
$ cat /tmp/named-pipe
Here, we can see that the cat command is waiting infinitely:
Next, let’s open another terminal and write some data to the /tmp/named-pipe file:
$ echo "tecmint.com" > /tmp/named-pipe
Finally, head over to the first terminal to view the result:
Here, we can see that the echo and cat commands are able to communicate using the named pipe.
3. Create a Character Device File in Linux
In a similar way, we can use the device type as c
to create a character device. However, we must have to use the major and minor numbers while creating a character device.
Let’s use the ls command to find the major and minor numbers of the /dev/full device:
$ ls -l /dev/full
In the above output, the comma-separated pair of numbers i.e. 1, 7 represent the major and minor numbers respectively.
In Linux, the /dev/full device always returns the No space left on the device error. To understand this, let’s write some data to this device:
$ echo "tecmint.com" > /dev/full
Now, let’s use the mknod command to create a new device that behaves the same as /dev/full device:
$ sudo mknod /tmp/full-device c 1 7
Next, let’s change the file permissions:
$ sudo chmod 666 /tmp/full-device $ ls -l /tmp/full-device
Finally, write some data to the newly created device:
$ echo "tecmint" > /tmp/full-device
It is important to note that, the newly created character device behaves like the /dev/full device because of the same major and minor numbers.
4. Create a Block Device File in Linux
In addition to this, we can specify the device type as b
to create a block device. To create a block device we must have to use the major and minor numbers.
In Linux, /dev/sr0 represents the CD/DVD ROM device. Now, let’s create a new block device that behaves the same as /dev/sr0.
First, let’s find out the major and minor numbers of the /dev/sr0:
$ ls -l /dev/sr0
In the above output, we can see that its major and minor numbers are 11 and 0 respectively.
Now, let’s create a new block device with the same major and minor numbers:
$ sudo mknod /tmp/dvd-rom b 11 0 $ ls -l /tmp/dvd-rom
Next, let’s mount the ISO image from the CD/DVD ROM to the /mnt directory and verify that the mount operation succeeds:
$ sudo mount /tmp/dvd-rom /mnt/ $ ls -1 /mnt/
In this example, we can see that the /tmp/dvd-rom block device is able to access the ISO image from the CD/DVD ROM.
5. Create a Device File With Permissions
Sometimes, we need to modify the access permission of the device file before using it. In such cases, we have to use the chmod command. However, we can achieve the same result using the mknod command instead of using two separate commands.
To understand this, let’s use the -m
option to set access permissions while creating a named pipe:
$ sudo mknod -m 444 /tmp/pipe-with-permissions p
Now, let’s verify that the permissions have been set properly:
$ ls -l /tmp/pipe-with-permissions
In this example, we used the -m
option with the named pipe. However, we can use it with the character and block devices as well.
Do you know of any other best example of the mknod command in Linux? Let us know your views in the comments below.