How to insert text into a root-owned file using sudo?

Possible Duplicate:
Redirecting stdout to a file you don't have write permission on

Running a command like sudo echo 'text' >> /file.txt fails with:

bash: /file.txt: Permission denied
Asked By: tshepang


You are telling bash to open a file and append the output of the command sudo echo 'text' to it, which of course doesn’t work since your bash runs as non-root. Interactively, I usually run sudo -s to get around this (since then the shell runs as root and can open the file). Alternatively, you can run sudo sh -c "echo 'text' >> /file.txt", which also works, but is a bit of a hassle with all the interpolation/escaping that can interfere if you have complicated expressions.

Answered By: Thomas Themel

This doesn’t work because the redirection is executed by the shell, not by the command it applies to. But your shell is not running as root, only echo 'text' is.

A common trick when you need to have root permissions to write to a file, but not to generate the data, is to use tee:

echo 'text' | sudo tee -a /file.txt

tee prints the text to stdout, too. In order to mute it so it behaves more similar to shell appending (>>), route the stdout to /dev/null:

echo 'text' | sudo tee -a /file.txt > /dev/null

If you do need root permissions to generate the data, you can run two separate sudo commands, or run a shell inside sudo and do the redirection there (careful with the quoting).

sudo echo 'text' | sudo tee -a /file.txt
sudo sh -c 'echo "text" >>/file.txt'

When overwriting rather than appending, if you’re used to your shell refusing to truncate an existing file with the > operator (set -o noclobber), remember that this protection will not apply. sudo sh -c 'echo >/etc/passwd' and sudo tee /etc/passwd will overwrite /etc/passwd, you’d need sudo sh -o noclobber -c 'echo >/etc/passwd' for that noclobber setting to also be applied to the sh started by sudo.

Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.