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 ?

Kaizer Sozay
  • 739
  • 3
  • 13
  • 19

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
  • 158
  • 11
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
  • 328
  • 1
  • 6
kirill-a
  • 2,883
  • 1
  • 16
  • 22