I deleted my /dev/null. How can I restore it?
-
6I once found that /dev/null was replaced with a normal file which only root could write to. I thought for a moment that I would have to reinstall everything but as mentioned below, the recovery is trival if you know the trick.Stuart Woodward– Stuart Woodward2011-12-24 12:15:07 +00:00Commented Dec 24, 2011 at 12:15
-
1I managed to remove /dev/null by getting my pipe wrong flipping $ and & around when running an rm command in a script. Ouch!flickerfly– flickerfly2015-10-09 22:28:45 +00:00Commented Oct 9, 2015 at 22:28
-
@flickerfly You develop scripts as root?Thorbjørn Ravn Andersen– Thorbjørn Ravn Andersen2020-01-14 14:02:38 +00:00Commented Jan 14, 2020 at 14:02
-
3If the permissions are required, you have to test it eventually. Didn't say it was in production.flickerfly– flickerfly2020-01-14 15:27:15 +00:00Commented Jan 14, 2020 at 15:27
3 Answers
mknod /dev/null c 1 3
chmod 666 /dev/null
Use these command to create /dev/null or use null(4) manpage for further help.
-
5For completeness, I'd note that this applies to all linux-based systems; other systems may have different numbers.Random832– Random8322011-12-21 14:53:20 +00:00Commented Dec 21, 2011 at 14:53
-
14
mknod -m 0666 /dev/null c 1 3- same thing but shorterfnkr– fnkr2017-10-23 15:06:10 +00:00Commented Oct 23, 2017 at 15:06
Under many traditional unices, you can recreate devices with their default permissions with the MAKEDEV script. This script is traditionally in /dev but is in /sbin on Ubuntu. Pass it an argument that indicates what devices you want to create; on Ubuntu that's std (you can write MAKEDEV null as well, that creates null as well as a number of other devices).
Under modern Linux systems running udev, you can tell udev to recreate all or some devices based on available drivers and connected hardware, through udevadm trigger:
udevadm trigger --sysname-match=null
-
1Is this answer still current? I'm on CentOS 7 and I've just overwritten my /dev/null by mistake, the
udevadm trigger --sysname-match=nulltrick didn't work for me, whilemknod -m 0666 /dev/null c 1 3worked perfectly.Bozzy– Bozzy2019-01-24 10:11:33 +00:00Commented Jan 24, 2019 at 10:11 -
@Bozzy I know it worked with the pre-systemd udev. I haven't tried recently, but as far as I know this should still work. If you add
--verbose, does it list/sys/devices/virtual/mem/null?Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2019-01-24 22:49:34 +00:00Commented Jan 24, 2019 at 22:49 -
Yes, it lists that. I've tried to overwrite again /dev/null on my dev machine, but strangely enough I'm unable to do it here, so I can't do a more in-depth analysis now...Bozzy– Bozzy2019-01-25 10:18:45 +00:00Commented Jan 25, 2019 at 10:18
When I accidentally deleted /dev/null, a simple sudo rebootseemed to restore it.
It did cause my desktop icons to go crazy, but no damage seemed to be done
If that doesn't the other answers probably will be more help
-
Please no. Habitual rebooting is a bad habit; some day it'll bite you.Martin Kealey– Martin Kealey2025-04-19 03:09:42 +00:00Commented Apr 19 at 3:09