This guide we will be focusing on how we can create an user with sudo
privileges. Sudo
allows users to execute commands as the superuser or simply as another user.
We’ll be focusing on how we can create and assign the sudo user on distributions such as: Debian-based distros such as Ubuntu, RHEL-based distros such as CentOS, AlmaLinux, Fedora, etc.
How to Create an User with Sudo Privileges
Debian / Ubuntu
Create an User
First, we need to create the user we’ll assign the sudo
group to, if you already have created an user feel free to skip this step:
sudo adduser username
Output
root@linuxify:~# sudo adduser linuxify-dev
Adding user `linuxify-dev' ...
Adding new group `linuxify-dev' (1002) ...
Adding new user `linuxify-dev' (1002) with group `linuxify-dev' ...
Creating home directory `/home/linuxify-dev' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for linuxify-dev
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
root@linuxify:~#
That’s it! You have successfully created an user.
Adding the new user to the sudo
group
To add the new user to the sudo group we need to use the usermod command to do so:
sudo usermod -aG sudo username
Output
root@linuxify:~# sudo usermod -aG sudo linuxify-dev
root@linuxify:~#
Verifying the Users’ group
To verify the new user was successfully added to the sudo group, login as the new user and run the whoami
command:
su username
sudo whoami
Example
su linuxify-dev
Output
linuxify-dev@linuxify:~$ sudo whoami
[sudo] password for linuxify-dev:
root
linuxify-dev@linuxify:~$
We can see that the response we get is root
, indicating we have sudo
privileges (access).
CentOS / AlmaLinux / Fedora
Create an User
First, we need to create a new user we are going to be assigning sudo privileges to. If you already have create a new user feel free to skip this part.
useradd username
Then, we need to assign a new password to the newly created user with the help of the passwd command:
sudo passwd username
Output example
[root@localhost-live ~]# passwd linuxify
Changing password for user linuxify.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost-live ~]#
Adding the new user to the sudo group
In comparison to Debian-based distros, RHEL-based distros can get sudo privileges if they get added to the wheel
group. To add an user to the wheel
group we’ll use the usermod command along with the -aG
flags:
-a
– Append or add user to a supplementary group. Only used with-G
-G
– group(s)
sudo usermod -aG wheel username
Example output
[root@localhost-live ~]# sudo usermod -aG wheel linuxify
[root@localhost-live ~]#
Verify the Users’ group
To verify if we have correctly added the user to the wheel group and thus providing the user with sudo privileges, we can use the whoami command to verify. If the user gets the response of root, that means the user has sudo privileges:
sudo whoami
Output
[linuxify@localhost-live ~]$ sudo whoami
root
[linuxify@localhost-live ~]$
And that’s it! You have successfully created a new user and assigned that user to have sudo privileges.
Summary
This guide focused on how you can create an user with sudo
privileges on Debian-based and RHEL-based distributions.