The sudo (substitute user do or superuser do) command is a program that allows users to run commands as another user, by default the superuser ie root.
The sudo command is mostly used to perform administrative tasks by regular users executing commands as the root user.
In this post, we see how to create a user with sudo privileges on CentOS, Ubuntu & Debian.
Prerequisites
Contents
Log in as the root user or switch to the root user.
su -
In Debian / Ubuntu, install the sudo application using the apt command.
apt update apt install -y sudo
Create User and Give sudo Access
Create a sudo user
Create a new user account for sudo access. If you want to give sudo access to an existing user, please skip to adding a user to the sudo group.
useradd -c "sudo user" -m -d /home/user_name user_name
The above command creates a user called user_name with the home directory /home/user_home and comment as sudo user.
Replace user_name with the username you want to create.
Set Password
Set the password for the new user account.
passwd user_name
Add user to the sudo group
The members of group wheel on CentOS and sudo on Ubuntu / Debian are allowed to run commands with sudo. Use the usermod command to add a user account to the respective group.
CentOS
usermod -aG wheel user_name
Ubuntu / Debian
usermod -aG sudo user_name
Test the sudo access
Switch to the newly created user.
su -l user_name
Verify that the sudo access is working as expected
sudo -l
You will be asked to enter the user’s password the first time you do sudo in a session.
Output:
Matching Defaults entries for user_name on server: !visiblepw, always_set_home, match_group_by_gid, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin:/bin:/usr/sbin:/usr/bin User user_name may run the following commands on server: (ALL) ALL
The above output confirms that user user_name can run any command as the root user.
Use sudo
To run a command with sudo privileges, just prefix the sudo in your command.
Syntax:
sudo your_command
Example:
sudo blkid
You will be asked to enter the user’s password the first time you do sudo in a session.
Output:
/dev/sda1: UUID="60a496d0-69f4-4355-aef0-c31d688dda1b" TYPE="xfs" /dev/sda2: UUID="Q9pSx3-bNi4-88ah-1Wa5-jLLs-7POm-ytlfUr" TYPE="LVM2_member" /dev/mapper/centos-root: UUID="63e5ad04-38ef-4ce2-857b-0197cdb7d582" TYPE="xfs" /dev/mapper/centos-swap: UUID="68cb3801-13e8-47da-bbfa-8389aab3836b" TYPE="swap" /dev/mapper/centos-home: UUID="187abb36-b5b3-48cd-8cbb-baad6dbb0dc5" TYPE="xfs"
Conclusion
That’s All. You have learned how to create a user with sudo privileges on Linux. Now, you will be able to run root’s administrative commands with this new user. Please provide your feedback in the comments section.