The Black Hole of Linux
Learn about the /dev/null file, the black hole of Linux. We will go over a practical example and how it is used to mask services.
Table of Contents 📖
/dev/null
Linux has a special file called /dev/null that acts as a data sink. Anything written to this file vanishes. Because of this, it is often referred to as the black hole of Linux. To demonstrate, lets write some text to the /dev/null file and watch it disappear.
INFO: The /dev directory contains files representing devices.
echo "This message goes nowhere" > /dev/null
cat /dev/null
A quick practical example could be checking if a command runs successfully while supressing output of the command:
ping -c 1 wittcode.com > /dev/null && echo "Network is up!"
If we remove the output to /dev/null, we can see the output from the command:
ping -c 1 wittcode.com && echo "Network is up!"
PING wittcode.com (31.220.55.159) 56(84) bytes of data.
64 bytes from blog.wittcode.com (31.220.55.159): icmp_seq=1 ttl=64 time=0.029 ms
--- wittcode.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
Network is up!
Masking Services
Masking a service usually involves creating a symbolic link to /dev/null, which effectively prevents the service from running. Masking is useful for disabling or preventing services from starting, either automatically or manually.
systemctl mask cron.service
Created symlink /etc/systemd/system/cron.service → /dev/null.