Updated on October 19, 2022
By Roman Rafacz
How to add users to a group in Linux
How to add a user to a group in Linux, create a new group, add a new user, delete a user, change a password, and modify a user in a Linux group.
Adding a user to a group in Linux is fairly simple once you know the commands. This guide will cover how to accomplish this task, plus a few other useful modifications.
First, let’s cover the key files that are associated with this process in case you are new to Linux or are working on passing a certification exam. Then, we’ll give you a step-by-step guide for each scenario you may encounter.
Modifying Groups in Linux
What is Linux passwd/groups?
It’s important to introduce two files that we will be using as examples of adding users to a group in Linux.
In the /etc directory, the passwd and the group files hold all of the users and group information. These files are essential when logging on to the system, and you'll need to know them if you ever take a Linux exam. Anytime you add a user to a group in Linux, that user is added to the passwd file.
The /etc/group file holds all of the group information, as well as the users belonging to each group. The structure is very similar to that of /etc/password.
Components in the /etc directory and /etc/group file
1. Issue command: less /etc/passwd
2. Use the arrow keys to go up and down, and q to exit.
Viewing the directory and file
3. Edit the file directly or use the commands below. Use the commands, especially if you’re a beginner, to avoid accidentally corrupt the passwd file.
Viewing directory and editing the file
2 Ways to Add a User to a Group in Linux
- Log in as root
- Use the command useradd "name of the user" (for example, useradd roman)
- Use su plus the name of the user you just added to log on.
- "Exit" to log out
- Use the usermod command.
- Replace examplegroup with the name of the group.
- Replace exampleusername with the name of the user you want to add.
Useradd command and su command
How to delete a user from Linux
There are several options to be mindful of as you delete a user from a group in Linux:
- Use the command userdel “name of the user” (for example, userdel roman).
- The command -d sets home directory for the user (if it’s different than the default which is /home/”user’s name”). Using -d on its own will only set the home directory for the user, but does not create it. You can see this is confirmed by “echo $HOME” which tells me my home directory. I use “ls” to confirm.
Using userdel and checking to see if you created a home directory
- Use the command -m to create the home directory.
- If you just add the user, the default directory is /home/“user’s name”. You can just use the -m to create.
- Use the -r command along with userdel to delete the user as well as the home directory.
Using M to create a directory and viewing the default directory
Using the -r command to delete a user and directory
How to Add a Group in Linux
To create a new group in Linux, follow these steps:
- Use the groupadd command.
- Replace new_group with the name of the group you want to create.
- Confirm by checking the /group/etc file (for example, grep software /etc/group or cat /etc/group).
- Run the grep command to confirm.
Using groupadd and confirming creation
If you want to add the user roman to the group “accounting” and make his primary group “sales,” do this:
- Use the command -G for other groups (-G adds the user to a new group but also keeps them in the old one (append).
Note: Make sure you use a capital ( -G ) with this command to append a new group. If you do not, it will assign that group as primary and not add you to other groups.
- Use the command id roman to confirm
Using the -G command for other groups
How to Change a User’s Group in Linux
If a user is already created and you just want to add that user to a group:
- Use the command usermod -Gmanagement “name of user”
If you want to change the primary group a user is assigned to:
- Use the command usermod.
- Replace the examplegroup with the name of the group you want to be the primary.
- Replace the exampleusername with the name of the user account.
If you want to change a user’s home directory:
- Use the command usermod -d/home/newfolder “name of user”.
The command usermod -Gmanagement "name of user"
The command usermod -d/home/newfolder "name of user"
How to Change Passwords in Linux
To change passwords in Linux:
- Login as root.
- Use the command passwd "username" (for example, passwd roman).
If you are logged on as the user, the command is passwd.
Using the command psswd
Roman Rafacz is the product of Western Illinois University’s prestigious academics programs. With a BS in Instructional Technology and a BA in Communications Roman has taken his education and applied it into the vast world of Information Technology. Roman currently works for Jack Morton Worldwide and is a Technical Administrator for the IBM training facility in the Chicago Loop. Roman has been working at Jack Morton for two years and is consistently engulfing himself in different technologies that IBM has to offer. While pursuing higher education through certifications such as A+ and Net + he also enjoys life outside of the computer world with ultimate frisbee, synchronized swimming, and fantasy football.