How to add Users to the Sudo Group in Linux
To run certain commands on a Linux system a user must run the command in sudo
mode (superuser do). By default sudo
will allow the user to run a command like it was being done from the root account, providing overriding power. Therefore as a system administrator, it is good to know how to create users with sudo privileges and to add sudo privileges to existing users.
In this guide, we will go through the process of creating a new sudo user and then adding them to the sudo group
Create a New User
We are going to assume we want to create a new user that will be able to run commands in sudo
mode. The easiest way to do this is with the adduser
command.
To begin type adduser
followed by the name you wish to call the new account.
adduser ben
To may get the following message if your account has insufficient privileges:
adduser: Only root may add a user or group to the system.
In that case, you will have to run adduser
in sudo
mode or login using a root
account.
sudo adduser ben
Adding user `ben' ...
Adding new group `ben' (1002) ...
Adding new user `ben' (1002) with group `ben' ...
Creating home directory `/home/ben' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for ben
Enter the new value, or press ENTER for the default
Full Name []: Ben Smith
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
Enter a strong password, retype it and then fill in details for the user or press enter to leave each detail blank.
If you would like to create a user with more settings tweaked check out the tutorial on using the useradd command.
Adding a User to the sudo Group
To give a user the ability to run commands as the root user we need to add it to the sudo group. This can be done using the usermod
command.
usermod -aG sudo username
Check if a User has sudo Access
To test whether a user can run a command as the root user, we will first switch to that account using the su
command (substitute user).
su - ben
Then use whoami
to determine what user sudo
is running as.
sudo whoami
You should see the output from this command is root:
[sudo] password for ben:
root
How to Run Commands in Sudo Mode
To run a command in sudo
mode you just have to add sudo
to the beginning of any command.
sudo mkdir
You will be asked to provide the password for the account and for a while after you will not be prompted to supply a password when running commands in sudo mode.
Warning - since you will only be prompted to supply a password when running in sudo
for the first time, you could cause massive damage to your system by running commands such as sudo rm
carelessly.
Conclusion
You can now create users and add them to the sudo group. This will provide you with the ability to perform tasks that need elevated privileges on any account you need.