System administrators and those who administer systems based on Ubuntu must perform the primary duty of adding and deleting users on Ubuntu 20.04. You can ensure secure and efficient management of your system by setting up user accounts that can be used to grant certain rights and access to various users. Hope we will help by adding and deleting users on Ubuntu 20.04. A step-by-step so you can manage user accounts and uphold system security. Increasing your productivity in Linux with an improved shell and integrating Zsh into your Ubuntu environment by pursuing steps can be a game-changer.
Running as the root user provides you total authority over a system’s users, but it can also be risky and even harmful. Adding an unprivileged user and performing typical system administration operations without root access is preferable. For any additional non-privileged users you might have on your system, you can also create new accounts. On a system, every user ought to have a unique account.
On Ubuntu systems, a program named 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 manual.
Adding User
You can create a new user at any time by executing the following command while logged in as the root user:
# adduser newuser
With the following command, you can add a new user if you are logged in as a non-root user and have sudo access:
$ sudo adduser newuser
In either case, you must respond to the following inquiries:
- Give the new user a password, and then confirm it.
- Add any more details you have about the new user. If you don’t want to use these fields, you can skip over them by using the ENTER key.
- Finally, you’ll be prompted to affirm that the data you supplied was accurate. Press Y to proceed.
Now that your new user is ready to use, you can log in. Continue to the following section if your new user requires 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 methods 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.
Making the New User a Member of the Sudo Group
On Ubuntu 20.04 computers, sudo is set up by default to give every member of the sudo group full privileges.
With the group’s command, you may see which groups your new user belongs to:
$ groups newuser
Because adduser produces this in addition to the user profile, a new user initially just belongs to their own group. A user has the same name as the group it belongs to. You can use the usermod command to include the user in a new group:
# usermod -aG sudo newuser
The -aG option instructs usermod to include the specified user in the groups.
Please be aware 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 to the sudo group. In the latter scenario, you will need to use sudo before this command, like in the following example:
$ sudo usermod -aG sudo newuser
Adding explicit user privileges to the sudoers file in /etc
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.
Because it locks the file against multiple concurrent edits and validates the contents of the file before overwriting it, visudo is the sole method advised for making changes to /etc/sudoers. By doing this, you can avoid accidentally misconfiguring sudo and being unable to fix the issue because you no longer have sudo rights.
Run the following commands if you are currently logged in as root:
# visudo
Run the same command with the sudo prefix if you are logged in as a non-root user with sudo privileges:
$ sudo visudo
Traditionally, the vi editor was opened when visudo opened /etc/sudoers, which can be confusing for novice users. On fresh Ubuntu installations, visudo will automatically switch to the more practical and user-friendly nano text editor. Locate the line that reads as follows using the arrow keys to move the cursor:
Include the highlighted line after this one. Make careful 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. Press CTRL + X, Y, and ENTER to confirm your decision to save and dismiss the file when you are finished.
Testing the Sudo Privileges of Your User
Your newly created user can now issue commands with administrative rights.
You can run commands as your regular user whilst logged in as the new user by typing the following commands:
$ some_command
By adding sudo before the command, you can run the identical operation with administrative rights:
$ sudo some_command
You will be required to enter the password for the ordinary user account you are currently signed in to when you do this.
The user To be Deleted
It’s wise to delete the 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:
# deluser newuser
You would use the following if you were logged in as a non-root user with sudo privileges:
$ sudo deluser newuser
Instead, you can use the following command to have root erase the user’s home directory when the user is deleted:
# deluser --remove-home newuser
The identical command would be entered with the sudo prefix if you were running it as a non-root user with sudo privileges:
$ sudo deluser --remove-home newuser
You might wish to remove the pertinent line once more if the user you deactivated had sudo rights previously configured:
# visudo
Alternatively, if you have sudo rights and are a non-root user, run the command below:
$ sudo visudo
This will stop an unintentional grant of sudo privileges to a newly formed user with the same name.
Conclusion
The ability to manage user accounts on Ubuntu 20.04 is crucial for system administrators and anyone in charge of system security. You can quickly create and delete users and grant or revoke access as necessary by following the guidelines in this manual. Always use caution while carrying out administrative duties, and confirm that you have the required rights and permissions before making any alterations.
You can efficiently regulate access and uphold a secure environment on your Ubuntu 20.04 system with the help of these user management skills. 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 may divide users and grant them only the access necessary for them to do their tasks if you have effective user management.