Give Sudo Access to a User in Ubuntu
Bestow the honor of sudo by adding a user to the sudoer list in Ubuntu.
So you added a new user to your Ubuntu system.
Now you want this user to be able to run commands with sudo.
To add a user to the sudoer list, you must add it to the sudo group.
Since this is a system management task, you need to be root or have sudo access yourself.
sudo adduser username sudo
That's all. You just created a sudo user in Ubuntu.
Sounds easy, right. Let's see the things in a little bit more detail.
Create a sudo user in Ubuntu
This part of the tutorial requires that you already have the user present on the system. You can list all the users on your system with the following command if you don't have the exact username.
compgen -u
Once you have the exact user name, use the adduser command and add the user to the sudo group.
sudo adduser username sudo
If the process was successful, you should see an output like this:
root@learnubuntu:~# adduser abhishek sudo
Adding user `abhishek' to group `sudo' ...
Adding user abhishek to group sudo
Done.
Verify that the user has got sudo access
There are can be various methods to check if the user has sudo access.
First, check the groups of the user.
groups username
The user should be a sudoer if it is part of the sudo group.
root@learnubuntu:~# groups abhishek
abhishek : abhishek sudo
Alternatively, you can use this command to see if the user can run commands with sudo or not:
sudo -l -U username
If the user has sudo access, it should show an output like this:
root@learnubuntu:~# sudo -l -U abhishek
Matching Defaults entries for abhishek on learnubuntu:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
use_pty
User abhishek may run the following commands on learnubuntu:
(ALL : ALL) ALL
If the user doesn't have sudo access, it will show an output like this:
root@learnubuntu:~# sudo -l -U abhishek
User abhishek is not allowed to run sudo on learnubuntu.
If nothing else, you can switch to that user and run a command with sudo and see if it works or not.
Bonus Tip: Remove the user from the sudoer list
You can revert the changes if you decide that a certain user should not be a sudo user anymore.
Take back what you gave. Remove the user from the sudo group.
sudo deluser username sudo
Don't worry. The user won't be deleted. It will just be removed from the sudo group and thus won't have the ability to run commands with sudo anymore. As discussed in the previous section, you can verify if the user has sudo access.
That's it. You can see that it is not too complicated to create a sudo user in Ubuntu.
Enjoy Ubuntu!