How To Add A User To A Group In Linux: A Step-By-Step Guide
Linux may not be as commonplace at the consumer level as operating systems like Windows or macOS, but there are several reasons why people who use it sing its praises. It's a highly customizable interface, and that extends to user profiles and groups.
As you might expect, user profiles in Linux can be tailored to the needs of their assigned users — which includes determining what a given user is allowed to access within the system. In other words, it's kind of like a custom clearance level you can set up for individuals (or even multiple profiles for the same person, if you want) that determine what files and information they can access.
Groups serve a similar purpose, but work in a more overarching sense. So rather than going in and changing permissions for individual user accounts one-by-one, you can set up a group with predefined permissions and then assign various users to said group in order to give them all the same level of access (and restrictions). A user can be added to more than one group, too, so if you need to give someone more permissions than one group allows you can use another group to make up the difference.
Managing groups
All of your work in creating and managing groups, assigning user profiles, and so on can be done using Linux's terminal window. Here's how.
- Open the terminal window by pressing Ctrl + Alt + T on the keyboard. Alternatively, you can open the Applications menu and click on Terminal.
- Type in the "groupadd" command, followed by the name of the group you want to create. For example, "groupadd newgroup" or something similar.
- Group permissions can be managed one folder at a time (though it takes longer), but you can use the "setfacl" command to manage multiple permissions at once.
- The setfacl command looks something like "setfacl OPTION X:NAME:Y /DIRECTORY" with OPTION standing in for the various symbolic commands, NAME standing in for the user or group name you want to adjust, and replacing DIRECTORY with the name of the folder you want to change permissions for.
- To give the example newgroup permission to view (but not edit) a directory — for example, CATS — type in "sudo setfacl -m g:newgroup:rx -R /CATS" where "-m" tells terminal to modify the settings, "g" denotes a group, and "rx -R" sets permissions to read without modification.
- To give another group both read and write permissions of the same directory, enter "sudo setfacl -m g:newgroup2:rwx -R /CATS" with "rxw -R" granting read and write permissions to newgroup2, but not newgroup.
You can reuse this process and various other permission commands for any and all users, groups, and directory combinations you want.
Adding or moving users to groups
With your groups and permissions set up, managing individual user profiles across various groups should be a breeze.
- Open the terminal window by pressing Ctrl + Alt + T on the keyboard. Alternatively, you can open the Applications menu and click on Terminal.
- To add a user to a group, type in "usermod -a -G GROUP USER" replacing GROUP with the name of the group you want and USER with the actual user profile name.
- If a user profile is already in a primary group, and you need to change to a different one, type in "usermod -g GROUP USER" and replace GROUP with the name of the group you want to change to, and USER with the user profile name. Note that using a "-g" tells Linux you're changing the primary group, while a "-G" is used for a secondary group.
- To assign the same user profile to multiple groups at once, type in "usermod -a -G GROUP1,GROUP2,GROUP3 USER" but replace each GROUP with the name of the groups you want, and replace USER with the user profile name.
- You can also create a user profile and add the new profile to a group with one command by typing in "useradd -G GROUP USER" and replacing GROUP with the group name and USER with the user profile name.
If you ever need a reminder of what groups you already have made, you can type in "getent group" to pull up a complete list.