4

I add this line to visudo, in order to give full permissions to yael user:

  yael ALL=(ALL) NOPASSWD: ALL

But when I want to update the /etc/hosts file, I get permission denied:

 su – yael
 echo "10.10.10.10 yael_host">>/etc/hosts
 -bash: /etc/hosts: Permission denied


 sudo  echo "10.10.10.10 yael_host">>/etc/hosts
-bash: /etc/hosts: Permission denied


 ls -ltr /etc/hosts
 -rw-r--r--. 1 root root 185 Aug  7 09:29 /etc/hosts

How can I give to user yael ability like root?

21

The source of the problem is that the output redirection is done by the shell (user yael) and not by sudo echo.

In order to enforce that the writing to /etc/hosts will be done by user root instead of user yael - You can use the following format:

echo "10.10.10.10 yael_host" | sudo tee --append /etc/hosts

or

sudo sh -c "echo '10.10.10.10 yael_host'>>/etc/hosts"
| improve this answer | |
  • @yael - did you try the solutions suggested in answer? did it work for you? – Yaron Aug 7 '17 at 11:08
1

Edit your /etc/sudoers (visudo) as follows:

# User privilege specification
root    ALL=(ALL:ALL) ALL
yael  ALL=(ALL:ALL) ALL

Then run:

sudo -- sh -c 'echo "10.10.10.10 yael_host">> /etc/hosts'

Or

sudo sh -c 'echo "10.10.10.10 yael_host">> /etc/hosts'
| improve this answer | |
  • still get Permission denied – yael Aug 7 '17 at 9:57
  • still - echo "10.10.10.10 yael_host">>/etc/hosts -bash: /etc/hosts: Permission denied – yael Aug 7 '17 at 10:00
  • maybe need to update others files as /etc/group , etc – yael Aug 7 '17 at 10:10
  • 1
    The redirections happen in yael's shell, before sudo is run. they are not affected in any way by sudo. the way to append text to a file you don't normally have perms to write to is with ... | sudo tee -a filename (-a aka --append). e.g. echo "10.10.10.10 yael_host" | sudo tee -a /etc/hosts as in @Yaron's answer. – cas Aug 7 '17 at 14:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.