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 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 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,103 times.

      Is this article up to date?

      Advertisement