Master the Linux ‘mknod’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 23, 2023

--

This article provides a comprehensive guide to the Linux mknod command, including its history, usage, parameters, and examples. It's intended for those who are interested in understanding and using the mknod command effectively.

Instructions

This article provides an in-depth examination of the Linux mknod command. It covers the basic usage, various parameters, common use cases through examples, and advanced techniques related to mknod.

History

The mknod command, which stands for "make node," is a traditionally Unix command that was included in the early versions of Unix and has remained as part of the POSIX standard.

When and why to use it

The mknod command is used to create device files. This is necessary for certain system configurations and maintenance tasks. Understanding mknod is beneficial when dealing with hardware devices and managing system files.

How to use it

The general syntax of the mknod command is mknod [OPTION]... NAME TYPE [MAJOR MINOR].

$ mknod mydevice c 1 7

In the example above, a character device named “mydevice” is created with major number 1 and minor number 7.

The commonly used parameters

  • -m, --mode=MODE - set file permission bits to MODE, not a=rw - umask
$ mknod -m 644 mydevice c 1 7
  • -Z, --context=CTX - set the SELinux security context of the new file to CTX
$ mknod -Z system_u:object_r:device_t:s0 mydevice c 1 7

Other supported parameters

  • --help display this help and exit
  • --version output version information and exit

Most common use cases

One common use case of mknod is to create device files that represent hardware devices or software that behaves like a hardware device.

$ mknod /dev/mydevice c 180 31

The tricky skills

mknod is typically used by system administrators for device management. It can be used in scripts for automating device creation and management.

$ for i in $(seq 0 3); do mknod /dev/mydevice$i c 180 $i; done

What needs to be noted

Understanding the correct usage of mknod requires knowledge of the Linux device management subsystem. Inappropriate use of mknod can lead to system instability or security issues.

Conclusion

The mknod command is a powerful tool for system administrators to manage devices on a Linux system. While not commonly used in everyday tasks, having a good understanding of mknod can be beneficial for those who work closely with hardware or system files.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet