You need one line in /etc/hosts, you know it needs root, so you add sudo:
$ sudo echo "10.0.0.5 db.internal" >> /etc/hosts
bash: /etc/hosts: Permission denied
Look at who complains: bash, not sudo or echo. That's the whole story. Everything below was run as a normal user with sudo rights on a BrokenCluster lab machine (Ubuntu, sudo 1.9.15).
What actually happens
When you press Enter, your shell reads the line and sees two separate things:
- a command to run:
sudo echo "10.0.0.5 db.internal" - a redirect:
>> /etc/hosts
Redirects are the shell's job. Before it starts the command, your shell opens /etc/hosts for appending, so it can connect the command's output to it. Your shell runs as you, not as root, and /etc/hosts belongs to root. The open fails, and the command never runs at all.
sudo would have run echo as root, but echo never needed root. Printing text doesn't touch any files. The step that needed root was opening the file, and that step was done without it.
The same thing happens with any file you can't write, and with a single >:
$ sudo echo hello > /root/note.txt
bash: /root/note.txt: Permission denied
So the rule is: the program that opens the file must be the one running as root. Here are three ways to arrange that.
Fix 1: pipe into sudo tee
tee copies its input to a file and to the screen. Because tee opens the file itself, running tee under sudo is enough:
$ echo "10.0.0.5 db.internal" | sudo tee -a /etc/hosts
10.0.0.5 db.internal
$ getent hosts db.internal
10.0.0.5 db.internal
echo runs as you (fine, it only prints), and tee runs as root and writes the file. Two details matter:
-ameans append. Without it,teeoverwrites the file.echo ... | sudo tee /etc/hostswould replace your whole hosts file with one line.teerepeats what it wrote. To keep scripts quiet, redirecttee's own output, which is allowed because/dev/nullis writable by everyone:
$ echo "10.0.0.6 cache.internal" | sudo tee -a /etc/hosts > /dev/null
This is the form to reach for first. It works the same in scripts, in CI jobs and over SSH.
Fix 2: run the whole line in a root shell
If you prefer writing the redirect normally, give it to a shell that already runs as root:
$ sudo sh -c 'echo "10.0.0.7 queue.internal" >> /etc/hosts'
$ tail -n 3 /etc/hosts
10.0.0.5 db.internal
10.0.0.6 cache.internal
10.0.0.7 queue.internal
Now the redirect belongs to the inner sh, which sudo started as root. Watch the quoting: the whole command must be one argument to sh -c, so use single quotes outside and double quotes inside. Don't put anything you didn't write yourself into that string, because the root shell interprets all of it.
A variant that avoids nested quotes reads the text from standard input:
$ sudo bash -c "cat >> /etc/hosts" <<< "10.0.0.8 search.internal"
Fix 3: become root for a while
For several root-only edits in a row, open a root shell with sudo -i, do the work, then exit. Every redirect inside that shell runs as root. It's convenient interactively, but in scripts prefer Fix 1, so you can see exactly which step runs with privileges.
Writing a whole file
The same pattern writes multi-line config files. Give tee a here-document; quoting 'EOF' stops your shell from expanding $ signs inside it:
$ sudo tee /etc/app.conf > /dev/null <<'EOF'
port = 8080
log_dir = /var/log/app
EOF
$ ls -l /etc/app.conf && cat /etc/app.conf
-rw-r--r-- 1 root root 35 Nov 10 08:44 /etc/app.conf
port = 8080
log_dir = /var/log/app
The file is created by root, so it's owned by root, which is what you want for a system config file. Without -a, tee replaces the whole file, which is also what you want here.
When you do want your shell to fill in a value, leave the text unquoted. Your shell expands $PORT before tee ever runs, so the variable comes from your environment, not root's:
$ PORT=9090; echo "port = $PORT" | sudo tee /etc/app.conf
port = 9090
That last command overwrote the file with a single line, a reminder of why -a matters when you only meant to add one.
Same trap, other forms
Once you know the rule, you'll see it elsewhere:
sudo cat config > /etc/app.conf: same failure. Usesudo tee /etc/app.conf < configorsudo cp config /etc/app.conf.sudo command 2> /var/log/app.err: the error redirect is opened by your shell too.cd /rootaftersudo:sudo cddoesn't work either, becausecdis a shell builtin that changes your shell's directory.
Check before you blame sudo
Permission denied with sudo in front doesn't always mean this redirect problem. If the message comes from the program itself rather than bash:, look at the file and the path. A directory on the way you can't enter, or a file owned by another service user, gives similar errors. The labs below include both kinds.
Questions
- Why does sudo echo text > file say Permission denied?
- Because the redirect (> or >>) is handled by your own shell, as your user, before sudo runs. sudo only elevates the echo command, which doesn't need root. Opening the file for writing is what needs root, and that happens without it.
- What is the simplest fix?
- Pipe into tee, which opens the file itself and runs under sudo: echo 'text' | sudo tee -a /etc/hosts. Use -a to append; without it tee overwrites the file.
- How do I stop tee from printing the line again?
- Send tee's own output to /dev/null: echo 'text' | sudo tee -a /etc/hosts > /dev/null. That redirect is fine, because /dev/null is writable by everyone.
- Is sudo sh -c 'echo text >> file' safe?
- It works, because the whole command line, redirect included, runs in a root shell. Be careful with quoting and never put untrusted input in that string: everything inside is interpreted as root.
Fix it yourself
A live machine in your browser, already broken. The grader checks the result.
- Pin a Hostname with /etc/hostseasy 3 min
- Fix "Permission denied" on Service Dataeasy 6 min
- Grant Exactly One Command via sudomedium 8 min
- sudo and Redirectionmedium 1 min
No account needed for your first tries.
More guides
- No space left on device: find what is filling the disk on Linux
Which filesystem is full, which directory is growing, which files are biggest, and the space a deleted file still holds. Five commands, with real output.
- RHCSA practice: what to do in the browser, what needs a VM, and the root password reset
Which RHCSA objectives you can practise in BrokenCluster's Rocky Linux labs, how to set up a free practice VM for the rest, and the root reset.
- SELinux for the RHCSA: contexts, ports and booleans, practised on a VM
The SELinux tasks the RHCSA expects, in the order you'd meet them: modes, file contexts, a web server on a new port, booleans and reading denials.