mknod

system administrationLinux/Unix
The mknod command is one of the most frequently used commands in Linux/Unix-like operating systems. mknod Make block or character special files

Quick Reference

Command Name:

mknod

Category:

system administration

Platform:

Linux/Unix

Basic Usage:

mknod [options] [arguments]

Common Use Cases

    Syntax

    mknod [OPTION]... NAME TYPE [MAJOR MINOR]

    Options

    Option Description
    -m, --mode=MODE Set file permission bits to MODE, not a=rw - umask
    -Z Set SELinux security context of each created file to default type
    --context[=CTX] Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
    --help Display help information and exit
    --version Output version information and exit

    For the TYPE parameter, use one of the following:

    Type Description
    b Create a block (buffered) special file
    c, u Create a character (unbuffered) special file
    p Create a FIFO (named pipe)

    Examples

    How to Use These Examples

    The examples below show common ways to use the mknod command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sudo mknod /dev/null c 1 3
    Create a character special file /dev/null with major number 1 and minor number 3.
    sudo mknod /dev/sda b 8 0
    Create a block special file /dev/sda with major number 8 and minor number 0.
    # Advanced Examples Advanced
    sudo mknod -m 666 /dev/loop0 b 7 0 Create a block special file with specific permissions (rw-rw-rw-). sudo mknod myfifo p Create a FIFO (named pipe) file called myfifo. sudo mknod --context=system_u:object_r:device_t:s0 /dev/custom c 42 0 Create a character special file with a specific SELinux context. sudo mknod -Z /dev/mynull c 1 3 Create a character special file with default SELinux context. mkdir -p /tmp/test && cd /tmp/test && sudo mknod -m 600 device b 8 1 Create a test device node with restricted permissions in a specific directory. for i in {0..7}; do sudo mknod /dev/loop$i b 7 $i; done Create a series of loop device nodes with sequential minor numbers.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'mknod' command is a low-level utility used to create special files or device nodes in Unix-like operating systems. It is primarily used by system administrators and developers to create device files that represent physical or virtual devices in the system. Special files are a unique feature of Unix-like filesystems that allow hardware devices and other special resources to be accessed as if they were regular files. Key features of the mknod command: 1. Device File Creation: mknod can create block or character special device files that represent hardware devices in the /dev directory. Block devices (like hard drives) handle data in blocks, while character devices (like terminals) handle data character by character. 2. FIFO Creation: While mkfifo is more commonly used for this purpose, mknod can also create FIFOs (named pipes) for inter-process communication. 3. Major and Minor Numbers: For device files, mknod requires specifying major and minor numbers that uniquely identify the device driver and specific device instance, respectively. These numbers are defined by the kernel. 4. Permission Control: The command allows setting specific permissions on the special files, controlling which users can access the device. 5. SELinux Integration: On systems with SELinux enabled, mknod supports setting appropriate security contexts for created device files. 6. Root Privileges: Creating device nodes typically requires root privileges because of the security implications of device access. 7. Manual Device Management: While modern Linux systems typically use udev for dynamic device management, mknod remains useful for manual device creation or in specialized environments. Common use cases for mknod include: - Creating custom device nodes for specific hardware - Recovering missing device files after system issues - Setting up virtual devices for testing and development - Creating special file types in chroot environments or containers - Building embedded systems where manual device creation is necessary - Implementing retrocompatibility with older applications expecting specific device files It's important to note that incorrect use of mknod can lead to system instability or security issues, as device files provide direct access to hardware and kernel functionality. Modern Linux distributions generally manage device files automatically through the udev system, which creates and removes device nodes dynamically as hardware is connected or disconnected. While the direct use of mknod has become less common with the adoption of dynamic device management, understanding this command remains valuable for system administrators, particularly when troubleshooting device issues or working with specialized system configurations.

    Related Commands

    These commands are frequently used alongside mknod or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mknod command works in different scenarios.

    $ mknod
    View All Commands