Permission denied when trying to append a file to a root owned file with sudo

Desmond Hume picture Desmond Hume · Dec 8, 2012 · Viewed 28.8k times · Source

This is the shell command that results in "Permission denied" when I'm trying to append the data in a file to another file with sudo:

sudo cat add_file >> /etc/file

The file at /etc/file is owned by root (i.e. me) and its permissions are rw-r--r--. Should I become root for a moment to make it work or is there a workaround for sudo?

Answer

Chris Seymour picture Chris Seymour · Dec 8, 2012

Run bash as sudo:

$ sudo bash -c "cat add_file >> /etc/file"

$ whoami;sudo bash -c "whoami";whoami
iiSeymour
root
iiSeymour