To understand the concept in detail, dive into this article and sort out all your ambiguities related to the topic.
What is sudo command?
Sudo here is an abbreviation for SuperUser Do. If you want to access several restricted files or operations, use this command.
The sudo command elevates some of the privileges that allow users to complete tasks without the need to log in as the root user.
Whereas, the standard user accounts do not have the permission to perform sensitive tasks, for instance, to view the content of the root directory and so on.
Sometimes, it helps to reduce some unfortunate incidents, where the commands are used accidentally that may have major consequences. Wrong with that it also protects the system from several intruders that may cause harm.
Additionally, at times, when you want to run as an administrator, you can use the sudo command, which grants you privileges to carry out sensitive tasks.
Now, what is sudoers? Let’s understand!
There are several methods to give sudo privileges to a user and one option is to add the user to the sudoers file.
This file inhibits the information and data about the record of users and groups that have sudo privileges. It also tells the level of the privileges.
There is another method where you can add the user to the sudo group. This sudo group is mentioned in the sudoers file. But when we talk about Ubuntu, by default all the members of the sudo group have sudo access.
Now that you have decent information about the concept of sudo command as well as sudoers, let us quickly hop on to our core segment.
Before we start, here are the prerequisites.
- System with Ubuntu
- Access to root user account having sudo privileges
- Terminal window
Firstly, understand the concept to add a sudo user.
How to add a sudo user on Ubuntu?
- Create a new user: login to the system as a root user having sudo privileges.
- Navigate to your terminal window and type this command as stated next:
- This command assists you in creating a new user along with the home directory as well as a group.
- There may occur an error message stating that you do not have sufficient privileges. Do not worry and simply enter this command:
sudo adduser newuser
- Here you can set any username as per your wish in the place of newuser. The next step is to set a password and retype it to confirm.
- An additional set of information appears on the screen. It is optional to fill these fields and you can skip them by hitting Enter.
Congratulations! you have successfully created a new user.
Now the next step is to add this user to the sudoers file. Let’s begin!
How to add users to the sudoers file?
The pseudo privileges of both the users and groups are well elaborated in the /etc/sudoers file.
If you add the user in this file, you have the option to configure custom security policies as well as to grant customised access to the commands.
Additionally, you can also modify the sudoers file to configure the user sudo access in the /etc/sudoers.d directory. The sudoers file consists of all the files of this directory.
If you ever wish to edit the file, use:
Once you save it this command helps you to scan the file to check the syntax errors, if any. This is important because if there are any persistent errors, the file does not save.
Moreover, in case you open the file in a text editor, you may end up losing sudo access. So beware.
Moving on, when we talk about visudo, it uses vim text editor to open /etc/sudoers file. Besides, if you are not well acquainted with vim, you can also modify your default editor by executing the following demand:
Now, suppose the user does not wish to enter a password at the time of running pseudo commands. To grant this privilege, open /etc/sudoers file. Type the below-stated command:
Reach the bottom of the page and type:
username ALL=(ALL) NOPASSWD:ALL
Now, save this file and exit. Also, you must change the username with the same username that you wish to grant access to.
You can also limit the commands that you want the user to access, with the help of sudo. Suppose, you only want to allow mkdir and rmdir commands, then type this command and execute:
username ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/rmdir
This is how you add users to the sudoers file.
Additionally, if you also wish to add the user to a pseudo group you may follow these steps.
How to add users to a sudo group?
If you wish to grant sudo privileges to a user, you can simply add them to the sudo group. The members of this group can easily execute commands as root through sudo.
In order to add a user to the group, you can use the following command:
usermod -aG sudo username
This command successfully grants sudo access. Otherwise, you can also be assured by running the whoami command. Take a look.
The next window may show an error saying, the user is not in the sudoers file. It shows that the user does not have sudo privileges.
As it is clear that it is not a complicated task to grant sudo access to a user or add a user to sudoers. You just need to follow some simple steps and you are good to go.
We hope that this article proves beneficial to you and helps you to clear your ambiguities related to the topic.
Keep practising, keep learning!