How to Fix ‘bash: /dev/null: Permission denied’ in Linux

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/null when /dev/null did 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- or 0666).
  • 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 /dev filesystem being mounted with the nodev mount 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.

  1. Open your Terminal shell.
  2. Execute a long listing on /dev/null:
    ls -l /dev/null
  3. 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.

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:

  1. Assign full read and write permissions (mode 666) to all users:
    sudo chmod 666 /dev/null
  2. Ensure ownership belongs to root:
    sudo chown root:root /dev/null
  3. Test writing to it as a regular unprivileged user:
    echo "testing" > /dev/null
  4. 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.

  1. Remove the corrupted regular file:
    sudo rm -f /dev/null
  2. Create a new character device node with major number 1 and minor number 3:
    sudo mknod -m 666 /dev/null c 1 3
  3. Confirm the newly generated node attributes:
    ls -l /dev/null
  4. 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.

  1. Check the mount flags of your /dev directory:
    mount | grep "/dev "
  2. If you observe the flag nodev assigned to the /dev root mount, edit /etc/fstab:
    sudo nano /etc/fstab
  3. Remove nodev from the options column for /dev (or devtmpfs).
  4. 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.

  1. Reload all udev rule configurations:
    sudo udevadm control --reload-rules
    sudo udevadm trigger
  2. Restart the udev daemon:
    sudo systemctl restart systemd-udevd
  3. This ensures the kernel automatically provisions standard character device modes on every subsequent boot.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.