The groups that a user belongs to in an operating system are a very flexible way of managing users on a system weather that be restricting permissions or extending functionality because of the fundemental importance of groups and group permissions in the Linux operating system after a short time you will almost surly need to add or remove a user from a group. In this post I will show you how to add a user to an existing group on a Linux system.
Lets say that we have a user with the username of tom and lets say that we want to allow tom to have sudo permissions on our system. On a Ubuntu linux system the
/etc/sudoers file comes preconfigured to give sudo access to users that are a member of the sudo group.
Check if the user is already in the group
The first thing we need to do is check to see if the user already a member of a group. Although this is not a hard requirement, it is good practice to have a full understanding of the current situation when issuing commands. Below is an example of listing all the users in the group sudo.
$ grep sudo /etc/group sudo:x:27:kyle $
So based on the output we can see the only user that is in the sudo group is kyle.
Add the user to the group
Below is the command that adds the user tom to the sudo group.
$ sudo usermod -aG sudo tom $
sudo usermod - With super user privileges issue the usermod command
-aG - will add the user to the group and allow that user to keep membership in all of there existing groups.
sudo - the group to add the user into
tom - the user to add
We did not get any errors so if we check one more time what users are in the sudo group we will see that tom now shows up.
$ grep sudo /etc/group sudo:x:27:kyle,tom $
And thats all you need to do in order to add a user to a group on a Linux operating system.