-bash: /dev/null: Permission denied

I’m trying to create a new user on a Centos 6 system.

First, I do

useradd kevin

Then, I tried to run commands as that user

su - kevin

However, I get the following error messages

-bash: /dev/null: Permission denied
-bash: /dev/null: Permission denied
-bash: /dev/null: Permission denied
-bash: /dev/null: Permission denied
-bash: /dev/null: Permission denied
-bash: /dev/null: Permission denied
[kevin@gazelle ~]$

And I can’t do very much as that user.

The permissions on /dev/null are as follows:

-rwxr-xr-x  1 root root           9 Jul 25 17:07 null

Roughly the same as they are on my Mac,

crw-rw-rw-   1 root   wheel         3,   2 Jul 25 14:08 null

It’s possible, but really unlikely, that I touched dev.

As the root user, I tried adding kevin to the root group:

usermod -a -G root kevin

However I still am getting /dev/null permission denied errors.

Why can’t the new user write to /dev/null?
What groups should the new user be a part of?
Am I not impersonating the user correctly?
Is there a beginners guide to setting up users/permissions on Linux?

Asked By: Kevin Burke

||

This should fix the issue (as root):

rm /dev/null
mknod /dev/null c 1 3
chmod 666 /dev/null

What these commands are doing:

  • rmis removing the bogus file that has been created because the expected one was missing;
  • mknod is creating a character device named /dev/null with the appropriate major and minor numbers for a Linux kernel;
  • chmod is setting the permissions for all users to be able to read and write to /dev/null.
Answered By: jlliagre

Someone evidently moved a regular file to /dev/null. Rebooting will recreate it, or do

rm -f /dev/null; mknod -m 666 /dev/null c 1 3

As @Flow has noted in a comment, you must be root to do this.

1 and 3 here are the device major and minor number on Linux-based OSes (the 3rd device handled by the mem driver, see /proc/devices, cat /sys/devices/virtual/mem/null/dev, readlink /sys/dev/char/1:3). It varies with the OS. For instance, it’s 2, 2 on OpenBSD and AIX, it may also not be always the same on a given OS. Some OSes may supply a makedev / MAKEDEV command to help recreate them.

Answered By: Mark Plotnick

The solution suggested by Mark did not work on OpenBSD.
However

mknod -m 666 /dev/null c 2 2

did the trick. I have tested this on OpenBSD 5.6. When the accepted answer is executed /dev/null will block and screw any code reading from it pretty badly.

To re-create all standard devices on OpenBSD (null included), you should use (as root):

cd /dev
./MAKEDEV std
Answered By: meschi

Posting the Mac OS X answer for posterity…

sudo sh -c '
  rm -rf /dev/null &&
    mknod /dev/null c 3 2 &&
    chmod 666 /dev/null'
Answered By: Julian

This happened to me on windows within the Ubuntu application, while trying to run a script that wrote into /dev/null. Permissions were correct for both /dev and /dev/null.

Turned out the problem was windows newlines in the script file. Running :

dos2unix.exe c:pathtoscript.sh

Solved the issue for me.

Answered By: Arthur.V

For IBM AIX Server i did the same and it’s working

# rm /dev/null;mknod /dev/null c 2 2;chown root:system /dev/null;chmod 0666 /dev/null

# ls -l /dev/null
crw-rw-rw-    1 root     system        2,  2 Jul 10 22:18 /dev/null
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.