The sudo command allows us to run any or selected command as another user as specified by the security policy by the system administrator. This page explains how to create a sudo user on Ubuntu 20.04 LTS server.
Procedure to create a sudo user on Ubuntu 20.04 LTS
Let us see all steps to create a new user account named wendy and give it sudo access to run all commands. In other words, the wendy user is going to be a sysadmin.
Step 1 – Log in to the server using ssh.
Use the ssh command to log in as root user:
ssh root@your-cloud-server-ip-here
ssh root@server1.cyberciti.biz
Step 2 – Create a new user account on Ubuntu.
Next use the adduser command add a user named wendy, enter:
adduser user-name-here
adduser wendy
Please 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.
Step 3 – Add the new user to the sudo group.
Now we have added a user named wendy with a password. It is time to add the wendy 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 wendy user you just created to the sudo group, run any one of the following command:
adduser wendy sudo
OR
usermod -aG sudo wendy
Sample outputs:
Adding user `wendy' to group `sudo' ... Adding user wendy to group sudo Done.
Step 4 – Test sudo user account.
Now user wendy can login using the ssh command command as follows from their own desktop:
ssh wendy@server1.cyberciti.biz
ssh wendy@server-ip-here
The first time you use the sudo command, you will be promoted for the password of the wendy user account. Hence, type the wendys password to gain root access. Any command type with sudo should run with root privileges for the wendy account. To gain root shell, enter:
sudo -s
Verify that wendy can use the sudo command:
sudo ls -l /root/
Step 5 – How to run a command using sudo
The syntax is:
sudo [option] command
sudo -i
sudo -s
sudo systemctl restart nginx.service
sudo tail -f /var/log/rsnapshot.log
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.
Ubuntu 20.04 add sudo user (existing users)
We can configure sudo for an existing user account too. In other words, add an existing user named vivek to sudo using the following syntax. First, make sure user account named vivek exist using the grep command and /etc/passwd file:
grep '^username-here' /etc/passwd
grep '^vivek' /etc/passwd
Now add that user to the sudo group:
adduser {exiting-user-name} sudo
adduser vivek sudo
Verify using the id command:
id vivek
A note about removing sudo access
Simply run the following deluser command:
deluser USERNAME sudo
deluser wendy sudo
id wendy
Sample outputs:
Removing user `wendy' from group `sudo' ... Done.
Conclusion
You learned how to add a user account with access and privileges to run system administrator’s commands on Ubuntu 20.04 LTS systems. See sudo command man page here for more info.
ð§ Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter ⢠Facebook ⢠0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |