Adding Linux Users to Groups and More

- select the contributor at the end of the page -
Test your skills. Learn something new. Get help. Repeat. Start a FREE 10-Day trial

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.

Adding users in Linux les /etc/passwd
Adding users in Linux /bin/bash
Adding users in Linux syntax explanation

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:

Linux group file
Linux group file details

 

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.

/etc/password structure in Linux

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)

useradd function in Linux

 

3.    Use su plus the name of the user you just added to log on.

switching users su command in Linux
 
4.    "Exit" will log you out.

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).
userdel "name of user" command in Linux
 
  • 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.
Is command in Linux
 
  • Use the command -m to create the home directory.
-m command in Linux
 
  • If you just add the user, the default directory is /home/“user’s name”. You can just use the -m to create.
using -m to create in Linux

 

  • Use the -r command along with userdel to delete the user as well as the home directory.
using the -r command and userdel command in Linux

 

How to Add a Group in Linux

To create a new group in Linux, follow these steps:

1.    Use the groupadd command.

using the groupadd command in Linux

 

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).

how to add a group in Linux

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).

Using the useradd command in Linux to add user to a 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).

Using the -G command in Linux

 

2.    Use the command id roman to confirm

You can also use -G on its own to add a user to another group.

Using -G in Linux 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”.

usermod -gmanagement command in Linux

 

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”.

example of usermod command in Linux

 

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).

example of changing a password in Linux with passwd command

 

If you are logged on as the user, the command is passwd.

Changing password in Linux

 

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.

Are you ready to learn more about Linux? Read more about Basic Linux Commands for Beginners or sign up for courses through Pluralsight!

Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.

Contributor

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.