How to insert text into a root-owned file using sudo?
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
.
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.