mknod to your rescue to restore /dev/null

Recently one of my friend unknowingly deleted /dev/null file and it was throwing error when he tried to launch the terminal console. So, what exactly is the file /dev/null ?

/dev/null is a character special file, which means that it is a contentless file that is marked as being able to source and sink byte streams to it. What happens to those streams is determined by their device numbers. Character special files are like most other files in many respects, and can be deleted if you have the permissions. But, in order to re-create this file, you should know the major and minor number associated with this character file.

[root@ldapserver /]# ll /dev/null
crw-rw-rw-. 1 root root 1, 3 Dec 18 2011 /dev/null

In my server, I have major-minor numbers for /dev/null as 1,3. But, what if somebody has already deleted this file, and you want to know the major and minor numbers for /dev/null so you can recreate this file.

If you have installed linux from source, then you should have devices.txt file on the following path

/usr/src/linux-3.1.6/Documentation/devices.txt

Open this file ‘devices.txt’ in any text editor and search for /dev/null. You will reach the following section:

  1 char        Memory devices
                  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

From the above, you can see the 1 and 3 highlighted in yellow. ‘1’ is the major number and ‘3’ is the minor number. The major number specifies the major device type (for example, the device driver number). The minor number specifies the device location, which is typically, but not always, the unit, drive, and/or line number.

Now that you know the major and minor numbers associated with /dev/null, you can re-create this file as follows:

mknod -m 666 /dev/null c 1 3

mknod is used to create the character and block devices that populates ‘/dev/’. So, the above command is used to create a character (c) device file with major and minor pair of 1,3 and world writable.

Enjoy creating character and block device files. 🙂

Advertisements