18

I want to add myself to the "docker" group. These are the commands I ran, and their output:

user1@laptop-p3510:~$ sudo usermod -a -G docker user1
user1@laptop-p3510:~$ groups
user1 adm cdrom sudo dip plugdev lpadmin lxd sambashare
user1@laptop-p3510:~$ whoami
user1
user1@laptop-p3510:~$ groups user1
user1 : user1 adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

Am I in the "docker" group or not? Why does groups $USER give different results than groups?

2
  • 12
    Try re-logging in before running groups without parameters.
    – choroba
    Nov 29, 2022 at 13:22
  • 4
    If you don't want to re-login you can do newgrp docker after adding you to the docker group and you will be able ot run docker without sudo without having to reopen the terminal/relogin
    – GACy20
    Nov 30, 2022 at 8:55

2 Answers 2

37

Because you need to log out and back in again for the added groups to be active.

groups returns the groups you are a member of for the current session

groups user1 checks to see what the config says that user is a member of.

Because you added user1 to a group and you have not yet logged out and back in again, the groups user1 was a member of when you logged in are the ones that current session will use.

Hope that helps

2
  • 7
    Yes, this was it. Thank you! I felt like I was in the Spongebob "that's not my wallet" meme.
    – Grollo
    Nov 29, 2022 at 13:40
  • 1
    Possibly also worth mentioning: newgrp can be used to start a new session without logging out and back in (but of course only the terminal you run it in will be using that new session). Dec 2, 2022 at 15:18
11

Like the UID and GID, groups are a per-process credential. Each process has its own list of groups that it belongs to, which is initialized from /etc/group at login time (by the initial process that handles your login) but inherited from the parent process otherwise.

So when you run groups from a shell, it inherits the group list from the shell (which has inherited them from the terminal app, etc) and that's the list it shows. The same applies to the UID and GID shown by id (and in fact also to environment variables shown by env). This list does not automatically change when /etc/group is updated – until the next time you log in.

Running groups SomeUser or id SomeUser, on the other hand, asks the program to read group information directly from /etc/group (in the same way that a login process would), so naturally it'll give you the updated list.

(User-switching tools like su and sudo also do the same, so if you urgently need to do something with a specific group you can "su" to root and then "su" back to yourself – su will reinitialize the group list as part of its job. You'll also notice that only the shell run by "su" has the new groups, while all other shells still show the old ones.)

To answer the question, yes, your account is in the "docker" group, but the specific shell you're running 'groups' from is not. Most things don't care about the account but only about the specific process that's performing the action (with some exceptions).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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