mknod command in linux 1

How to Use mknod Command in Linux

With the mknod command in Linux, you can create device files that function differently from the usual files. The mknod system call helps users create a filesystem node or special device files with mode and dev specified attributes.

The term mknod, which stands for make node, is derived from the word inode, which refers to index node. When we talk about the inode, we’re looking at the fundamental unit of the metadata for file system entities in any UNIX-based system.

Although inodes in most cases describe plain disk files, the same in UNIX depict things more like network sockets. The mknod command in Linux helps build and make a directory entry and the related inode for any special file.

As already mentioned, the mknod is a command-line utility that helps create filesystem nodes and named pathname, with mode and dev set attributes. The mode argument in mknod helps specify permission to create a node alongside defining the type.

The node you create is governed by the user ID that is in effect during the process. However, if the node’s directory has the preset group-ID bit, the newly created node inherits the group ownership from the root directory.

In this article, we’ll make you learn different ways to employ the mknod command, but before that, let us have a quick look at the command’s syntax.

mknod Command in Linux: Syntax

The basic structure of mknod command looks something like this:

$ mknod [Pathname] [OPTIONS]

Using mknod Command in Linux

Having discussed the syntax and other aspects of the mknod command in Linux, let us now learn how it works.

Indicating the Special File as Block Oriented Device

The mknod command in Linux can be used to indicate the special files. These usually include disk, tape, or diskette. For that, all you need to do is pass the b flag. Running the mknod command with the b flag will display the block-oriented device files. 

Indicating the Special File as Character Oriented Device

In case you want to display the special file as a character-oriented device using the mknod command, pass the c flag.

Creating FIFOs

The based usage of the mknod command in Linux is to create named pipes or FIFOs. To build a FIFO, you just need to use the p flag alongside the mknod command.

How does the mknod Command in Linux Works?

To help you understand the working of the mknod command in Linux, here are a few examples to look at. Walking through the following set of examples will help you figure out the best ways to invoke mknod on your Terminal.

Creating a New Diskette Drive

Suppose you want to create a new diskette drive, /dev2/fd3. In that case, launch the Terminal and run the following command:

$ mknod /dev2/fd3 b 1 2
creating new diskette drive

Running the command mentioned above will create a special file /dev2/fd3. It will be a special block file with 1 being the major device number and 2 being the minor.

Creating FIFO file

Creating a FIFO file with the mknod command in Linux is super simple. Launch the Terminal using the Ctrl+Alt+T key combination and run the command as:

$ mknod [fifo filename] p
create FIFO using mknod command in linux

Creating the Special File for New Character Drive

For instance, if you’re willing to create a special file /devnew/fdnew for the character drive, run the command like this:

$ mknod /devnew/fdnew c 2 1
creating special files in linux

Here, 2 and 1 represent the major device number and the minor device number, respectively.

mknod Command: Return Value

The mknod command in Linux returns 0 on being executed successfully. In other words, 0 as return value indicates the success status of mknod command, while it displays -1 in case an error has occurred. Talking about errors in mknod, there are several tags with varied meanings altogether.

EACCES: You’ll get this error-tag when the provided parent directory does not allow the write permission to the involved process. 

EEXIST: It indicates that the pathname already exists.

EFAULT: It indicates that the pathname provided points outside the accessible address space in your system.

EINVAL:  The requested mode creation of something intended other than any normal file, device, special file(s), FIFO, or the socket.

ELOO: The system shows ELOO when too many symbolic links are encountered while resolving pathname.

ENAMETOOLONG: It indicates that the provided pathname was too long.

ENOENT: In mknod, it represents that a directory component in the provided pathname does not exist, or else it is a dangling symbolic link.

ENOMEM: It indicates that an insufficient kernel memory was available during the process.

ENOSPC: The device that contains the pathname has no room for any new node.

ENOTDIR: When this tag gets displayed, you need to understand that a component that is used as a directory in the pathname is, in fact, not a directory.

EPERM: The mode requested the yield of something other than a standard file, or FIFO (the named pipe), or the Unix domain socket, and at the same time, the caller is not privileged. 

The error message is also displayed if the filesystem that consists of the pathname doesn’t support the type of node you’ve requested. 

(Keep in mind: Linux doesn’t have the CAP_MKNOD ability)

EROFS: The provided pathname refers to the file that sits on a read-only filesystem.

mknod Command Arguments

-m, --mode: It sets the file permission bites to MODE

-Z: You can set the SELinux security context to the default type by using the Z argument

Other mknod Command Options

--help flag: It displays the help-related information about the mknod command.

Input:

$ mknod --help

Output:

mknod command help

--version flag: Passing this flag will display the mknod command version information.

Input

$ mknod --version

Output:

mknod command version check

This is pretty much everything about the mknod command in Linux. In this article, we tried our best to familiarize you with the command and make you learn the best possible ways of using it. We employed easily understandable examples so that you can have no issues whatsoever.

If this guide helped you, please share it.

Leave a Reply
Related Posts