In Linux, every part is a file, even bodily units similar to disk drives, CD/DVD ROM, and floppy disks are represented utilizing recordsdata. Nonetheless, these recordsdata are usually not common knowledge recordsdata. As a substitute, these particular recordsdata are known as system recordsdata they usually can generate or obtain the info.
Normally, all of the particular recordsdata are current below the /dev listing. A few of the widespread examples of particular recordsdata are /dev/null, /dev/zero, /dev/full, and /dev/sr0.
The particular recordsdata could be of the sort character or block. Because the identify suggests, the character recordsdata course of one character at a time the place because the block recordsdata course of a number of characters at a time.
On this superior information, we are going to focus on create a tool file utilizing the mknod command. After following this information, Linux customers will have the ability to work with completely different sorts of system recordsdata confidently.
mknod Command Syntax
The syntax of the mknod command is barely completely different than the different Linux instructions. The principle distinction is that we have to specify the key and minor numbers whereas creating a personality and block system recordsdata:
$ mknod [OPTIONS] [MAJOR] [MINOR] ...
Within the above syntax, the sq. brackets ([])
symbolize the optionally available arguments whereas angular brackets (<>)
symbolize the obligatory arguments.
In Linux, the key quantity is used to specify the system driver that must be used. This quantity is assigned whereas registering a tool driver.
In the same manner, the minor quantity is used to specify the precise system. We are able to discover the reserved main and minor numbers within the Linux kernel’s documentation.
1. Create Unnamed Pipe in Linux
In Linux, we use pipes to allow inter-process communication. In Shell, the pipe is represented utilizing the vertical line (|)
. It reads the output of the earlier course of and sends it because the enter to the following course of.
Let’s perceive this with a easy instance:
$ echo "tecmint.com" | wc -c
On this instance, we’re utilizing the pipe to learn the output of the echo command and ship it because the enter to the wc command for additional processing.
Right here, now we have used the vertical line (|)
to create a pipe. Nonetheless, this creates an unnamed pipe and its scope is proscribed to the present command solely. Within the subsequent instance, we are going to see create a named pipe.
2. Create a Named Pipe in Linux
We are able to use the mknod command to create a named pipe. The named pipe resides on the file system identical to some other common file. Nonetheless, its primary function is to permit inter-process communication between unrelated processes.
First, let’s specify the system kind as p
to create a named pipe:
$ mknod /tmp/named-pipe p $ ls -l /tmp/named-pipe
Now, let’s attempt to learn the file:
$ cat /tmp/named-pipe
Right here, we will see that the cat command is ready infinitely:
Subsequent, let’s open one other terminal and write some knowledge to the /tmp/named-pipe file:
$ echo "tecmint.com" > /tmp/named-pipe
Lastly, head over to the primary terminal to view the outcome:
Right here, we will see that the echo and cat instructions are in a position to talk utilizing the named pipe.
3. Create a Character System File in Linux
In the same manner, we will use the system kind as c
to create a personality system. Nonetheless, we will need to have to make use of the key and minor numbers whereas creating a personality system.
Let’s use the ls command to search out the key and minor numbers of the /dev/full system:
$ ls -l /dev/full
Within the above output, the comma-separated pair of numbers i.e. 1, 7 symbolize the key and minor numbers respectively.
In Linux, the /dev/full system at all times returns the No house left on the system error. To know this, let’s write some knowledge to this system:
$ echo "tecmint.com" > /dev/full
Now, let’s use the mknod command to create a brand new system that behaves the identical as /dev/full system:
$ sudo mknod /tmp/full-device c 1 7
Subsequent, let’s change the file permissions:
$ sudo chmod 666 /tmp/full-device $ ls -l /tmp/full-device
Lastly, write some knowledge to the newly created system:
$ echo "tecmint" > /tmp/full-device
It is very important word that, the newly created character system behaves just like the /dev/full system due to the identical main and minor numbers.
4. Create a Block System File in Linux
Along with this, we will specify the system kind as b
to create a block system. To create a block system we will need to have to make use of the key and minor numbers.
In Linux, /dev/sr0 represents the CD/DVD ROM system. Now, let’s create a brand new block system that behaves the identical as /dev/sr0.
First, let’s discover out the key and minor numbers of the /dev/sr0:
$ ls -l /dev/sr0
Within the above output, we will see that its main and minor numbers are 11 and 0 respectively.
Now, let’s create a brand new block system with the identical main and minor numbers:
$ sudo mknod /tmp/dvd-rom b 11 0 $ ls -l /tmp/dvd-rom
Subsequent, let’s mount the ISO picture from the CD/DVD ROM to the /mnt listing and confirm that the mount operation succeeds:
$ sudo mount /tmp/dvd-rom /mnt/ $ ls -1 /mnt/
On this instance, we will see that the /tmp/dvd-rom block system is ready to entry the ISO picture from the CD/DVD ROM.
5. Create a System File With Permissions
Generally, we have to modify the entry permission of the system file earlier than utilizing it. In such circumstances, now we have to make use of the chmod command. Nonetheless, we will obtain the identical outcome utilizing the mknod command as a substitute of utilizing two separate instructions.
To know this, let’s use the -m
choice to set entry permissions whereas making a named pipe:
$ sudo mknod -m 444 /tmp/pipe-with-permissions p
Now, let’s confirm that the permissions have been set correctly:
$ ls -l /tmp/pipe-with-permissions
On this instance, we used the -m
choice with the named pipe. Nonetheless, we will use it with the character and block units as nicely.
Are you aware of some other greatest instance of the mknod command in Linux? Tell us your views within the feedback under.