Archive for September, 2007

Unix/Linux — Redirect output with sudo

September 26, 2007

Sometimes you might need to redirect the output with sudo privileges. For example:

echo myhost.com > /etc/hostname

will give you the following error:

-bash: /etc/hostname: Permission denied

Unfortunately, using:

sudo echo myhost.com > /etc/hostname

will give the same error.

The solution is to start bash as sudo and then give the entire command to bash as input:

sudo bash -c “echo myhost.com > /etc/hostname”