In this guide, we will explain how to perform basic user management tasks such as adding, editing, and deleting users in Ubuntu 22.04 server.
If you're running Ubuntu 22.04, at some point you may need to add, modify or delete users on your system. Fortunately, this can be done quite easily using the command line interface, and in this guide, we'll show you how.
Before we begin, make sure you have the following prerequisites:
- A machine running Ubuntu 22.04
- Access to a terminal
- User privileges: root or non-root user with sudo privileges
Adding a New User on Ubuntu 22.04
To add a new user to your Ubuntu system, you'll need to have root or sudo access. Here's the command to add a new user:
sudo adduser <username>
For example, to create a new user named developer
, use the following command:
sudo adduser developer
This command will create a new user with the specified username, and prompt the user to provide a password and some additional information during the process. You can also add additional options to the command to specify a home directory or user groups.
Once the user has been created, you can switch to their account using the su
command:
sudo su <username>
For example, to switch to the developer
account, use the following command:
sudo su developer
Granting a User Sudo Privileges on Ubuntu 22.04
If you want your new user to be able to run commands with root (administrative) capabilities, you must grant them sudo access. There are two approaches to this task: first, adding the user to a pre-defined sudo user group, and second, specifying privileges in sudo's setup on a per-user basis.
Option 1: Adding the New User to the Sudo Group
On Ubuntu 22.04 computers, sudo is set to grant full privileges to any user in the sudo group by default. You can see which groups your new user is a member of by using the groups
command:
groups developer
// Output
developer : developer
A new user is by default only a member of their own group because adduser
creates this in addition to the user profile. Both a user and its own group have the same name. You can use the usermod
command to include the user in a brand-new group. To add a user to sudoers, enter the following command:
sudo usermod -aG sudo developer
Option 2: Specifying Explicit User Privileges in /etc/sudoers
Instead of adding your user to the sudo group, you can use the nano /etc/sudoers
command or visudo
command to open the system's default editor and manually set each user's capabilities in the configuration file /etc/sudoers
.
sudo visudo
This will open file /etc/sudoers
and this will contain
root ALL=(ALL:ALL) ALL
Just add developer ALL=(ALL:ALL) ALL
line to grant sudoers privileges to user developer
.
root ALL=(ALL:ALL) ALL
developer ALL=(ALL:ALL) ALL
For each user who needs to have full sudo access, add a new line like this. Once finished, save and close the document by pressing CTRL + X, followed by Y, and ENTER to confirm.
Modifying a User on Ubuntu 22.04
You can modify a user's account information using the usermod
command. Here are a few examples:
Change the Username
If you need to change a user's username, you can do so with the following command:
sudo usermod -l <new_username> <old_username>
For example, to change the user's username from developer
to john
, use the following command:
sudo usermod -l john developer
Changing Own Password
To change your own password, use the following passwd
command:
passwd
You'll be prompted to enter your current password and then enter your new password.
Changing Password for Other User
If you are a root user or non-root user with sudo privileges, you can change the password for another user. The passwd <username>
command can be used. A sudo privileged user will have to use sudo
before this command:
sudo passwd developer
You'll be prompted to enter the new password for the user.
Change the Home Directory
To change a user's home directory, use the -d
option:
sudo usermod -d /path/to/new/directory <username>
This command will change the user's home directory to the specified path.
Add User to a Group
To add a user to an existing group, use the -aG
option:
sudo usermod -aG <group_name> <username>
This command will add the user to the specified group.
Changing the Shell
Most modern systems have a default user bash
shell. However, this can vary based on the Linux distribution or the user’s environment. To change the shell for a user:
sudo chsh <username>
Changing User Information
To change the details for a user (for example full name, room number, etc.):
sudo chfn <username>
Deleting a User on Ubuntu 22.04
If you need to delete a user from your Ubuntu system, you can do so with the following command:
sudo deluser <username>
By default, deluser
will remove the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the --remove-home
option:
sudo deluser --remove-home <username>
The --remove-all-files
option removes all files on the system owned by the user. Note that if you activate both options --remove-home
will have no effect because all files including the home directory and mail spool are already covered by the --remove-all-files
option:
sudo deluser --remove-all-files <username>
If you previously configured sudo privileges for the user you deleted, you may want to remove the relevant line again by entering vissudo
or nano /etc/sudoers
.
root ALL=(ALL:ALL) ALL
developer ALL=(ALL:ALL) ALL # Delete this line
This will prevent a new user created with the same name from accidentally being given sudo
privileges.
Forcefully Delete a User on Ubuntu 22.04
If you get the error while deleting the user userdel: user <username> is currently used by process XXXX
then enter below command to kill the process and then delete the user.
kill -9 <XXXX>
sudo deluser --remove-home <username>
Or you can try below command for deleting the user:
sudo killall -u <username> && sudo deluser --remove-home -f <username>
Conclusion
Managing users on your Ubuntu 22.04 server is an uncomplicated process that can be achieved through the command line interface. This guide provides step-by-step instructions that can help you add, edit, and delete users on your system with ease. However, it's crucial to exercise caution when performing these actions as a single mistake can lead to severe consequences. By following the instructions carefully, you can prevent issues and manage your users effortlessly.
We hope you find this guide helpful, and for more information on Ubuntu, including tips, tricks, and tutorials, be sure to check out Rexposed, your ultimate resource for all things Ubuntu.