By ogr

2019-04-12 09:34:56 8 Comments

I stumble across a weird behaviour today. After adding a user to a new group like so :

# gpasswd -a test myuser

then connecting to a new bash session, here are the result for groups and groups myuser:

[email protected]$ groups
wheel myuser
[email protected]$ groups myuser
wheel myuser test

Only if I reboot the output of groups will be the same as groups myuser, but not necessary showing the groups in the same order.

So my question is simple : why ?


@Toby Speight 2019-04-12 14:34:53

  • When you run groups without an argument, it shows the groups list of the current process. Normally, the list is simply inherited from the parent process, but it is changed by login, newgrp and similar.

  • When you run groups with an argument, it shows the groups that are listed (in the user databases) for the specified user. That's the group list that login etc. will set the next time it's run for that user.

This is why updating the user/group databases will only affect future login sessions - don't expect it to change any processes that are already running.

@terdon 2019-04-12 09:46:38

Because changes to group membership only take effect after starting a new login shell. Starting a new non-login interactive shell session (which is what you get when you open a new terminal) is irrelevant.

So, when you run groups, that prints the groups your user is currently in. However, those were set up when your user first logged in and cannot be changed until you log in again. Therefore, groups doesn't include your new group.

On the other hand, when you run groups myuser, the system doesn't look for the groups the current user belongs to at the moment, it looks up the groups that the user myuser belongs to, which it gets by reading the settings file (/etc/group, presumably). Since your user is set up to belong to the new group in /etc/groups, this command shows that as well, even though you're not currently in that group since you haven't logged in again.

@Stephen Kitt 2019-04-12 09:53:05

newgrp also comes in handy, without logging in again ;-).

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Added user supplementary group, but 'groups(1)' not showing change

  • 2013-10-16 14:59:39
  • fthinker
  • 10500 View
  • 9 Score
  • 1 Answer
  • Tags:   users group

1 Answered Questions

[SOLVED] suplementary vs default

  • 2017-07-14 10:29:55
  • user240711
  • 32 View
  • 0 Score
  • 1 Answer
  • Tags:   osx group

1 Answered Questions

Adding user to secondary group not showing in id -Gn

  • 2016-12-30 12:25:35
  • MrBburn
  • 131 View
  • 0 Score
  • 1 Answer
  • Tags:   users group

4 Answered Questions

[SOLVED] Setting default username and group for files in directory

3 Answered Questions

[SOLVED] adding list of users to multiple groups

1 Answered Questions

[SOLVED] Trouble adding existing user to existing group in ubuntu

  • 2013-01-04 11:27:02
  • Windows
  • 13705 View
  • 2 Score
  • 1 Answer
  • Tags:   users group

2 Answered Questions

[SOLVED] Primary and secondary groups

2 Answered Questions

[SOLVED] su user fails with "grep: /var/cpanel/users/user: Permission denied"

Sponsored Content