PDF download Download Article
A complete guide to managing user groups in Linux
PDF download Download Article

In Linux, groups organize users by grouping certain access levels to files and directories. Every user is a member of a primary group by default and can also be added to secondary groups with additional permissions. If a set of Linux users all need the same permissions to specific files and directories, you can add them to a group and set permissions for that group as a whole instead of for each individual user. This wikiHow article will teach you how to add a user to a group, change a user's primary group, and take care of other group-related administrative tasks on a Linux server or workstation.

Things You Should Know

  • Use the command sudo usermod -a -G <groupname> <username> to add a Linux user to a group.
  • To change a user's primary group, use sudo usermod -g <groupname> <username> .
  • To remove a user from a group, use sudo gpasswd -d <groupname> <username> .
Section 1 of 10:

Add a User to a Secondary Group

PDF download Download Article
  1. Every Linux user belongs to a primary group, which is used by default when they create files. Users can also be assigned to secondary groups to receive additional permissions. In most cases, you'll want to add a user to a secondary group without affecting their other group memberships or changing their primary group. This command ensures that behavior.
    • The -a option appends the username to the group without replacing the user's existing groups. The -G option indicates that you're adding to a secondary rather than changing the primary group.
    • If you leave out -a -G , the user will be removed from all secondary groups that are not included in the command. [1]
  2. Advertisement
Section 2 of 10:

Change a User's Primary Group

PDF download Download Article
  1. Sometimes, it's necessary to change a user's primary group. For example, suppose a user on your company's server is promoted or moved to another department. In that case, you may need to change their primary group to give them the same level of access as the rest of their department by default. This command reassigns a Linux user to the specified group.
Section 3 of 10:

Add a User to Multiple Groups

PDF download Download Article
  1. For example, if you need to add user billybob to the www , dns , and staff groups, use sudo usermod -a -G www,dns,staff billybob .
  2. Advertisement
Section 4 of 10:

View a User's Groups

PDF download Download Article
  1. This command displays a simple list of the groups a user belongs to—both their primary group and any secondary groups.
    • If you want to see the GIDs (group IDs) of each group the user belongs to, use id <username> instead.
Section 5 of 10:

View a Group's Users

PDF download Download Article
  1. This command lists the group's name, GID (group ID), and usernames of each group member.
    • To view all groups on a system, use getent group or cat /etc/group .
  2. Advertisement
Section 6 of 10:

Remove a User from a Group

PDF download Download Article
  1. For example, if you no longer want user billybob to be a part of the www group, you'd use sudo gpasswd -d www billybob . [2]
Section 7 of 10:

Create a Group

PDF download Download Article
  1. For example, if you wanted to create a group called www , you'd type the command sudo groupadd www .
    • If you want to specify a group ID (GID) during creation, use sudo groupadd -g <groupid> <groupname> instead. [3] Otherwise, the group will be assigned a unique GID automatically. [4]
  2. Advertisement
Section 8 of 10:

Create a New User and Add It to Groups

PDF download Download Article
  1. Let's say you want to add a new user account called billybob to your Linux server. You know that billybob's primary group should be the users group, and their secondary groups should be www and ftp . To create this user's account and add them to the necessary groups simultaneously, you'd use sudo useradd -g users -G www,ftp billybob .
    • Don't forget to set a password for the new user after creation.
Section 9 of 10:

Delete a Group

PDF download Download Article
  1. This removes a group from the system.
    • You can only delete a group if it's not the primary group of a Linux user . If you want to do that, you'll need to delete the user first or change the user's primary group to something else. [5]
  2. Advertisement
Section 10 of 10:

Change a File's Group Ownership

PDF download Download Article
  1. For example, if you want the developers group to own a file called users.txt , you’d enter sudo chgrp developers users.txt . Anyone who is a member of the developers group will now have the developer's group's permissions for that file.
    • You can also use chgrp to change the owner of a directory. Just replace the filename with a directory name.
    • To change the ownership of a directory and all of its contents (including subdirectories), add the -R option to make the command recursive.

Expert Q&A

Ask a Question
      Advertisement

      Tips

      • While you can technically edit /etc/group by opening it with a text editor like Nano or Vim , it's best to use the vigr command to lock the file during the editing process. [6]
      • When viewing a file or directory's permissions, the second set of characters (after the first character, which is always d for a directory or - for a regular file) indicate the group's permissions. For example, if a file's permissions are -rwxrw-rw- and the group is developers , the developers group has rw permissions (read and write).
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 4,282 times.

      Is this article up to date?

      Advertisement