Users are arranged into different groups on all Linux-based systems. The whole idea behind this is that it makes the administration of the system easier, as the users can be arranged into different groups, and can be granted different permissions as required by the system administrator. The same user can be included in different groups, and the same file can be given different permissions based on the groups.
This article is about how to add a user to a group.
The instructions will work well on most Linux distros, including Ubuntu, CentOS, Debian, Fedora, Linux Mint, etc.
Different scenarios when adding a user to a group
Adding a user to a group has many factors to consider. Some are:
- Existence of the user – The commands are usually different depending on whether the user already exists on the system,
- Group category – The main group that the user belongs to is called the primary group. Generally, this group has the same name as that of the user. The other groups that the user belongs to are called the secondary groups. There are other groups too, that a user is not a part of, at all.
- User permissions – This is a major factor, as only the super users can add any user to any given group. This permission limits the users in terms of which groups and which users they can edit.
Keeping all these factors in mind, we are only presenting two commands in order to add users to groups. But this is being shown considering that the user entering these commands is a super user/root (can perform sudo). These are the commands:
To add new users to groups
First, and the only exception, to add new users to groups:
sudo useradd -G <group_name> <new_user_username>
The id command shows basic information about a user on the system. Therefore, to prove that the user doesn’t exist at first:
pulkit@hostname:~$ id testuser id: ‘testuser’: no such user pulkit@hostname:~$
Now we add a new user to an existing group:
pulkit@hostname:~$ sudo useradd -G pulkit testuser pulkit@hostname:~$ id testuser uid=1004(testuser) gid=1004(testuser) groups=1004(testuser),1000(pulkit) pulkit@hostname:~$
All the remaining scenarios – when a user already exists
Now the only condition is that the user should already exist on the system. All the remaining scenarios can be worked out with this command:
sudo gpasswd -a <user_name> <group_name>
If you want to remember this command, then you can create a new user before adding the user to the system. This can be done with:
sudo adduser <user_name>
pulkit@hostname:~$ sudo adduser testuser Adding user `testuser' ... Adding new group `testuser' (1001) ... Adding new user `testuser' (1001) with group `testuser' ... Creating home directory `/home/testuser' ... Copying files from `/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for testuser Enter the new value, or press ENTER for the default Full Name : Room Number : Work Phone : Home Phone : Other : Is the information correct? [Y/n] y pulkit@hostname:~$ sudo gpasswd -a testuser pulkit Adding user testuser to group pulkit pulkit@hostname:~$
Now checking if this worked:
pulkit@hostname:~$ id testuser uid=1001(testuser) gid=1001(testuser) groups=1001(testuser),1000(pulkit) pulkit@hostname:~$
Removing users from a group
This is not a part of adding users to groups, obviously, but an essential command to know if you’re managing groups. The command uses the same format as seen with the gpasswd command:
sudo gpasswd -d <user_name> <group_name>
pulkit@hostname:~$ sudo gpasswd -d testuser pulkit [sudo] password for pulkit: Removing user testuser from group pulkit pulkit@hostname:~$ id testuser uid=1001(testuser) gid=1001(testuser) groups=1001(testuser) pulkit@hostname:~$
This removes the user from the given group, as shown.
You can test these commands out on a Linux server at Vultr.
Adding users to groups on Linux is an easy task, if you know what commands to use. You can essentially do everything using just one command, as long as you know how to use it. The gpasswd command is the simplest command to use for the task. The useradd or usermod commands can be used as well, but they have a comparatively more complex syntax, and therefore not recommended for beginners.
We hope this article served its purpose. Let us know if you have any questions in the comments below.