I deleted my /dev/null. How can I restore it?

  • 6
    I 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 Dec 24 '11 at 12:15
  • 1
    I managed to remove /dev/null by getting my pipe wrong flipping $ and & around when running an rm command in a script. Ouch! – flickerfly Oct 9 '15 at 22:28
  • @flickerfly You develop scripts as root? – Thorbjørn Ravn Andersen Jan 14 at 14:02
  • 2
    If the permissions are required, you have to test it eventually. Didn't say it was in production. – flickerfly Jan 14 at 15:27
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.

| improve this answer | |
  • 5
    For completeness, I'd note that this applies to all linux-based systems; other systems may have different numbers. – Random832 Dec 21 '11 at 14:53
  • 2
    Also this only sets up the device file itself, it will not restore its permissions. You'll have to chmod go+w it manually. – Attila O. Jul 3 '15 at 9:44
  • 7
    The number of the beast! – wefwefa3 Nov 6 '15 at 16:21
  • 8
    mknod -m 0666 /dev/null c 1 3 - same thing but shorter – fnkr Oct 23 '17 at 15:06
  • 1
    mknod -m 0666 /dev/null c 1 3 worked perfectly to restore /dev/null on CentOS 7, just to let other know. – Bozzy Jan 24 '19 at 10:13

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
| improve this answer | |
  • Is this answer still current? I'm on CentOS 7 and I've just overwritten my /dev/null by mistake, the udevadm trigger --sysname-match=null trick didn't work for me, while mknod -m 0666 /dev/null c 1 3 worked perfectly. – Bozzy Jan 24 '19 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' Jan 24 '19 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 Jan 25 '19 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.