The Linux system supports joining users together in a group to make account management easier. Let’s take a look at how you can create, list, and erase groups and add and remove users from them.
What You Need for This Tutorial
Apart from a Linux-based system, such as Ubuntu OS (if you don’t have it already, see how to install ubuntu desktop), you also need to ensure that you are using the root account or a user that has super permissions (sudo). To check for the user groups, you can use the “less” command to list users on Linux.
What You Should Know about Linux Groups
The groups or Linux are identical to groups on any other operating system, or in real life. Think of a group as a unit of organization that you can use to manage user accounts of your system. After creating groups and adding the desired users to it, you can share files with the group, as well as set different permission, including those to read, write, or execute.
Linux divides all groups into two categories:
- Primary groups – if you create a file from scratch, the system will set its group to your primary group. You can find more info on your primary group if you check “/etc/passwd” file.
- Supplementary groups – some people also call them secondary. You use these when you need to give specific permissions to a file to the preferred members of the group. By adding a user to a particular group, they also receive all the permissions set for its members.
The only limitation is that each user can only have a single primary group while you can be a member of the unlimited supplementary groups (or not be a member of any).
How to Add a User to a Group
If you have an existing account that you want to add to a supplementary group, use the following line:
useradd -a -G group_name account_name
Here, the “-a” is an option that tells the system you want to add the user to a group. In case you do not use this option, the system will think that you want to remove the user from all other groups except the ones listed after the “-G” option.
Keep in mind that Linux doesn’t confirm the success of the command. The system will only report if he failed to find a user with the specified username.
How to Add a User to Multiple Groups
When you need to add a particular user to multiple groups, use the following command:
usermod -a -G group_name_1, group_name_2, group_name_3 account_name
The command is basically the same, except that you separate the groups by using commas. You can list as many groups as you want. You won’t get any confirmation that the command was successfully executed.
How to Remove an Account from a Single Group
You use a different command to remove an account from the group. Here is how the line should look:
gpasswd -d account_name group_name
How to Create and Erase a Group
First, let’s take a look at how to make a new group:
Once you create the group, you can start adding members to it.
When you need to erase a group, use the following line:
How to Add Users to Groups When Creating an Account
If you do not want to waste time on two separate commands, you can use the following command to add an account to multiple groups when creating it:
useradd -g primary_group -G secondary_group 1, secondary_group 2 username
Make sure that you run the command properly and please note that you should specify the username at the end of the line.
Adjust the Primary Group of a User
Each account can have only one primary group. In case you want to change it, use this command:
usermod -g group_name account_name
Show User Information
The “id” command can be useful to discover what groups the user belongs to, as well as get additional information about the account.
If Linux doesn’t recognize the name of the user, it will show you complete information of the user who is currently logged in.
When you only need to find out the secondary groups of a specific user here are the commands to use:
You should now know everything there is to know about adding users to groups on Linux. You can perform these commands on any Linux-based OS, including but not limited to Ubuntu, Linux Mint, and Debian.
If you are a fan of using Python programming language, you can make your life easier by using Python Pip. It is a package management utility that allows you to install and manage Python software packages easily. Ubuntu doesn’t come with pre-installed Pip, but here…
If you are looking for a convenient way to manage Java applications on Ubuntu, the solution may lie in the IntelliJ IDEA development platform. An excellent advantage of this option is that it is relatively suitable for beginners, but experienced users will also find it…