Adding Users to Linux Groups and User Management
- select the contributor at the end of the page -
Knowing how to use Linux to add a user to a group is fairly simple once you know the commands. Whether you need to do any of the following, we’ve got your answer:
- Add a user to Linux
- Delete a user in Linux
- Add a new group to Linux
- Add a user to a group in Linux
- Change a password
- Modify a user in a Linux group
But 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.
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. Anytime you add a user to a group in Linux, that user is added to the passwd file.
Here’s a look at /etc/passwd:
1. Issue command: less /etc/passwd
2. Use the arrow keys to go up and down, and q to exit.
3. Edit the file directly or use the commands below. It’s recommended to use the commands, especially if you’re a beginner, because you don’t want to accidentally corrupt the passwd file.
Here's the group 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.
Again, these files are vital to the system and you will need to know them if you are taking any Linux exams.
How to Add a User to Linux
Follow these steps to add an existing user to a group in Linux:
1. Log in as root
2. Use the command useradd "name of the user" (for example, useradd roman)
3. Use su plus the name of the user you just added to log on.
Here is another option for adding a user to a group in linux:
1. Use the usermod command.
2. Replace examplegroup with the name of the group.
3. Replace exampleusername with the name of the user you want to add.
How to Delete a User From Linux
There are four options for deleting 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.
- 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.
How to Add a Group in Linux
To create a new group in Linux, follow these steps:
1. Use the groupadd command.
2. Replace new_group with the name of the group you want to create.
3. Confirm by checking the /group/etc file (for example, grep software /etc/group or cat /etc/group).
4. Use the groupdel command to remove the group entirely.
How to Add a User to a Group in Linux
To add the user roman to the group “accounting,” do the following:
1. Use the command useradd -gaccounting roman (-g changes the user’s primary group).
2. Run the grep command to confirm.
If you want to add the user roman to the group “accounting” and make his primary group “sales,” do this:
1. Use the command -G for other groups (-G adds the user to a new group but also keeps them in the old one (append).
2. Use the command id roman to confirm
You can also use -G on its own to add a user to another group.
Note: The group must exist before users can be added to it.
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”.
How to Change Passwords in Linux
To change passwords in Linux:
1. Login as root.
2. use the command passwd "username" (for example, passwd roman).
If you are logged on as the user, the command is passwd.
Learn More About Adding Users and Groups to Linux
When working in Linux, there are a number of options you have for creating users and groups. For instance, you could just go into /etc/passwd directly and add a user there, but unless you are familiar with file editors and putting a lock on, you should work with the commands to avoid corruption.