Tuesday, September 14, 2010

Sudo Redirect Problem

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


For example:

echo newhost.com > /etc/hostname

will give you the following error:

-bash: /etc/hostname: Permission denied

Unfortunately, using:
sudo echo newhost.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 newhost.com > /etc/hostname”

or if u want by a specific user

sudo -u <some user> bash -c “echo newhost.com > /etc/hostname”



No comments: