How to Create Device Files in Linux Using mknod Command

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:

Read Named Pipe File
Read Named Pipe File

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:

View Named Pipe File
View Named Pipe File

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
Find File Major and Minor Number
Find File Major and Minor Number

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
Write Data to Device File
Write Data to Device File

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
Create New Device File
Create New Device File

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
Check Device File Major and Minor Numbers
Check Device File Major and Minor Numbers

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 
Create New Block Device
Create New Block Device

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/
Mount New Block Device
Mount New Block Device

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
Create Device File With Permissions
Create Device File 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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Narendra K
I'm an experienced and passionate software engineer with in-depth experience in Linux, Distributed systems, DevOps, and Cloud. My expertise lies in back-end web development, and the main languages in my tech stack are Java, Spring, Python, and Go. I’m a lifelong learner and an open-source enthusiast.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 Comments

Leave a Reply
  1. Excellent tutorial! Might elaborate on where to get the major/minor numbers. One could figure it out from the text but “straight forward” helps some. Thank you. SE

    Reply
  2. Hi,

    I run:

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

    Which gives me an error. I try on both Ubuntu 18.04 and the latest Fedora 38. Which Linux distribution are you using? I don’t have /dev/sr0 by default.

    Regards,

    Reply
    • @Any,

      If you don’t have a /dev/sr0 device, it’s likely that the system isn’t recognizing your DVD drive properly.

      Run the following command to check if your system detects the DVD drive:

      $ sudo lshw -C disk
      

      Look for your DVD drive in the list. It might be listed as something like /dev/sr0 or /dev/cdrom.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.