Ubuntu: How to assign one or more groups to a user (via Shell/SSH)

Marc Wag­ner

Octo­ber 20, 2022

1 min read|

To edit an exis­ting user, use the user­mod com­mand on Linux.

Adding a group to a user #

The fol­lo­wing com­mand will help you to add a group to a user:

sudo usermod -a -G Group_Name Username

Adding multiple groups to one user at once #

If you want to add mul­ti­ple groups to a user, you just need to extend the exam­p­le abo­ve by mul­ti­ple groups. This way you can add mul­ti­ple groups to a user by using a com­ma sepa­ra­ted list. Here is an exam­p­le of how the who­le thing is struc­tu­red:

sudo usermod -a -G Group_Name,Group_Name_2,Group_Name_3 Username
Para­me­terDescrip­ti­on
-aStands for “append”. You should spe­ci­fy this, other­wi­se you will remo­ve all other groups of the user.
-GStands for “groups”. Here you can spe­ci­fy all the groups that will be added to the user, sepa­ra­ted by a com­ma. Important: wit­hout “-a” (append) all groups you don’t add here will be remo­ved from the user.

Display groups of a user #

If you are not sure which groups alre­a­dy exist for the desi­red user, you can sim­ply dis­play them. To do this, use the com­mand “groups”. The fol­lo­wing exam­p­le shows you how to dis­play all groups of a user:

groups Username

List all users in Linux #

In case you do not know the user­na­me, you can sim­ply dis­play all users. The users and their groups are stored in the file /etc/shadow. With the help of “cat” you can easi­ly dis­play them, as the fol­lo­wing exam­p­le shows:

sudo cat /etc/shadow
88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar