2

I use Debian 12 and want to add a user bob to sudoers. While logged in as bob, I ran su root command to become root. As a root, I added bob to the sudo group using the following command:

sudo usermod -aG sudo bob

Right after that I inspected /etc/sudoers file, which had the following line: %sudo ALL=(ALL:ALL) ALL, which signifies that everybody who has sudo group has access to sudo.

However, when I exited the root shell issuing exit command and, therefore, returned to bob's shell, I was not able to use sudo as if bob still was not in the sudoers file: user not in sudoers.. Moreover, when I ran id command, bob did not have the sudo group. I tried to close the terminal and to log in / log out, but it did not work. It was required to reboot the machine so that bob had access to sudo.

Was there a way to not reboot the machine?

3
  • Could you please edit your question and explain exactly what " I exited the root shell and logged in bob's shell" means? Did you run the sudo usermod command as root, as bob or as another user? Was bob already logged in? How did you log into bob's shell?
    – terdon
    Commented Aug 4 at 15:13
  • I ran sudo usermod as root. I logged in as root using su root command while being logged in as bob. I logged into bob's shell just by opening up my terminal (bob is my account). Commented Aug 4 at 15:24
  • It may be a language issue, or it may be understanding. But "when I exited the root shell issuing exit command and entered bob's shell again" is slightly incorrect. You didn't enter Bob's shell again, you returned to the (same) shell that was temporarily paused while you ran su root. It's an important difference Commented Aug 4 at 16:53

1 Answer 1

5

You didn't log in as bob, you opened a new terminal while already logged in. If you had opened a new login shell, for example with

sudo -iu bob

Then you would have seen the new group. Alternatively, if you had closed bob's open session (log out from the GUI) and then logged in again as bob, then too you would have seen the new group.

So no, rebooting isn't necessary to pick up new group changes, but logging in again is.

To illustrate:

$ whoami 
bob
$ groups
bob

$ su -
# whoami
root
# usermod -aG sudo bob
# groups bob
sudo bob
# exit

$ whoami 
bob
$ groups
bob

So it looks like it hasn't worked, but that's because you're back in the shell you started from. Try opening a new login shell (not just a new terminal, a new login shell) instead:

$ su -          ## become root
# sudo -iu bob  ## open a new shell as bob
$ whoami
bob
$ groups
sudo bob

PS. If you are logged in as root, for example with su or sudo -i, there is no reason to use sudo, just run the commands directly, you're already root.

6
  • Does my shell preserve a session even if I close a terminal? Commented Aug 4 at 15:35
  • @VladMikheenko sorta. Opening a new terminal opens an interactive non-login shell, and you need a new login shell (I think; I may be wrong here) to see the new groups. See updated answer.
    – terdon
    Commented Aug 4 at 15:41
  • Sorry, @VladMikheenko please check again. Since I had a user on my system named bib and Arch uses wheel instead of sudo, I was doing my tests with bib and wheel and forgot to change some of the names.
    – terdon
    Commented Aug 4 at 15:42
  • 1
    Just a heads-up: on systemd systems, due to session lingering, you might have to terminate your session completely (logging out from the GUI might not suffice): askubuntu.com/q/1045993/158442
    – muru
    Commented Aug 4 at 15:45
  • @muru, could you please describe how one should terminate their session completely? Would it be as simple as issuing exit? Commented Aug 6 at 5:31

You must log in to answer this question.

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