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 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 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
200 characters left
Include your email address to get a message when this question is answered.
Submit
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

You Might Also Like

Add a User to a Group on WindowsSimple Ways to Add a User to a Group on Windows
Become Root in Ubuntu Get Root Access in Ubuntu Linux
Linux How to Change UserLinux: How to Change User in Terminal & GNOME
Change File Permissions in Linux from the TerminalChange File Permissions in Linux from the Terminal
Become Root in Linux Access and Use the Root Account in Linux
Change the Root Password in Linux Change Your Root Linux Password (Even If You Don't Know It)
Sudo Command Not Found2 Ways to Fix the "Sudo Command Not Found" Error on Linux
Delete Read Only Files in Linux Delete Read-Only Files in Linux
Add Users from CMDAdd Users from CMD
Use SSHUse SSH
Zsh: Permission Denied Fix the Zsh: Permission Denied Error on Mac
Change File Permissions on Windows 72 Simple Ways to Change File Permissions on Windows 7
Set up an FTP Server in Ubuntu Linux Install and Configure an FTP Server in Ubuntu
Run a Program from the Command Line on LinuxRun a Program from the Command Line on Linux
Advertisement

About This Article

Nicole Levine, MFA
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 4,877 times.
How helpful is this?
Co-authors: 3
Updated: October 16, 2023
Views: 4,877
Categories: Linux
Thanks to all authors for creating a page that has been read 4,877 times.

Is this article up to date?

Advertisement