3

From a user that is part of the sudoers group, I can normally run any command by doing sudo <command>

But the following command fails stating "Permission denied"

sudo echo "myhostname" > /etc/hostname

It doesn't even ask me for the password. How do I change the hostname ?

2 Answers2

3

The correct command in your case will be echo "newhostname" | sudo tee /etc/hostname, because as nssnd explained, sudo applies only to single command, and redirection has less priority than sudo. Another option is to use bash command:

sudo bash -c "echo newhostname > /etc/hostname"
Danatela
  • 178
1

sudo applies only to your first command, so the ">" operation is done from your user.

To run your command:

  1. get a root shell by executing sudo -i
  2. execute your command: echo "myhostname" > /etc/hostname
  3. exit the root shell with exit
pyther
  • 368
kirill-a
  • 2,923