Logging into a Linux server or executing shell scripts only to see dozens of terminal errors reporting: bash: /dev/null: Permission denied breaks automated cron jobs, package managers (such as apt or dnf), and standard commands that discard standard output or error streams (e.g. command > /dev/null 2>&1). In Unix-like operating systems, /dev/null is a special virtual character device file that discards all data written to it and returns end-of-file (EOF) on read. When an administrator or misconfigured script accidentally deletes /dev/null and recreates it as a regular file with restricted permissions (e.g. 0644 owned by root), non-root users and system daemons are completely blocked from writing to it.
Why Does Linux Return /dev/null: Permission denied?
Every process in Linux expects /dev/null to be a world-writable character device. The error occurs when:
- Recreated as Regular File: A script running as root executing
sudo command > /dev/nullwhen/dev/nulldid not exist, creating a standard flat text file owned by root with-rw-r--r--permissions. - Incorrect File Mode Bits: The special file lacking the required universal read/write permissions (
crw-rw-rw-or0666). - Missing Major and Minor Device Numbers: The inode losing its character device attributes (Major 1, Minor 3) after improper chroot jail creation or backup restoration.
- Mounted with No-Device (nodev) Flags: The
/devfilesystem being mounted with thenodevmount option, preventing the kernel from recognizing character nodes.
Step 1: Check the File Attributes of /dev/null
Verify whether /dev/null is a special character device or an accidental regular file.
- Open your Terminal shell.
- Execute a long listing on
/dev/null:ls -l /dev/null - Examine the very first character of the output:
- If it begins with a c (e.g.
crw-rw-rw- 1 root root 1, 3), it is properly formatted as a character device. - If it begins with a dash – (e.g.
-rw-r--r-- 1 root root 0), it has been converted into a regular flat file and must be recreated immediately.
- If it begins with a c (e.g.
Step 2: Set Correct Permissions (If Character Device Exists)
If the file is already a character device (starts with c) but lacks universal write access:
- Assign full read and write permissions (mode 666) to all users:
sudo chmod 666 /dev/null - Ensure ownership belongs to root:
sudo chown root:root /dev/null - Test writing to it as a regular unprivileged user:
echo "testing" > /dev/null - If no error message appears, permissions have been successfully restored.
Step 3: Recreate /dev/null Character Device with mknod
If /dev/null was turned into a regular file, delete the file and recreate the authentic character device using mknod.
- Remove the corrupted regular file:
sudo rm -f /dev/null - Create a new character device node with major number 1 and minor number 3:
sudo mknod -m 666 /dev/null c 1 3 - Confirm the newly generated node attributes:
ls -l /dev/null - The output must match:
crw-rw-rw- 1 root root 1, 3 [Date] /dev/null
Step 4: Fix /dev Mount Options in /etc/fstab
Ensure that the device filesystem is not mounted with security flags prohibiting device interpretation.
- Check the mount flags of your
/devdirectory:mount | grep "/dev " - If you observe the flag
nodevassigned to the/devroot mount, edit/etc/fstab:sudo nano /etc/fstab - Remove
nodevfrom the options column for/dev(ordevtmpfs). - Remount the filesystem:
sudo mount -o remount,dev /dev
Step 5: Trigger udev to Rebuild Device Nodes Automatically
If character nodes disappear across system restarts, reload the udev device manager.
- Reload all udev rule configurations:
sudo udevadm control --reload-rules sudo udevadm trigger - Restart the udev daemon:
sudo systemctl restart systemd-udevd - This ensures the kernel automatically provisions standard character device modes on every subsequent boot.