Manage Users and Groups in Ubuntu

As a System Administrator, we have to frequently manage users and their groups. If there is a team working on several projects, then we need to ensure that only authorized users gets access to critical project files. Therefore, one needs to know how to add new users, manage passwords and permissions to access valuable system resources.

Since its something that can be done only by someone who has got administrative rights. So, if you don’t have the required rights then contact your System Administrator for assistance. In this article, we would discuss how we use various utilities to manage users and groups in Ubuntu.

Create a User

useradd and adduser are the two commands that are used to create users. Although, both the commands would lead to similar outcomes still there is difference when it comes to execution. First we cover useradd command.

Open a terminal and enter –

sudo useradd -d /home/<username> -m <username>

It creates a new user. We have used -m option to create a home directory for the user if it doesn’t exists yet.

In addition to creating the user, we also have to provide a password to the user. For that, use passwd command. Hence,

sudo passwd <username>

It ask us to enter same password for the user twice.

We can get the similar results by using the adduser command –

sudo adduser <username>

It prompts us to enter password and other relevant details like Full Name, Phone Number etc by itself. We don’t have to use additional commands like passwd in this case. The adduser command executes following operations the background – it creates a home directory, assigns user and group IDs, copies files from /etc/skel/ directory among others.

Remove a User

To remove a user, use userdel command.

sudo userdel <username>

The above command will not delete the home directory for the user. To remove the home directory as well, use option -r with the command.

sudo userdel -r <username>

So, if you only want to delete the user and keep users` home directory then don’t use -r option with the userdel command. Also, if in case you wanted to delete users` home directory too and forgot to use the -r option. Then, do that with the help of rm command.

Switching Users

To log in to another user’s account, we first log ourselves out and then log in for the specified user. Instead of doing that, we could simply use the su command.

su - <username>

If we are already logged in as super user then, we don’t have to provide user’s password. Else, we have to provide that particular users’ password to log in.

Till now, we have discussed how to create, remove and switch between different user accounts. Next, its about group management.

Managing Groups

One advantage of using Groups is, we can grant or revoke a users’ access to a resource on our server as and when required. We can do this for an entire group as well. Also, a user can be a member of multiple groups.

First we need to know which groups a user is member of, for that use groups command.

groups <username>

To delete a user from the group, use –

sudo groupdel <username>

We will now see how to add a user to a group. We can also decide if that group for the user be primary or secondary.

For a user to be a member of other groups in addition to the group he/she is already a part of, we can do that with the help of secondary groups option –

sudo usermod -aG <groupname> <username>

-a is used to append, so that primary group details remain intact
-G is used for secondary group membership

At times, it is feasible for us to change the primary group of the user itself. To do so, use the following command –

sudo usermod -g <groupname> <username>

We can also remove a user from a group by gpasswd command with -d option,

sudo gpasswd -d <username> <groupname>

In conclusion, we have covered here how to use various command-line utilities in managing users and groups in Ubuntu.

Similar Posts