This guide will walk you through the simple steps of adding, editing, and deleting users in Ubuntu 20.04 server.
If you're using Ubuntu 20.04, you might need to add or delete users on your system at some point. This can be done easily through the command line interface, and in this guide, we'll walk you through the process.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A machine running Ubuntu 20.04
- Access to a terminal
- User privileges: root or non-root user with sudo privileges
How to Add a New User on Ubuntu 20.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 the sake of this tutorial let's assume we will create a new user named developer
sudo adduser developer
This command will create a new user with the specified username. The user will be asked 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>
In our case we can enter following command to switch to developer
's account.
sudo su developer
How to Grant a User Sudo Privileges on Ubuntu 20.04
If you want your new user to be able to run commands with root (administrative) capabilities, you must grant them sudo access. Let's look at 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 20.04 computers, sudo is set to grant full privileges to any user in the sudo group by default.
With the groups
command, you can see which groups your new user is a member of:
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 user to sudoers enter 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
// file: /etc/sudoers
root ALL=(ALL:ALL) ALL
Just add developer ALL=(ALL:ALL) ALL
line to grant sudoers privileges to user developer
.
// file: /etc/sudoers
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.
How to Modify a User on Ubuntu 20.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>
This command will change the user's username from the old value to the new value. In our case we can change the username developer
to john
sudo usermod -l john developer
Changing Own Password
Type following passwd command command to change your own password:
passwd
The following prompt will appear where you have to enter Current Password and then enter New Passwords to set a new password.
// Output
Changing password for developer.
Current password:
New password:
Retype new password:
The password has been changed successfully.
Changing Password for Other User
If you are a root user or non-root user with sudo privileges, you can change password for other user. The commad is passwd <username>
, sudo privileged user will have to use sudo
before this command:
sudo passwd developer
The following prompt will appear where you have to enter New Passwords to set a new password for the user.
// Output
Changing password for developer.
New password:
Retype new password:
The password has been changed successfully.
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
though it can vary based on Linux distribution or the user’s environment. To change the shell for a user:
sudo chsh <username>
Changing the User Information
To change the details for a user (for example full name, room number, etc.):
sudo chfn <username>
How to Delete a User on Ubuntu 20.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
.
// file: /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 being accidentally given sudo
privileges.
Conclusion
Adding and deleting users on Ubuntu 20.04 is a straightforward process, and can be done quickly using the command line interface. By following the steps outlined in this guide, you can easily manage the users on your system. Remember to always use caution when adding or deleting users, as these actions can have significant consequences if done incorrectly.
We hope you found this guide useful. For more information on Ubuntu, including tips, tricks, and tutorials, be sure to check out Rexposed, your go-to source for all things Ubuntu.