Introduction
Managing user accounts on a Linux system is a fundamental aspect of system administration. By default, when you set up a new Ubuntu system, you typically have access only to the root account. However, running daily tasks as the root user can be risky. It’s recommended to create unprivileged user accounts for these tasks. Additionally, each user should have their own separate account. In Ubuntu, you can utilize the sudo
tool to grant administrative privileges when needed. This guide will walk you through creating user accounts, assigning sudo privileges, and deleting users on Ubuntu 22.03.
Prerequisites
To follow this tutorial, you should have access to a server running Ubuntu 22.03. Ensure that you have root access to the server and have the firewall enabled. If you haven’t set up your server, consider following our “Initial Server Setup Guide for Ubuntu 22.03.”
Adding a User
If you are logged in as the root user, you can create a new user at any time using the following command:
adduser newuser
If you are logged in as a non-root user with sudo privileges, add a new user with the following command:
sudo adduser newuser
You’ll be prompted to set a password for the new user and provide optional additional information. Confirm the details, and the new user will be ready for use.
Granting a User Sudo Privileges
To allow a new user to execute commands with root (administrative) privileges, you need to grant them access to sudo
. There are two methods for achieving this:
Adding the New User to the Sudo Group
By default, Ubuntu 22.03 configures sudo
to grant full privileges to any user in the sudo group. You can check the groups a user belongs to with the groups
command:
groups newuser
If the new user is only in their own group (with the same name as the user), you can add them to the sudo group using usermod
:
sudo usermod -aG sudo newuser
Alternatively, you can use the following command if you are a non-root user with sudo privileges:
sudo usermod -aG sudo newuser
Specifying Explicit User Privileges in /etc/sudoers
You can also specify sudo privileges on a per-user basis by editing the /etc/sudoers
file using visudo
. This method is recommended because it locks the file against simultaneous edits and performs validation checks on its contents.
sudo visudo
Edit the /etc/sudoers
file to specify privileges for the new user:
newuser ALL=(ALL:ALL) ALL
Repeat this for each user requiring sudo privileges. Save and exit the file.
Testing Your User’s Sudo Privileges
The new user can now execute commands with administrative privileges by using sudo
. For example:
sudo some_command
You will be prompted to enter the new user’s password.
Deleting a User
To remove a user account without deleting their files, use the following command as the root user:
deluser newuser
If you are a non-root user with sudo privileges, use:
sudo deluser newuser
If you want to delete the user’s home directory as well, use this command:
deluser --remove-home newuser
If you previously configured sudo privileges for the user, remove the relevant line from /etc/sudoers
using visudo
.
You should now be proficient in adding and deleting users on your Ubuntu 22.03 system. Effective user management is crucial for maintaining security and access control.