On Linux, the sudo command gives you the ultimate privilege to perform various administrative functions, despite your non-root access. But to use sudo, you need to have a superuser account on your system.
Since there are several Linux operating systems, more commonly known as distros, there are multiple ways to add a sudo user. Read on to find more about how you can use Linux commands within different distros to achieve this simple, yet essential task.
Adding a Superuser on Ubuntu and Debian
Within Ubuntu and other Debian-based distros, there are two prominent ways of adding a sudo user. You can either do it through the terminal or take the graphical approach, using System Settings.
1. The Command-Line Approach
To add a sudo user via the command line, you have to launch the terminal first. Then, type the following command:
sudo adduser username
...where username is the name of the new user that you want to create.
This command will create a new user that you can add to the sudoers group. Yet again, there are multiple ways to add a user to the sudo group. The first way is using the usermod command.
sudo usermod -aG sudo username
...where -a refers to the Append function and G stands for Group.
Once you've added the user to the sudo group, you need to check if the aforementioned command worked properly. To verify, type the following:
groups username
In case you are a power user, who prefers to avoid multiple commands, you can directly create a new superuser using a single command:
sudo adduser username sudo
The verification process remains the same as before.
2. Using the Graphical Interface
Before adding the user to the sudo group using System Settings, you'll have to create a new user first through the command line. Then, follow the steps given below to proceed:
- Go to the Applications menu and click on the Users option. You will see a list of users including the newly created user(s).
- Click on the Unlock option, followed by the root password. You can switch to another user account by simply clicking it.
- As soon as you select it, you will see an option to convert the newly created user account into an administrator account. Toggle the button next to the Administrator label, and the account will get added as a sudo account.
Creating a New Superuser on Arch Linux
On Arch Linux, since the system doesn't create a sudo account by default, the user has to create one manually. The first step is to get root access using the su command.
su
Enter the password for the root user followed by the below command:
pacman -Sy sudo
As soon as sudo is installed, create a new user.
useradd --create-home username
Set up a password for the new user using the passwd command.
passwd username
Now, it is time to add the newly created user name to the sudoers list. Use the usermod command to achieve this.
usermod --append --groups wheel username
You can check the sudoers file using the following command:
visudo
Once the /etc/sudoers file opens, you need to edit some user privilege specifications, located at the bottom of the sudoers file. Locate the following line in the text file:
#%wheel ALL=(ALL) ALL
Output:
Remove the # symbol preceding the %wheel line and save the changes. Press Ctrl + O on the keyboard to save the file.
To check if the user has been added successfully, you need to execute the following command:
su username
The bash prompt will change to reflect the name of the new user. To verify, type:
whoami
It should display the name of the current user. To check if the new user has sudo privileges, type:
sudo whoami
If the output displays root, then the current user has administrative access.
Adding a sudo Account in CentOS
Considering CentOS's popularity, it would be a shame to miss out on the process of adding a sudo account in this distro. The process for adding a sudo account in CentOS is quite similar to the process specified above for Arch Linux, but with a few basic changes.
Before you create a new user, you have to log in as root. To do so, use the su command:
su
After logging in as the root user, it is time to create a new user, which we will then add to the sudoers list.
useradd -G wheel username
Set the password for this new user, using the passwd command.
passwd username
Now, log in as the new user, to check if you have sudo access. To do so, enter the following commands one by one:
su username
sudo whoami
Output:
As soon as you hit enter after typing the first command, the system will prompt you for the password. Enter the new password to continue. The output of the above command will display root, which means that the current user has root privileges.
How to Add a Superuser in Fedora
Last but not the least, in case you are using Fedora as your primary OS, you can assign superuser privileges to a new user in a few easy steps.
To create a new user, use the adduser command as follows:
adduser username
Now, set a password for the new user.
passwd username
Add this newly created user to the wheel group using the usermod command.
usermod -aG wheel username
Open the sudoers file using the following command:
visudo
Now, you need to edit some user privilege specifications. Locate the following line in the text file and uncomment the %wheel ALL=(ALL) line by removing the Pound character (#) preceding it.
## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL) ALL
Granting Superuser Privileges to New Users
Depending on which distro you are using, the process of creating new sudo users is going to be the same more or less, with a few slight differences. The idea is to make sure you are able to grant root permissions to any new administrators who will be using the distro.
Not many Linux users know about this, but sudo and su are two different commands. Although both of them perform similar functions, they are very different from one another in many aspects.