Amazon Linux add user to group

9,260

The current shell was initialized before you added yourself to the group, so it cannot reflect a later change to your group memberships. You can

exec sg docker

to replace the current shell with a new one which is explicitly switched to the group you added yourself to, or simply log out and back in.

Share:
9,260

Related videos on Youtube

nybon
Author by

nybon

Updated on September 18, 2022

Comments

  • nybon
    nybon almost 2 years

    I am using Amazon Linux AMI image doing development. And I want to add the default user 'ec2-user' into group 'docker' like below:

    sudo groupadd docker
    sudo gpasswd -a ec2-user docker
    

    When I logged in as user ec2-user, and run groups ec2-user, it shows:

    ec2-user : ec2-user wheel docker
    

    And this seems to indicate the 'ec2-user' was added to group 'docker'. However, if I run groups command without any parameter, which should be the same as groups ec2-user because I logged in as ec2-user, it shows:

    ec2-user wheel
    

    And it tells me ec2-user is not in the group 'docker' yet (actually it was not added to this group because permission error was reported when I tried to access some file belongs to group 'docker').

    And this does not happen if I try to add the 'ec2-user' into group in this way:

    sudo gpasswd -a ${USER} docker
    

    And I found I can add other user name like 'harry' into group like below without any problem:

    sudo gpasswd -a harry docker
    

    My question is what is the cause for this behavior? It seems there is something wrong with this particular user name 'ec2-user' when adding it to a group but I have no idea why this happens. Any help is appreciated. Thanks.

    • Panther
      Panther about 10 years
      You have to log out and back in or start a new log-in shell for changes to your group membership to become effective.
    • nybon
      nybon about 10 years
      @bodhi.zazen but it is not the case for other users I created such as 'harry' in the example. Once I created use 'harry' and added it into a group using gpasswd command, I can see it added to group immediately without logging out.
    • David A. Gray
      David A. Gray over 6 years
      Taking into account the comment, this answer is correct, and deserves to be marked as accepted.