Add a user to groups

Add a User to a Group on Linux

Adding and removing users from a group, here are the basic steps to get you started.

 

Add a New Group

To add a new group, we will use the groupadd command like this:

groupadd <groupname>

Add an Existing User to a Group

Next we’ll add a user to the group, using this syntax:

usermod -a -G <groupname> username

For example, to add user tommy to the group admins:

usermod -a -G admins tommy

 

Change a User’s Primary Group

Sometimes you might want to switch out the primary group that a user is assigned to:

usermod -g <groupname> username

View a User’s Group Assignments

To see all the groups a user is associated with, type the command id followed by the username:

id <username>

This will display output something like this:

uid=500(tommy) gid=500(tommy) groups=500(tomyy), 1100(admins)

You can also use the groups command if you prefer, though it is the same as using id -Gn <username>.

groups <username>

View a List of All Groups

To view all the groups on the system, you can just use the groups command:

groups

 

 

Sudo

 

We will take this example from a fresh install of debian.

in order to use sudo, do a apt-get install visudo

after installation you can add any user to the sudo group with the help of the commands above:

usermod -a -G sudo tommy

and you are done.

 

 

 

 

 

Posted on: Saturday 26 November 2016