using visudo command In Linux
visudo is a special command used to edit the sudoers file. visudo edits the “/etc/sudoers” to provide a user sudo privilege. It also checks the syntax for the sudoers file, it is the preferred way of editing the sudoers file as any syntax error may result in losing sudo access to the system. By default, ubuntu uses nano to open the sudoers file, you can change that with update-alternatives.
Note: Never edit the /etc/sudoers file directly as you may lose sudo access if anything goes wrong like a Syntax error.
Step 1: Run the visudo command to open the /etc/sudoers file for editing in the terminal
sudo visudo
Step 2: Add the below line to add the user as a privileged user in the sudoers file.
username ALL=(ALL:ALL) ALL
- The first ALL states the user can run sudo from any host.
- The (ALL:ALL) states the user can run the command as any user and any group.
- The third ALL states every command can be executed.
Step 3: Switch to the user which given the privilege of sudo using the su command. Make sure you pass the “-“ to completely switch into the user like PATH variables and user privileges.
su - username
Step 4: Run sudo whoami to check whether that user has the privilege to use the sudo command.
sudo whoami
In the above image we can see that after switching to our new user, we have run a sudo whoami. The output says we are root as we have used sudo, we are performing whoami from the place of the root user, that’s why it says the username root.
How to Add User to Sudoers in Ubuntu
Sudo is a command line tool in a Unix-based system that allows a regular user to run a command as the superuser or another user. Only the super user is allowed to modify the majority of the System-wide settings and configurations. It is not advisable to run any Linux machine as a super user, so always create a normal user for day-to-day usage. To use the super user privileges we don’t need to log in as root users every time, just provide the sudo rights to a normal user to portray the normal user as a root user. It is often done with the help of Sudo group or sudoers file in Linux systems.
In this article, we will discuss how to add a normal user to the sudoers list so that the normal user can use the Sudo command to perform privileged operations.