How to insert text into a root-owned file using sudo?
Solution 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
.
Solution 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.
Related videos on Youtube
tshepang
I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.
Updated on September 17, 2022Comments
-
tshepang over 1 year
Possible Duplicate:
Redirecting stdout to a file you don't have write permission onRunning a command like
sudo echo 'text' >> /file.txt
fails with:bash: /file.txt: Permission denied
-
Admin about 4 years
-
-
erik about 11 yearsThis is definitively the better way, as this lets you easily
echo
complicated statements with newlines and so on. For example I didn’t manage to to the following withsudo -s
, but withtee
it was very easy:echo -e '#!/bin/bash\n\nexport PYTHONPATH=/usr/local/lib/python2.7/site-packages\ngtk-redshiftreal $@' | sudo tee gtk-redshift