Are you trying to create a new user with sudo access on Ubuntu 20.04 LTS Linux server?
This guide will help you out.
With the sudo command, you can execute any or selected command as another user as specified by the security policy by the system administrator.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Software Installation on their Ubuntu Servers.
In this context, you shall learn how to create a sudo user on Ubuntu 20.04 LTS server.
Now, let us see all steps to create a new user account named "user1" and give it sudo access to run all commands. In other words, the user1 user is going to be a sysadmin.
To begin, start by using an ssh tool such as putty to log into your Server as the root user. To do this, execute;
ssh root@your-server-ip-here
ssh root@server1.linuxapt.com
In this step, add a new user named "user1" with the adduser command as shown below;
adduser user-name-here
adduser user1
Note that you need to provide the new user password as other information as prompted on the screen. When done entering all information, make sure you type "Y" to "Is the information correct?" prompt. You can press [ENTER] key for the default except for the password prompt.
Since you have added a new user named user1 and password, you need to add the user1 user account to the sudo group. All members of the sudo group granted sudo and root level access on Ubuntu and Debian based systems. Therefore to add the user1 user you just created to the sudo group, execute any one of the following command;
adduser user1 sudo
OR
usermod -aG sudo user1
You will get an output such as this;
Adding user 'user1' to group `sudo' ...
Adding user user1 to group sudo
Done.
Now, user user1 can login using the ssh command command as follows from their own desktop;
ssh user1@server1.linuxapt.com
ssh user1@server-ip-here
Since this is the first time of using sudo command, you will be promoted for the password of the user1 user account. Hence, type the user1's password to gain root access. Any command type with sudo should run with root privileges for the user1 account. To gain root shell, execute:
sudo -s
To verify that user1 can use the sudo command, execute:
sudo ls -l /root/
To run a command using sudo, simply run the command shown below;
sudo [option] command
sudo -i
sudo -s
sudo systemctl restart nginx.service
sudo tail -f /var/log/rsnapshot.log
Here the -i option run the shell specified by the target user’s password database entry as a login shell. This means that login-specific resource files such as .profile, .bash_profile or .login will be read by the shell. The -s executes the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user’s password database entry. If a command is specified, it is passed to the shell for execution via the shell’s -c option.
We can set sudo for an existing user account too. In other words, add an existing user named "user5" to sudo using the following syntax. First, make sure user account named user5 exist using the grep command and /etc/passwd file:
grep '^username-here' /etc/passwd
grep '^user5' /etc/passwd
Next, add user to the sudo group as shown below;
adduser {exiting-user-name} sudo
adduser user5 sudo
To verify it, use the id command as shown below;
id user5
The output will tell you that the user belongs to sudo.
To do this, simply execute;
deluser USERNAME sudo
deluser user5 sudo
id user5
You will see an output such as this;
Removing user 'user5' from group `sudo' ...
Done.