What is a sudo user or a sudoer?
Heard of admins for a computer system? Those people are also called sudo users in the Linux world. These users have rights that are normally possessed by an admin. After that sudoer right is granted, a sudo user can change system-wide changes like upgrading software, adding or removing users for the system, etc..
Creating a sudo user in Ubuntu and Debian
The commands used here are standard Linux commands and these should be installed on most Linux distributions by default. However, I am not sure if all Linux distributions have a group named sudo.
Let’s now get started on creating a sudo user or sudoer in Linux command line.
Step 1: Create a new user [if it doesn’t exist already]
The first step in creating a sudo user is to create a normal user. Linux will set up necessary permissions for the user to read, write files and execute programs.
If you are interested, I advise reading our article on Linux file permissions for a better understanding on this subject.
Note: Adding a new user will also create a user group named the same as the user.
To create a user, one must be a sudoer or a root. This is to ensure that only people who are having rights over the system or in charge of protecting the system are creating new users and nobody else.
I am going to use adduser command here.
sudo adduser <username>
Below Picture show the creation of new user named sen:
Note that group sen is created and files are created for the new user. It also asks for a password. When you type your password it will be invisible, but you can also use delete or backspace whenever you like. Always use alphanumeric passwords to protect your account.
It also asks for details for name, phone numbers, and others. You may enter the details if you want to but it is not mandatory. After confirmation, the user will be created successfully. You can see that the user is created by running either of the following commands.
ls /home/ (or) cat /etc/passwd | grep <username>
Below are screenshots which I used to verify that user is created.
Now if you list users in Linux, you should see the newly created user there.
Step 2: Add user to sudoers
Once the user is created, he/she can be easily converted into sudo user by using one command. Once again you have to be root or a sudoer to do this step.
sudo usermod -aG sudo <username>
In the above command:
- usermod changes user’s properties and rights
- –a option tells to append the given group to the user without removing him/her from the groups he/she is already in.
- Option -G lists the groups to which user is to be added. In our case, it is “sudo”. So it is added directly to the command.
Below is screenshot which shows upgrading existing user “sen” to sudoer.
Step 3: Verify sudo access
Now, you should verify if the user has sudo access or not.
You can change user in command line or simply log out and log in as the new sudo user.
When you log in to the system for the first time using your new user account with sudo rights, you will be shown a message just like the one below:
From this point, You can write any command that needed sudoer privileges by prepending the command with “sudo”.
You can verify whether the user has root privileges by running a command with sudo command that will require root access such as this one:
sudo ls -l /root
Or you can run the whoami command with sudo and it should output root:
sudo whoami root
Congratulations! You made it to the end. At this point, you have a new user with sudoer privileges. Be careful about to whom you give sudoer rights because:
With great power, comes great responsibility
I hope you liked this quick tip and it helped you to add a user to sudoers. If you have any questions or suggestions, feel free to leave a comment below.