45

Do we need to reboot after adding a user to /etc/sudoers?

Ashish Karpe
  • 2,427

5 Answers5

40

No. It'll work with the next sudo command.

But if it does not work, you can avoid rebooting by running

sudo service sudo restart
Kulfy
  • 17,696
aastefanov
  • 1,391
  • 12
  • 16
  • 3
    On my 14.04 system it did not work with the next sudo command. I needed to do sudo service sudo restart (Rebooting would have worked too, I guess. Bit overkill though.) – Nick Rice Jun 02 '18 at 06:32
  • 33
    Failed to restart sudo.service: Unit sudo.service is masked.? – xeruf Nov 13 '20 at 11:41
  • 1
    sudo is masked, which means most systemctl commands don't work with it. There is no need to restart anything after sudoedit, though, so there shouldn't be a need to restart the sudo "service." Read more here: https://askubuntu.com/questions/816285/what-is-the-difference-between-systemctl-mask-and-systemctl-disable – gonzojive May 30 '22 at 06:53
12

I just did this and yes, I did in fact have to reboot. So, maybe the previous answer wasn't wrong, but it definitely isn't right 100% of the time. Writing this in case someone else is looking for the answer as I just was.

2

In CentOS 7 you can also Logout from the system with "exit" and Login again and the Sudoers will be updated.

I've tested on minimal installation but I believe it works in other targets and possibly other distributions aswell.

Nelssen
  • 181
1

In fact the only thing you need is to get a (new) login prompt, so using the following command works:

anyuser$ su -l <user>
user$ sudo <thenewlysudoedcommand>
... works...

But if you are logged in as GUI, then you need to logout & login again.

But, as with MOST linux tools you DON'T need to reboot the computer (that's a Windows thing).

liar
  • 11
1

After add user to sudo group,

#su - root -c "usermod -aG sudo username;"

execute following command:

newgrp sudo

And after that you can use sudo in your commands in current session without need to restart.

Also if you are in a shell script and you want to execute command just after add user to sudoers, run following command instead:

#su - root -c "usermod -aG sudo username;"
sg sudo -c 'sudo command1; sudo command2;'

And If you don't want to reapeat sudo word in each command do as follow:

sg sudo -c "sudo -- sh -c 'command1; command2;'"

OR

sg sudo -c "sudo -- sh -c ' command1 command2 '"

  • 1
    Please be careful if you use newgrp sudo as this will modify that user's primary group. If you want an alternative that does not modify that user's primary group but instead updates that user's secondary group list to include the previously added group, please try exec sg sudo newgrp. More info here. HTH. – jaimet Aug 12 '23 at 13:34