In this blog post, we will explain to you how to create a sudo user on Ubuntu 22.04 OS.
Sudo stands for either “substitute user do” or “super user do” and allows normal users to elevate the current user account to have root privileges temporarily. The difference between the sudo user and the root user is that the root user has id 0, has unlimited to the system, and can modify ANY file on it.
Creating a sudo user and granting privileges is a straightforward process. Let’s get started!
Table of Contents
Prerequisites
- A server with Ubuntu 22.04 as OS
- User privileges: root user
Step 1. Update the System
Before doing anything on the server, update the system packages to the latest versions available.
sudo apt-get update -y && sudo apt-get upgrade -y
Step 2. Create New System User
To create a new user, execute the following command:
adduser developer
Now, you will be asked to set the user’s password twice and to fill in some non-required fields for additional info about the user.
Adding user `developer' ... Adding new group `developer' (1000) ... Adding new user `developer' (1000) with group `developer' ... Creating home directory `/home/developer' ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully Changing the user information for developer Enter the new value, or press ENTER for the default Full Name []: RoseHosting Developer Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y
Now, the user called developer is created successfully in the system. To check this you can execute the following command:
root@host:~# cat /etc/passwd | grep developer
You should receive the following output:
developer:x:1000:1000:RoseHosting Developer,,,:/home/developer:/bin/bash
Step 3. Adding User to Sudo group
Next is to add the User to a sudo group. This way, we are granting privileges to normal users with root privileges.
usermod -aG sudo developer
Once the developer user is added to the sudo group, we will test with real examples. First, switch to the developer user with the command below:
su - developer
The command prompt should look like this:
root@host:~# su - developer To run a command as administrator (user "root"), use "sudo". See "man sudo_root" for details. developer@host:~$
Now, we can test the sudo user with the following commands:
sudo ls -alh /root
You will need to enter the password for the developer user
developer@host:~$ sudo ls -alh /root/ [sudo] password for developer:
Once the password is entered, you will see the content of the /root directory.
root@host:~# su - developer developer@host:~$ sudo ls -alh /root/ [sudo] password for developer: total 40K drwx------ 4 root root 4.0K Feb 10 16:36 . drwxr-xr-x 19 root root 4.0K Feb 10 16:35 .. -rw------- 1 root root 412 Feb 15 06:10 .bash_history -rw-r--r-- 1 root root 57 Feb 10 16:35 .bash_profile -rw-r--r-- 1 root root 3.1K Dec 5 2019 .bashrc drwx------ 2 root root 4.0K May 11 2020 .cache -rw-r--r-- 1 root root 161 Dec 5 2019 .profile drwx------ 2 root root 4.0K Feb 10 16:35 .ssh -rw------- 1 root root 3.4K Jul 30 2022 .viminfo -rw-r--r-- 1 root root 174 Feb 10 16:35 .wget-hsts
Now, you will be able to modify any file on the system since the user developer is added to the sudo group. Let’s execute the following command:
sudo cat /etc/sudoers
You will be able to see the content of the sudoers file:
# # This file MUST be edited with the 'visudo' command as root. # # Please consider adding local content in /etc/sudoers.d/ instead of # directly modifying this file. # # See the man page for details on how to write a sudoers file. # Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" Defaults use_pty # This preserves proxy settings from user environments of root # equivalent users (group sudo) #Defaults:%sudo env_keep += "http_proxy https_proxy ftp_proxy all_proxy no_proxy" # This allows running arbitrary commands, but so does ALL, and it means # different sudoers have their choice of editor respected. #Defaults:%sudo env_keep += "EDITOR" # Completely harmless preservation of a user preference. #Defaults:%sudo env_keep += "GREP_COLOR" # While you shouldn't normally run git as root, you need to with etckeeper #Defaults:%sudo env_keep += "GIT_AUTHOR_* GIT_COMMITTER_*" . . . . . . . . . .
This time the password was not required since we already entered it once, and this is proof that this developer user has root privileges.
That’s it. You successfully created a sudo user on Ubuntu 22.04 with permissions. If you find this difficult, you can always contact our technical support, and they will help you with any aspect of this. All you need to do is to sign up for one of our NVMe VPS hosting plans and submit a support ticket. We are available 24/7.
If you liked this post about creating a sudo user on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.