While working as a System-Administrator of Ubuntu-based Servers, you will have to manage users and associated groups frequently. Few of the reasons could be – new employees joining or leaving the organization. Therefore, you need to know how to add new users, manage passwords and permissions to access valuable resources etc. Before making any changes you need to have superuser privileges otherwise contact your System Administrator for assistance. In this article, we would discuss how we use various utilities in managing users and groups in Ubuntu distribution.
Create a User
useradd and adduser are the two commands that help us create users. Although, both the commands gets us the similar results still they are somewhat different in execution. First we will discuss the useradd command.
So, in terminal type:
sudo useradd -d /home/<username> -m <username>
It will create 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, we will use passwd command. Hence,
sudo passwd <username>
It will ask us to enter password for the user twice.
We can get the similar result by using the adduser command. Pass the following command in the terminal:
sudo adduser <username>
It will prompt us to enter password and other relevant details like Full Name, Phone Number etc by itself. We don’t have to pass 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 particular user, we will use userdel command.
sudo userdel <username>
The above command will not delete the home directory for the user. To remove the home directory too, we need to use the 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.
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, we will discuss group management.
One advantage of using Groups is, we can grant or revoke access to a resource on our server efficiently. That could be easily done through addition and removal of user from a particular group. Also, a user can be a member of multiple groups.
First we need to know which all groups a user is member of, we will be using the groups command.
To delete a group, use
sudo groupdel <username>
We will now be adding a user to a particular group. We can also decide if want that group for the user to be primary or secondary during the process.
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 discussed how we can use various command-line utilities in managing users and groups in Ubuntu distribution.