One config line, written as root, from a script. The reflex every Linux user has when /etc says no is to put sudo in front:
$ sudo echo "max_conn=200" > /etc/demo.conf pd.sh: line 2: /etc/demo.conf: Permission denied exit=1 $ ls -l /etc/demo.conf ls: cannot access '/etc/demo.conf': No such file or directory No password prompt. No file. The error does not come from sudo or from echo. Look at who signed it: pd.sh: line 2, the script itself. Then the next line, run the same way but piped:
$ echo "max_conn=200" | sudo -n tee /etc/demo.conf sudo: a password is required exit=1 That one reached sudo, which asked for a password it was not allowed to prompt for. The first one never got that far. sudo did not fail. It never started.
I have typed the first form more times than I want to count, and for years my fix was to type it again, slower, as if sudo had not heard me.
The redirect belongs to your shell, not to sudo
> /etc/demo.conf is not an argument to sudo. Before bash runs any command, it sets up that command's redirections, as you, in your own process. Creating a file is a write to the directory that will hold it, and /etc is drwxr-xr-x root:root. Your shell cannot open the file, so it prints Permission denied and skips the command entirely. sudo echo is elevated; the > never was.
sudo tee works because tee is the program opening the file, and tee is the one running as root. sudo sh -c 'echo ... > /etc/demo.conf' works for the same reason: the redirect moves inside the elevated process.
That is the general shape of this error. The message is the same for at least five different checks, and only the check matters.
Exit 126 is execution. Exit 1 is everything else
The exit code splits the problem in half before you read anything else. A script that could not be executed returns 126. A read, write or create that was refused returns 1 from whatever command tried. The create above was a 1. So is this one:
$ touch ro.txt; chmod 444 ro.txt; echo x >> ro.txt pd.sh: line 1: ro.txt: Permission denied exit=1 $ rm -f ro.txt; ls ro.txt ls: cannot access 'ro.txt': No such file or directory exit=2 Read-only file, and rm deleted it without a word. Deleting changes the directory, not the file, so the file's own mode was never consulted. The same rule, in reverse, is why /etc/demo.conf could not be created: the file did not exist yet, so the only permission in play was the directory's.
When chmod +x is already done
The 126 that sends people to Stack Overflow is the one where the execute bit is plainly set:
$ ls -l locked/backup.sh | cut -c1-10 -rwxrwxr-x $ ls -ld locked | cut -c1-10 drw------- $ ./locked/backup.sh pd.sh: line 12: ./locked/backup.sh: Permission denied exit=126 $ namei -m locked/backup.sh f: locked/backup.sh drw------- locked backup.sh - Permission denied The x bit on a directory means "may pass through". Without it nothing inside is reachable, whatever the file's mode says, and the error names the file, not the directory that blocked it. namei -m prints the mode of every component down the path, which finds the blocking directory in a ten-level path in one line. chmod u+x locked, and the same command printed backup ran and exited 0.
The other two causes chmod cannot touch are a filesystem mounted noexec, which beats every execute bit including root's, and a file somebody else owns, which only they or root can chmod.
One script that names the check
The page has why-permission-denied.sh, which takes a path and what you were trying to do (exec, read or write), walks every parent directory, reads the mount options, checks the shebang interpreter, ACLs and the immutable flag, and says which one is in the way. Run here against the two cases above that are not about execute bits (my user name shown as travis):
$ ./why-permission-denied.sh /etc/demo.conf write ✗ cannot create files in /etc: drwxr-xr-x root:root exit=1 $ ./why-permission-denied.sh /etc/shadow read /etc/shadow: -rw-r----- owner=root group=shadow; you are travis, not in group shadow ✗ no read permission for you: the owner (root) has to grant it, or use sudo if you are meant to have access exit=1 The first one is the answer I needed before I typed sudo the second time: the directory, not the file, and not something sudo in front of echo could ever reach. Against locked/backup.sh it printed the directory with no search permission and its mode. It exits 1 whenever something blocks you, so a deploy step can call it before trying and fail with a reason instead of a 126.
Why not sudo everything
Because sudo on your own scripts trades one problem for two. Files the script writes become owned by root, and the next run without sudo fails on those with the same message, in a new place. Fix the mode or the ownership once, chmod +x or sudo chown you: file, and run the script as yourself. And when the thing being denied is a redirect, sudo in front of the command is the one fix that cannot work.
All five causes, the exit-126-vs-1 split, and the full why-permission-denied.sh: https://bashsnippets.xyz/snippets/bash-permission-denied
To build the right chmod line instead of guessing at it, the chmod Permissions Builder converts 755 and rwxr-xr-x both ways, and File Permissions Security audits a tree for modes that are too loose rather than too tight. The rest of the library is at https://bashsnippets.xyz
I bundled the scripts I copy onto every new server, plus 10 bonus ones, into the Production Bash Toolkit. 30-day refund if it isn't useful.
