How to add and delete users on Ubuntu 20.04?
Overview
One of the most critical system administration tasks to become proficient at is adding and removing users from a Linux system. You often get access to the root account by default when you build a new system.
Running as the root user provides you total authority over the users and the system, but it is also risky and might be harmful. It is preferable to add an unprivileged user and perform routine system administration operations under that user's control rather than as root.
On Ubuntu systems, a tool called sudo is installed for tasks that demand administrator rights. In a nutshell, sudo enables you to execute commands as another user, including one who has administrative rights. You'll discover how to establish user accounts, grant sudo rights, and remove users in this tutorial.
Prerequisites
There are certain prerequisites that need to be met before you begin:
Ubuntu version 20.04 or any other Linux equipped system
A regular user (non-root) having sudo privileges
Firewall enabled
Internet connection
Adding a User
If you are a root user: Execute the following command to create a new user if you are logged in as the root user:
If you are non-root user: Execute the following command to add a new user if you are logged in as a non-root user and have sudo access:
In both cases, answer the following questions:
Set a password for the new user and confirm it.
Enter any additional details you have regarding the new user. If you choose not to use these fields, you can skip it by pressing ENTER.
You'll be prompted to check that the data you supplied was accurate one last time. To proceed, press Y.
With the password you provided, you may now access your new user and log in.
Continue to the following stage if you require your new user to have administrative rights.
Providing Sudo Privileges to a User
You must grant the new user access to sudo if you want them to be able to run commands with root (administrative) privileges. Let's look at two approaches for doing this task: first, adding the user to a sudo user group that has already been created, and second, configuring sudo to specify privileges on a per-user basis.
a. Making the New User a Member of the Sudo Group
Any user in the sudo group is given full rights by default when using sudo on an Ubuntu 20.04 system. The groups command allows you to see which groups your new user is a part of:
A new user is by default only a member of their own group because adduser produces this in addition to the user profile. The name of a user is also the name of its own group. Use the following command to include the user in a brand-new group:
The -aG
option instructs usermod
to include the user in the specified groups.
Note that the usermod
command itself needs sudo access. As a result, you can only add users to the sudo group if you are logged in as either the root user or another user who has already been assigned as a member to the sudo group. In the latter scenario, you will need to use sudo before this command, like in the following example:
b. Adding explicit user privileges to the in /etc/sudoers
Instead of adding your user to the sudo group, you can use the visudo command to open the system's default editor and manually set each user's capabilities in the configuration file /etc/sudoers.
Run the following command if you are currently logged in as root:
Run the same command with the sudo prefix if you are logged in as a non-root user with sudo privileges:
On new Ubuntu systems, visudo
will automatically switch to the more practical and user-friendly nano text editor. Locate the line that looks like this using the arrow keys:
Include the highlighted line after this one. Make sure to replace newuser with the name of the user profile you want to provide sudo access to
Each user that needs to have full sudo access should have their own line added, like this. Once finished, save and close the document by clicking CTRL + X, then press Y, and at last ENTER to confirm.
Now, you are done providing sudo priveleges to a user. Let's look at how to delete a user.
Deleting a User
It's beneficial to delete an old account if you decide you don't need the user any longer.
Run the following command as root to delete the user without removing any of their files:
If you are signed in as another non-root user with
sudo
privileges, you would use the following:
You can use the following command as root if you'd rather remove the user's home directory as well when the user is deleted:
You should issue the same command with the sudo prefix if you were running this as a non-root user with sudo privileges:
You might wish to remove the sudo rights for the account you deleted. Use following command to do this as a root user.
Alternatively, if you have sudo rights and are a non-root user, run the command below:
This will stop an unintentional grant of sudo privileges to a newly formed user with the same name.
Conclusion
Now that you know how to add and remove users from your Ubuntu 20.04 system, you should be able to do it with relative ease. You can divide users and grant them only the access they need to perform their jobs if you have effective user management.
Last updated