1. Overview

In this tutorial, we’re going to learn how to create, modify, and delete users and groups in Linux using the terminal. In addition, we’ll learn how to add a user to a group, how to remove one from a group, how to list all users, and how to get more information about the existing users on a Linux machine.

2. List All Users

The file /etc/passwd contains all registered users as well as information about them:

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
daemon,,,:/var/run/pulse:/usr/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
baeldung:x:1000:1000:,,,:/home/baeldung:/bin/bash

/etc/passwd lists users in this format:

username:x:user id: group id: , , , :/home/username:/bin/bash

Each user has its own UID. 0 is root. 1 to 999 are system users, and from 1000 onward are local users.

3. See User and Group IDs

Linux has a command, id, that prints user and group IDs for the specified user:

$ id baeldung
uid=1000(baeldung) gid=1000(baeldung) groups=1000(baeldung),27(sudo)

We can see what groups the specified user is in.

4. Create a New User

To create a new user in Linux, we can use the useradd command:

$ sudo useradd --create-home new_user

In addition, we can add the –create-home option to create a home directory for the new user.

5. Add/Change User Password

We can use passwd to make a new password for a user or to change a user’s password:

$ sudo passwd new_user
[sudo] password for baeldung:             
New password: 
Retype new password: 
passwd: password updated successfully

The new user that we created didn’t have a password. As a result, passwd made one for it.

6. Modify User

usermod can modify a user account.

6.1. Change Primary Group

We can add the -g option to change the main group of a user account:

$ id new_user
uid=2027(new_user) gid=2027(new_user) groups=2027(new_user)
$ sudo usermod -g baeldung new_user
$ id new_user
uid=2027(new_user) gid=1000(baeldung) groups=1000(baeldung)

The new group replaces the previous group.

6.2. Change UID

We can add the -u option to change the user ID of an existing user account:

$ id new_user
uid=2027(new_user) gid=1000(baeldung) groups=1000(baeldung)
$ sudo usermod -u 2030 new_user
$ id new_user
uid=2030(new_user) gid=1000(baeldung) groups=1000(baeldung)

The command has changed the UID from 2027 to 2030.

6.3. Change Login Name

The -l option helps us change the login name of an existing user account:

$ id new_user
uid=2030(new_user) gid=1000(baeldung) groups=1000(baeldung)
$ sudo usermod -l new_name new_user
$ id new_name
uid=2030(new_name) gid=1000(baeldung) groups=1000(baeldung)

The command has changed the login name from new_user to new_name.

7. Delete a User

We can use userdel to delete an existing user. Adding the -r option will make userdel delete the user’s home directory along with its contents, as well as the user’s mail spool:

$ sudo userdel -r new_user
[sudo] password for baeldung:             
userdel: new_user mail spool (/var/mail/new_user) not found
userdel: new_user home directory (/home/new_user) not found

This user didn’t have a mail spool or a home directory defined for it. So, -r was unnecessary.

8. Add User to Group

To add a user to a group, we can use gpasswd -a:

$ id new_user
uid=2027(new_user) gid=2027(new_user) groups=2027(new_user)
$ sudo gpasswd -a new_user baeldung
Adding user new_user to group baeldung
$ id new_user
uid=2027(new_user) gid=2027(new_user) groups=2027(new_user),1000(baeldung)

The command has appended the specified group to new_user‘s groups.

9. Remove User From Group

We can use gpasswd -d to remove a user from a group:

$ id new_user
uid=2027(new_user) gid=2027(new_user) groups=2027(new_user),1000(baeldung)
$ sudo gpasswd -d new_user baeldung
Removing user new_user from group baeldung
$ id new_user
uid=2027(new_user) gid=2027(new_user) groups=2027(new_user)

The command has removed new_user from the specified group.

10. Create a New Group

We can use groupadd to create a new group:

$ sudo groupadd new_group
$ cat /etc/group | grep new_group
new_group:x:2028:

The command has created a new group, and its group ID is 2028.

11. Modify Group

We can use groupmod to modify a group.

11.1. Change GID

We can add the -g option to change the group ID of an existing group:

$ cat /etc/group | grep new_group
new_group:x:2028:
$ sudo groupmod -g 2040 new_group
$ cat /etc/group | grep new_group
new_group:x:2040:

The command has changed the group ID from 2028 to 2040.

11.2. Change Group Name

To change a group’s name, we can add the -n option:

$ cat /etc/group | grep new_group
new_group:x:2040:
$ sudo groupmod -n new_name new_group
$ cat /etc/group | grep new_name
new_name:x:2040:

The command has changed the group’s name from new_group to new_name.

12. Delete a Group

To remove an existing group, we can use groupdel:

$ sudo groupdel new_group

13. Conclusion

To sum up, we learned how to manage users and groups, as well as how to get more information about users on a Linux machine.