1. Overview
In the shell scripting world /dev/null is a fundamental concept. Its accidental deletion might wreak havoc on the system because many system processes rely on it. So, knowing what /dev/null is and how to work with it is essential for any Linux developer.
As we'll see in this tutorial, issues caused by a missing /dev/null are temporary and can be easily fixed.
2. What Is /dev/null?
In Linux systems, devices are files stored in the /dev directory. These files can represent both physical and virtual devices. /dev/null is a virtual null device used to discard any output redirected to it.
There are two types of output streams to redirect, standard output (stdout) and standard error (stderr). Each stream has a numerical descriptor, 1 for stdout and 2 for stderr.
To suppress one of these streams, we simply redirect it to /dev/null using the descriptor and > redirect operator:
$ command 1> /dev/null
$ command 2> /dev/null
When we omit the descriptor, it's assumed to be stdout for an output type stream.
In order to suppress both streams in one command, we can write both in one line:
$ command > /dev/null 2> /dev/null
A shorter option is to redirect stdout to /dev/null and then redirect stderr to stdout:
$ command > /dev/null 2> &1
The &1 notation means the destination is a file descriptor, not a file named 1.
Additionally, the above can be written much shorter using &>:
$ command &> /dev/null
In this shorthand, &> is the semantic equivalent for both redirections.
3. Inspecting the Properties of /dev/null
Now, let's try to echo some text and redirect the output to /dev/null. If we try to read it, it'll give EOF:
$ echo "Hello world!" > /dev/null
$ cat /dev/null
Here, we'll get no output. The file is completely empty.
Now, let's see what properties /dev/null has:
$ ls -l /dev/null
crw-rw-rw- 1 root root 1, 3 May 23 23:56 /dev/null
$ stat /dev/null
File: /dev/null
Size: 0 Blocks: 0 IO Block: 4096 character special file
Device: 5h/5d Inode: 5 Links: 1 Device type: 1,3
Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-05-23 23:56:28.815999926 -0700
Modify: 2023-05-23 23:56:28.815999926 -0700
Change: 2023-05-23 23:56:28.815999926 -0700
Birth: -
As we see in the results, the file size is 0 and it belongs to the root user. All categories, including the owner, have only read and write permissions. Also, the device type numbers 1 and 3 are major and minor device categories specific to /dev/null.
Another noteworthy piece of information is the special type. Here, c denotes a character device, also called a character special file. These devices behave like pipes and allow direct unbuffered access to the device. Furthermore, they handle each character individually, hence the name.
4. Experimenting with Deleting /dev/null
First, we need to delete /dev/null to demonstrate recovery. Note, however, this tutorial seeks to help in case it is already deleted. There's absolutely no need to follow along and actually delete /dev/null unless it's a safe experiment in a VM.
Since the file belongs to root, it won't be possible for a random user with no such access to accidentally delete it. Therefore, to delete, we run the rm command as root:
$ sudo rm /dev/null
Anything strange can happen after this step. After all, /dev/null is essential for the system, and having it removed isn't a healthy state.
On the Ubuntu 20.04 VM where this experiment was held, the system froze and the screen went black after trying to open Settings from the right-click menu on the Desktop. On another occasion, after the user was logged out due to inactivity, it was no longer possible to log in. Thus, we have only one choice in such cases: force reboot.
5. Creating /dev/null
Luckily for us, the system recovers /dev/null after reboot. But this isn't an optimal solution, since any unsaved work will be gone. Therefore, immediately after accidental deletion and before anything strange happens, we need to recover /dev/null manually.
For that, we'll use the mknod command to create a character special file. We have to specify the original device types 1 and 3:
$ mknod /dev/null c 1 3
After the file is created, we need to set the correct permissions for all categories:
$ chmod 666 /dev/null
In fact, the same can also be done with just mknod and the --mode, or short -m, option:
$ mknod -m 0666 /dev/null c 1 3
Additionally, it's possible for the processes redirecting to /dev/null to unknowingly recreate it as a regular file. In case that happens, we'll need to create the real one with a different name, such as /dev/null-new, then force move over to replace the fake one:
$ mv -f /dev/null-new /dev/null
A reboot afterward might also be necessary.
6. Conclusion
In this article, we've seen why /dev/null is important and how to create it.
With this information, we are fully equipped to recover the system in case /dev/null is accidentally deleted.
In the worst case, a simple reboot will fix everything.