A group is formed in Linux so that certain information can be used only by users of that particular group. Here are the ways to add a user to a group in Linux.
It’s very easy to add a user to a group in Linux. One can use the command useradd or usermod to do so. However, it is also important to know as to how to form the group and why is it formed.
Need of Groups
A particular group of users in Linux may need to share a type of information, which is not required by other users. This can be done very simply by creating a group and then assigning permissions to the files created, such that only the group members can access them. This is the basic purpose for creating the groups.
How to do it?
Before adding users to a particular group, we need to learn the command to create a group. The groupadd command is used for the same. Let us now take a look at this command in more detail along with the options that can be used with this command.
Syntax
groupadd [-g gid [-o]] [-r] [-f] [-K KEY=VALUE] groupname
The different options with groupadd command can be used as follows.
- -g: Using the -g option, you can specify the group id and a unique number. The value must however be a non-negative unique number greater than 500. However, if you use the -o option along with the groupadd command, you can specify a non-unique group id.
- -r: This option is used for creating system accounts. The first available group id below 499 is chosen as an id for the group. However, if the -g option is provided, you can specify your own group id.
- -f: If this option is provided, then the command will exit with success status, if a particular group that you are trying to create already exists.
- -K: With the -K option, you can specify the minimum and maximum values for group ids. For example, -K GID_MIN=200 -K GID MAX=300, will cause the groupids to be within the range of 200 to 300. In general, these ranges are mentioned in the /etc/login.defs file, but this command overrides all the options mentioned in that file.
How to Add a User to a Group?
As already mentioned, new users can be added to a group, using the useradd command. Let us now take a look at the syntax of this command.
Syntax
useradd -G groupname username
Here, ‘groupname’ is the name of the group to which you want to add a particular user and its ‘username’. However, for performing all these operations, you need to first login as the root user. Suppose, we want to add a user called Peter to a group called Testingteam, the command to do so will be:
useradd -G Testingteam Peter
Let us now take a look at some of the important files related to group information.
- /etc/group: This file contains the account information for all the different groups.
- /etc/gshadow: It contains information for the secure groups.
- /etc/login.defs: It contains the shadow password suite configuration.
We need to know these files because, before adding a user to a particular group, it is better that you check if that particular group exists or not. In the previous example, to check if the group testingteam existed or not, all you had to do was type the command, grep testingteam /etc/group.
In the output, if you would get the information related to it, then it would mean that the group already exists, else you would have to create it. For adding the user to more than one group, you can separate the groupnames with ‘,’ (comma).
For example, to add the user Peter to the teams Testingteam and Developteam, you can use the command useradd -G Testingteam,Developteam Peter. If he already exists and you want to add him to supplementary groups, then you need to use the usermod command. In the previous example, this can be done by using usermod -G -a Testingteam Peter.
For finding out more about the useradd and usermod command and to check out all the other options, you can always try the ― help command.