How to Add a User to a Group in Linux

A group is formed in Linux, so that certain information can be used only by users in that particular group. Hence, we come down to a very important question in Linux. How to add a user to a group in Linux?
Well, we are now face to face with a simple question in Linux. How to add a user to a group in Linux? The answer is just one word, the useradd or usermod command. What is important, is to know how to form the group, why is a group formed, and only then can we come down to the question, how to add a user to a group.

Why do we Need Groups in Linux?

A particular group of users in Linux may need to share the same type of information, which is not required by other users, or should not be accessible by other users. This can be done very simply, by creating a group in Linux and then assigning permissions to the files created, such that only the group members can access them. This is the basic purpose for creating groups in Linux.

How to Create a Group in Linux?

Before adding users to a particular group in Linux, we need to learn the command to create a group in Linux. The groupadd command in Linux is used for creating new groups in Linux. Let us now take a look at the groupadd command in a little more detail, along with the options that we can use along with the command.

Syntax
groupadd [-g gid [-o]] [-r] [-f] [-K KEY=VALUE] groupname

The different options along with groupadd command can be used as follows.
  • -g: Using the -g option, you can specify the group id, 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 in Linux?

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 the useradd command, for adding a particular user to a group in Linux.

Syntax
useradd -G groupname username

Here, 'groupname' is the name of the group, to which you want to add a particular user, 'username'. However, for performing all these operations, you need to first login as the root user. Let us try to understand how to use the useradd command through the following example. Suppose, we want to add a user, Peter, to a group called Testingteam, the command to do so will be as follows:
useradd -G Testingteam Peter

Let us now take a look at some of the important files related to group information in Linux.
  • /etc/group: This file contains the account information for all the different groups in Linux.
  • /etc/gshadow: This file contains information for the secure groups.
  • /etc/login.defs: This file contains the shadow password suite configuration.
Why do we need to know these files? Well, before adding a user to a particular group in Linux, it is better that you check out 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 in the command, grep testingteam /etc/group. In the output, if you would get the information related to the testingteam group fetched out, it would mean that the group exists, else you would have to create the group. 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 user the command; useradd -G Testingteam,Developteam Peter. If the user 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 the command, usermod -G -a Testingteam Peter.

Hope this explanation to one of the most common queries in Linux, how to add a user to a group proves to be of help to you. For finding out more about the useradd and usermod command, you can always try the --help along with the command, to check out, all the other options.
By
Last Updated: 10/4/2011
Like This Article?
Follow:
Post Comment | View Comments
Your Comments:
Your Name: