2

So the mknod command, what does that do?

/dev/random$ mknod -m 666 random c 1 8

I understand that -m specifies permissions, and that mknod overall create an inode in the filesystem which can be a file, directory, device, fifo, etc...but I really don't understand what the last 3 arguments there do.

leeand00
  • 4,615

1 Answers1

3

The last three arguments are type, major, minor. They determine what a device actually represents; the name is convention, but does not determine functionality. If you create it as /dev/wubbalubbadubdub it'd still be /dev/random as long as type, major, minor says it is so.

Type c is a character device (as opposed to a block device).

The major / minor numbers are, in essence, magic numbers. There's no way to make sense of them other than refer to relevant kernel documentation.

https://www.kernel.org/doc/Documentation/admin-guide/devices.txt

So in your case you have character device, major 1, minor 8 which is documented as such:

   1 char   Memory devices
   ⇑MAJOR ⇓MINOR = /dev/name
          1 = /dev/mem      Physical memory access
          2 = /dev/kmem     Kernel virtual memory access
          3 = /dev/null     Null device
          4 = /dev/port     I/O port access
          5 = /dev/zero     Null byte source
          6 = /dev/core     OBSOLETE - replaced by /proc/kcore
          7 = /dev/full     Returns ENOSPC on write
          8 = /dev/random   Nondeterministic random number gen.
          9 = /dev/urandom  Faster, less secure random number gen.
         10 = /dev/aio      Asynchronous I/O notification interface
         11 = /dev/kmsg     Writes to this come out as printk's, reads
                    export the buffered printk records.
         12 = /dev/oldmem   OBSOLETE - replaced by /proc/vmcore

That's just all there is to it. If you want /dev/random you have to use c 1 8. If you want it to be /dev/urandom, it's c 1 9. That's how it's historically enumerated in kernel.

Most of the time you just use the device nodes as provided by kernel, devtmpfs, udev, or your distro's provided static /dev structure; you rarely have to create them yourself and even then there might be helpers for that so you don't have to figure out the correct magic numbers by yourself.

You can also use mknod to create FIFOs but mkfifo should be easier to use for those.

frostschutz
  • 48,978
  • 1
    Please ignore the descriptions of random and urandom given there. Linux has a bit of a tradition when it comes to bad doco for these devices. (-: https://unix.stackexchange.com/q/324209/5132 – JdeBP Jan 16 '20 at 00:41