Create device files in Linux using the mknod command

by time news

In Linux, everything is a file, even physical devices such as disk drives, CD/DVD ROMs, and floppy disks are represented by files. However, these files are not regular data files. Instead, these special files are called device files, and they can either generate or receive the data.

Usually all special files are present in the directory /dev. Some of the common examples of special files are /dev/null, /dev/zero, /dev/full y /dev/sr0.

Special files can be character or block type. As the name suggests, character files process one character at a time, while block files process multiple characters at a time.

In this advanced guide, we will discuss how to create a device file using the command mknod. After following this guide, Linux users will be able to confidently work with different types of device files.

  • mknod Command Syntax
  • 1. Create Unnamed Pipe on Linux
  • 2. Create a named pipe in Linux
  • 3. Create a character device file in Linux
  • 4. Create a block device file in Linux
  • 5. Create a device file with permissions

mknod Command Syntax

The command syntax mknod is slightly different from other Linux commands. The main difference is that we need to specify the major and minor numbers while creating a character and locking device files:

$ mknod [OPCIONES] [MAYOR] [MENOR]...

In the above syntax, the square brackets ([])represent the optional arguments while the angle brackets (<>)represent the required arguments.

On Linux, the principal number is used to specify the device driver to use. This number is assigned when registering a device driver.

Similarly, the minor number is used to specify the actual device. We can find the major and minor reserved numbers in the Linux kernel documentation.

1. Create Unnamed Pipe on Linux

In Linux, we use pipes to allow communication between processes. In Shell, the pipe is represented by the vertical line (|). Reads the output of the previous process and sends it as input to the next process.

Let’s understand this with a simple example:

$ echo "linuxparty.es" | wc -c

In this example, we are using the pipeline to read the output of the echo command and send it as 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 following example, we will see how to create a named pipe.

2. Create a named pipe in Linux

We can use the command mknod to create a named pipe. The named pipe resides on the file system like any other normal file. However, its main purpose is to enable interprocess communication between unrelated processes.

First, let’s specify the device type for pcreate a named pipe:

$ mknod /tmp/named-pipe p
$ ls -l /tmp/named-pipe 

Now, let’s try reading the file:

$ cat /tmp/tubería con nombre

Here, we can see that the cat command is waiting infinitely:

Read named pipe file

Next, let’s open another terminal and write some data to the file /tmp/named-pipe:

$ echo "linuxparty.es" > /tmp/tubería con nombre

Finally, go to the first terminal to see the result:

View named pipe file

Here, we can see that the commands echo y cat they can communicate using the named pipe.

3. Create a character device file in Linux

Similarly, we can use the device type as cto create a character device. However, we must have to use the major and minor numbers when creating a character device.

Let’s use the ls command to find the major and minor device numbers /dev/full:

$ ls -l /dev/full

Find major and minor file number

In the above output, the pair of numbers separated by commas, i.e., 1, 7 represent the largest and smallest numbers respectively.

On Linux, the device /dev/full always returns the error No space left on the device. To understand this, let’s write some data on this device:

$ echo "linuxparty.es" > /dev/completo

Write data to device file

Now, let’s use the command mknod to create a new device that behaves the same as /dev/full device:

$ sudo mknod /tmp/full c 1 7

Next, let’s change the file permissions:

$ sudo chmod 666 /tmp/full
$ ls -l /tmp/full

Finally, write some data to the newly created device:

$ echo "linuxparty" > /tmp/full

Create new device file

It is important to note that the newly created character device behaves like the device /dev/full due to 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 bto create a block device. To create a block device we must have to use the major and minor numbers.

En Linux, /dev/sr0 represents the device the CD/DVD ROM. Now, let’s create a new block device that behaves just like /dev/sr0.

First, let’s find the largest and smallest numbers of /dev/sr0:

$ ls -l /dev/sr0

Check the major and minor numbers of the device file

In the above result, we can see that its largest and smallest numbers are 11 y 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 of the CD/DVD ROM in the directory /mnt and let’s verify that the mount operation succeeds:

$ sudo mount /tmp/dvd-rom /mnt/
$ ls -1 /mnt/

If it cannot be mounted, it is due to the SELinux security environment.

Mount new block device

In this example, we can see that the block device /tmp/dvd-rom you can access the ISO image from the CD/DVD ROM.

5. Create a device file with permissions

Sometimes we need to modify the file access permission of the device before using it. In such cases, we have to use the command chmod. However, we can achieve the same result using the command mknod instead of using two separate commands.

To understand this, let’s use the option -m 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 correctly:

$ ls -l /tmp/pipe-with-permissions

Create device file with permissions

In this example, we use the option -m with the named pipe. However, we can also use it with the character and the block devices.

Do you know of any other better examples of the command mknod on linux? Let us know your views in the comments below.

Pin It

You may also like

Leave a Comment