45

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

2
  • Have you tried running sudo before rebooting?
    – kos
    Aug 27, 2015 at 10:05
  • @kos have you down voted according to your comment ? if yes then you got my question wrongly. Jan 12, 2017 at 11:46

5 Answers 5

39

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
3
  • 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 2, 2018 at 6:32
  • 32
    Failed to restart sudo.service: Unit sudo.service is masked.?
    – xeruf
    Nov 13, 2020 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: askubuntu.com/questions/816285/…
    – gonzojive
    May 30, 2022 at 6: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.

4
  • 2
    I had to reboot as well.
    – Nathan B
    Feb 12, 2018 at 10:12
  • 1
    Instead of rebooting you can do sudo service sudo restart
    – Nick Rice
    Jun 2, 2018 at 6:32
  • @NickRice any idea on what to do in Arch Linux?
    – Necktwi
    Aug 5, 2018 at 3:50
  • 29
    What if $sudo service sudo restart outputs 'Failed to restart sudo.service: Unit sudo.service is masked.' Aug 2, 2019 at 12:31
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.

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).

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
  • 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, 2023 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .