Gentoo : cannot modify the hostname file as a sudoer

1

I am trying to change the hostname of a Gentoo host using a sudoer account. I use the following procedure :

  1. sudo rm -f /etc/conf.d/hostname
  2. sudo touch /etc/conf.d/hostname
  3. sudo echo "hostname=foo" >> /etc/conf.d/hostname
  4. sudo /etc/init.d/net.lo restart

Only the step 3 fails. It raises a permission error. So I would like to know why I get this ? I tried to stop the networking service first, but no change.

user189857

Posted 2013-04-02T09:28:40.403

Reputation:

Answers

2

It fails because the >> is interpreted by the normal user's shell; only the echo is run as root.

A simple way around this is to run the command in an interactive root shell:

$ sudo su -
# echo "hostname=foo" > /etc/conf.d/hostname
# /etc/init.d/net.lo restart
# exit
$

Note that you don't need the touch command, and if you just use > you can also do without the rm, as > will overwrite the file's contents.

Flup

Posted 2013-04-02T09:28:40.403

Reputation: 3 151

0

The right way to do this:

echo "hostname=foo" |  sudo tee -a /etc/conf.d/hostname > /dev/null

You don't need sudo to run echo, but you can't apply sudo to redirection, so instead pipe it into tee (an output splitter), in append mode (-a, acts like >>), and then can the output (because you're not interested in it) by redirecting it to /dev/null.

Synchro

Posted 2013-04-02T09:28:40.403

Reputation: 203