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?

peterh
  • 9,731
yael
  • 13,106
  • Add yael to group sudo. Logout and login. The entry %sudo ALL=(ALL:ALL) ALL Give all users in group sudo the rights. P:S If you like full granted rights. for comd-tools sudo -s –  Aug 07 '17 at 10:48
  • Your files are ok, i found a solution in stackoverflow https://stackoverflow.com/questions/4640011/append-text-to-file-from-command-line-without-using-io-redirection – Angel Porlan Aug 07 '17 at 13:13
  • You simply use sudoedit https://unix.stackexchange.com/questions/104429/how-do-i-edit-a-file-as-root – Benoît Aug 07 '17 at 15:08

2 Answers2

22

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"
Yaron
  • 4,289
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'
GAD3R
  • 66,769
  • still get Permission denied – yael Aug 07 '17 at 09:57
  • still - echo "10.10.10.10 yael_host">>/etc/hosts -bash: /etc/hosts: Permission denied – yael Aug 07 '17 at 10:00
  • maybe need to update others files as /etc/group , etc – yael Aug 07 '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 07 '17 at 14:16