mknod - Linux


Overview

The mknod command in Linux is used to create filesystem nodes which can be either block, character devices, or named pipes (FIFOs). This command is particularly useful in system administration, driver development, and when setting up device nodes manually in a UNIX-like OS. Its utilization is crucial in environments where manual configuration of device files is necessary, typically under the /dev directory.

Syntax

The syntax for the mknod command is as follows:

mknod [OPTION]... NAME TYPE [MAJOR MINOR]
  • NAME: Specifies the name of the file to be created.
  • TYPE: Node type, which can be b for block device, c or u for character devices, and p for FIFO.
  • MAJOR and MINOR: These are numbers indicating the major and minor device numbers, respectively, for block and character devices.

Options/Flags

  • -m, --mode=MODE: Set file permission bits to MODE. MODE is not a simple permission number but can include all rights and attributes adjustments.
  • -Z, --context=CTX: Set the SELinux security context of the file to CTX. Only useful on systems with SELinux enabled.
  • --help: Display help information and exit.
  • --version: Display version information and exit.

Examples

  1. Create a block device:

    mknod /dev/sda b 8 0
    

    This command creates a block device named sda with major number 8 and minor number 0.

  2. Create a character device:

    mknod /dev/null c 1 3
    

    This example creates a character device named null which typically absorbs or discards all data written to it.

  3. Create a named pipe (FIFO):

    mknod /tmp/myfifo p
    

    This creates a FIFO named myfifo in the /tmp directory.

Common Issues

  • Permission Denied: If you encounter a “Permission Denied” error, ensure you are running mknod with root privileges.
  • Invalid Arguments: Be sure to specify the correct major and minor device numbers; incorrect numbers may lead to non-functional device nodes.

Integration

mknod can be used with other commands to handle device files. For instance, combining mknod with dd to write directly to a block device:

mknod /dev/myblock b 7 0
dd if=/my/file of=/dev/myblock

This sequence creates a block device and then uses dd to write file data directly to this block device.

  • lsblk: List information about block devices.
  • dd: Convert and copy a file, often used for writing data to devices.
  • mount: Mount a filesystem, often necessary after creating new device nodes.

For further information, consult the official Linux man pages: mknod(1).