Linux Add User To Group

Linux: Add User to Group (Primary/Secondary/New/Existing)

Written by: Roberto Sanchez

Roberto Sanchez

Roberto has been a professional full-stack developer for more than 10 years. Triaging and resolving the most mysterious bugs found in code is one of his many technical specialties. He loves experimenting with emerging programming languages and tools — and sharing his experience with the HostingAdvice readership.

See full bio »

Edited by: Lillian Castro

Lillian Castro

Lillian brings more than 30 years of editing and journalism experience to our team. She has written and edited for major news organizations, including The Atlanta Journal-Constitution and the New York Times, and she previously served as an adjunct instructor at the University of Florida. Today, she edits HostingAdvice content for clarity, accuracy, and reader engagement.

See full bio »

Let’s see how we can add new and existing users to primary and secondary groups in Linux. The standard Linux permission model makes use of users, groups, and file permissions (i.e., read, write, execute, and a sticky bit).

Adding a User to a Group in Linux

If you just want to add a user to a group use the following command:

sudo adduser username grouptoadd

This will add your user: username, to the grouptoadd group. More often than not, this is the best practice for when you want to add a user to a group. Technically, this is considered a secondary group. The primary group defaults to a group that is the same as the username in Linux. In this example, the primary group for username would most likely be called “username” as well.

The Nitty-Gritty Details and a Tutorial

There are two kinds of groups:

  1. Primary Group: This is the group applied to you when you log in; in most user cases it has the same name as your login name. The primary group is used by default when creating new files (or directories), modifying files, or executing commands.
  2. Secondary Groups (AKA Supplementary Groups): These are groups you are a member of beyond your primary group. As an example, this means that if a directory or file belongs to the www-data group (as used by the web server process in this case), then all www-data group members can read or modify these files directly (assuming the permissions also allow for this).

A list of all currently available groups can be found in the /etc/group file.

Note that every group can also have administrators, members, and a password. See explanations of the gpasswd and sg commands below.

1. Create a New User: useradd or adduser

Linux users can be added via the useradd or adduser commands. Note that useradd is the native binary associated with Linux systems, whereas useradd is a Perl script that uses said binary in its backend. Both commands share functionality, but some say adduser is more user-friendly, so we’re going to start there in our demo. Using the adduser command, let’s create a new user: foobar. Later we’ll change the group permissions for this new user.

sudo adduser foobar

We will be asked to enter our (sudo-allowed) user password before the user account is created:

[sudo] password for user:  Adding user foobar' ...  Adding new groupfoobar' (1001) ...  Adding new user foobar' (1001) with groupfoobar' ...  Creating home directory `/home/foobar' ...  ...

We see that the user, foobar, was assigned the primary group, foobar, by default.

2. Get User ID and Groups Information: id and groups

To show all the user information and group memberships, we can use the id command:

$ id foobar  uid=1001(foobar) gid=1001(foobar) groups=1001(foobar)
$ id foobar  uid=1001(foobar) gid=1001(foobar) groups=1001(foobar)

Here the gid, or group ID, is the primary user group and groups is the secondary group.

We could also get all the users’ groups with the groups command:

$ groups foobar  foobar : foobar
$ groups foobar  foobar : foobar

3. Change the Primary Group of a User: usermod -g

In some cases it can make sense to change the primary group of a user.

We can do this with the usermod command:

$ sudo usermod -g www-data foobar

The lowercase -g option refers to a primary group.

Let’s verify that the change was made:

$ id foobar  uid=1001(foobar) gid=33(www-data) groups=33(www-data)

Now foobar has the www-data primary group context. So whenever a new file is created by this user, it will be assigned the www-data group by default.

Let’s undo this change before we continue:

sudo usermod -g foobar foobar

4. Add or Change Users in Secondary Groups: adduser and usermod -G

Now let’s add our foobar user to www-data as a secondary group. The easiest way to do this is via the adduser command:

sudo adduser foobar www-data

We can see the secondary group of this user was updated:

$ id foobar  uid=1001(foobar) gid=1001(foobar) groups=1001(foobar), 33(www-data)

There is another way to achieve the same result as above using the usermod command:

$ sudo usermod -G www-data foobar

The uppercase -G option refers to a secondary or supplementary group. Now foobar will have access to the www-data group files, but new files created by that user will not have the www-data group label by default.

It’s also possible to add a user to several secondary groups at once using the usermod command:

$ usermod -a -G group1, group2, group3 foobar

The optional -a option makes sure the groups are added to the existing secondary groups of the user (if these exist). If this option is omitted, the user will be removed from any groups not listed after the “-G.”

5. Create or Delete a Group in Linux: groupadd and groupdel

Using the groupadd command, we can create a new group: group1.

sudo groupadd group1  sudo adduser foobar group1

We can then remove group1 from the Linux system utilizing the groupdel command:

sudo groupdel group1

This will also remove the memberships of any user related to this group.

User Administration in Linux (Other Commands and Articles to Try)

Let’s wrap up this article by referring to some of the other group commands in Linux:

Now you should be able to confidently configure users, groups, and their administrative info in Linux. Feel free to check out our article on changing file ownerships in Linux for more insights.

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.

ABOUT THE AUTHOR

Roberto Sanchez

Roberto Sanchez got his start with web hosting when he was still in high school, with a Perl script to conjugate verbs on a shared host. Roberto has been involved with web development ever since, experimenting with many web languages and hosting types. Roberto is on the web development team at Digital Brands Inc., where he applies his knowledge of PHP and WordPress to improve performance for the firm's various web properties. Roberto also leverages NodeJS to develop exciting products at Digital Brands Inc. and is eager to share his experience with HostingAdvice.com users.

« BACK TO: HOW-TO
Follow the Experts
We Know Hosting

$

4

8

,

2

8

3

spent annually on web hosting!

Hosting How-To Guides