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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x