Intro To 'mknod' Command In Linux
2023-05-31 - By Robert Elder
I use the 'mknod' command to create special block files, character files, and pipe files:
mknod -m 666 my_special_file c 1 8
Creating An Equivalent To '/dev/zero' Using 'mknod'
The file located at '/dev/zero' is a special file that always returns an infinite number of zeros:
head -c 100 /dev/zero | xxd
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: 0000 0000 ....
I can use the 'mknod' command to create my own version of '/dev/zero' directly on the desktop:
sudo mknod -m 666 my_custom_dev_zero c 1 5
The '-m' sets the permissions for the file and the letter 'c' indicates that this will be a character device. The number '1' indicates the major number of this device and the number '5' indicates the minor number. You can also view the corresponding system file related to this character device at this location:
cat /sys/dev/char/1:5/uevent
MAJOR=1
MINOR=5
DEVNAME=zero
DEVMODE=0666
Now, I can use 'my_custom_dev_zero' just like the real '/dev/zero':
head -c 10 my_custom_dev_zero | xxd
00000000: 0000 0000 0000 0000 0000 ..........
Creating Other Special Files Using 'mknod'
As you might expect, you can also use the 'mknod' command to create other special device nodes, like '/dev/random':
sudo mknod -m 666 my_dev_random c 1 8
or '/dev/urandom':
sudo mknod -m 666 my_dev_urandom c 1 9
or '/dev/full':
sudo mknod -m 666 my_dev_full c 1 7
A full listing of the major and minor numbers for these special files can be found in the kernel documentation here.
Creating A Block Device File Using 'mknod'
I can also use the 'mknod' command to create a special file that represents a block device like a storage drive. For example, here's the block device file representing a solid state drive in my system:
ls -l /dev/sdb
brw-rw---- 1 root disk 8, 16 May 21 23:42 /dev/sdb
If I use the 'b' character and the major and minor number of an existing block device, I'll get a second block device node that represents the same drive:
sudo mknod -m 666 my_custom_block_device b 8 16
And here is a file listing of both the original and the new block devices representing this drive:
ls -l my_custom_block_device /dev/sdb
brw-rw---- 1 root disk 8, 16 May 21 23:42 /dev/sdb
brw-rw-rw- 1 root root 8, 16 May 28 18:04 my_custom_block_device
It will now be possible to view system information about the new block device node with the 'fdisk' command:
sudo fdisk -l my_custom_block_device
Disk my_custom_block_device: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: RTL9210 NVME
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0x57607dd4
Device Boot Start End Sectors Size Id Type
my_custom_block_device1 65535 3907029167 3906963633 1.8T 83 Linux
This works just like with the original '/dev/sdb' block device:
sudo fdisk -l /dev/sdb
Disk /dev/sdb: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: RTL9210 NVME
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0x57607dd4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 65535 3907029167 3906963633 1.8T 83 Linux
Creating A Named Pipe File Using 'mknod'
The 'mknod' command can also create named pipes just like the 'mkfifo' command does:
mknod other-hexdump-fifo1 p
mkfifo other-hexdump-fifo2
ls -l other-hexdump-fifo1 other-hexdump-fifo2
prw-rw-r-- 1 robert robert 0 May 28 18:19 other-hexdump-fifo1
prw-rw-r-- 1 robert robert 0 May 28 18:19 other-hexdump-fifo2
And that's why the 'mknod' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|