sudo in front, Permission denied anyway

sudo in front, Permission denied anyway

●2 ●42 ●105
calendar_today • schedule1 min read

The reflex when /etc says no is to put sudo in front. From a script:

$ sudo echo "max_conn=200" > /etc/demo.conf
pd.sh: line 2: /etc/demo.conf: Permission denied
exit=1
$ echo "max_conn=200" | sudo -n tee /etc/demo.conf
sudo: a password is required
exit=1

The first line never asked for a password, and the error is signed by the script, not by sudo. The redirect > belongs to your shell. Bash opens the file as you before it runs any command, cannot create a file in a root:root drwxr-xr-x directory, and skips the command. sudo never started. The second line did reach sudo, because there tee is the program opening the file.

That is the shape of this error in general: one message, at least five different checks. Exit 126 means a script could not be executed; exit 1 means a read, write or create was refused. A rwx script inside a directory with no x bit is still a 126, and namei -m shows which directory blocks it. A read-only file in a writable directory can still be deleted.

All five causes, and a script that names the check blocking you, are in Fix Bash "Permission denied".


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.

Want more #devops & #linux deep dives?

Join 6,176+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

The cleanup failed on the night there was nothing to clean

BashSnippets - Dec 2

My hung process had no file open. It was stuck inside open().

BashSnippets - Dec 1

A deploy guard that passes with no token

BashSnippets - Nov 24

Who owns that port? One ss flag answers it without sudo

BashSnippets - Sep 16

One file more and rm deleted none of them

BashSnippets - Nov 18
chevron_left
4k Points • 149 Badges
North America • bashsnippets.xyz
79Posts
44Comments
14Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

Commenters (This Week)

9 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!