How to use the command mknod (with examples)
- Linux
- December 25, 2023
The mknod
command is used to create block or character special files in Unix-like operating systems. These special files can represent devices such as hardware devices or virtual devices.
Use case 1: Creating a block device
Code:
sudo mknod path/to/device_file b major_device_number minor_device_number
Motivation: Creating a block device is useful when you want to simulate a storage device, such as a hard drive or a USB drive. This can be helpful for testing or virtualization purposes.
Explanation:
sudo
: This command is used to run themknod
command with root privileges.mknod
: Themknod
command to create a device file.path/to/device_file
: The full path and name of the device file you want to create.b
: Indicates that we want to create a block device.major_device_number
: The major device number for the device file.minor_device_number
: The minor device number for the device file.
Example output:
sudo mknod /dev/sdb b 8 16
This will create a block device file named /dev/sdb
with the major device number 8 and minor device number 16.
Use case 2: Creating a character device
Code:
sudo mknod path/to/device_file c major_device_number minor_device_number
Motivation: Creating a character device is useful when you want to represent a device that transfers data character by character, such as a keyboard or a printer.
Explanation:
sudo
: This command is used to run themknod
command with root privileges.mknod
: Themknod
command to create a device file.path/to/device_file
: The full path and name of the device file you want to create.c
: Indicates that we want to create a character device.major_device_number
: The major device number for the device file.minor_device_number
: The minor device number for the device file.
Example output:
sudo mknod /dev/hello c 7 3
This will create a character device file named /dev/hello
with the major device number 7 and minor device number 3.
Use case 3: Creating a FIFO (queue) device
Code:
sudo mknod path/to/device_file p
Motivation: Creating a FIFO device is useful for inter-process communication, where one process can write data to the FIFO and another process can read the data from the FIFO.
Explanation:
sudo
: This command is used to run themknod
command with root privileges.mknod
: Themknod
command to create a device file.path/to/device_file
: The full path and name of the FIFO device file you want to create.p
: Indicates that we want to create a FIFO device.
Example output:
sudo mknod /dev/myfifo p
This will create a FIFO device file named /dev/myfifo
.
Use case 4: Creating a device file with default SELinux security context
Code:
sudo mknod -Z path/to/device_file type major_device_number minor_device_number
Motivation: Creating a device file with a specific SELinux security context can be necessary when working with SELinux-enabled systems, ensuring that the device file has the correct security context.
Explanation:
sudo
: This command is used to run themknod
command with root privileges.mknod
: Themknod
command to create a device file.-Z
: Specifies that we want to set the SELinux security context for the created device file.path/to/device_file
: The full path and name of the device file you want to create.type
: Specifies the type of device file.major_device_number
: The major device number for the device file.minor_device_number
: The minor device number for the device file.
Example output:
sudo mknod -Z /dev/sdcard b 1 0
This will create a block device file named /dev/sdcard
with the major device number 1, minor device number 0, and with the default SELinux security context.