Have you ever thought the sudo command could run without the password? If not, it is time to learn how it can be done.
The “sudo” keyword has a notable association with the system and the user. If you are a normal user (other than root), you need the “sudo” command to execute the root user’s command. Hence, by default, there are only two possibilities to run any sudo command, i.e., run the command with a password or use the root user’s account.
However, another possibility is to run the sudo commands without using the password with the normal user account. Well, how is it possible? Don’t worry. This post is all about the steps that enable a normal user to execute the sudo commands without a password.
The steps in this post are practically demonstrated on Ubuntu 22.04.
Note: Executing sudo commands without passwords may invoke any security threat. Ensure that you are not using it for all commands or you are an administrator.
How to Execute the sudo Commands Without Passwords?
Executing commands without passwords may invoke some security threats. However, it is not the issue on a personal computer/machine, as you are the only one to use that. To execute the sudo commands without a password, you must add the NOPASSWD line for that user in the sudoers file.
Following are the steps that show the practical illustration to enable the user to execute the sudo commands
Step 1: Switch to Root Access
Before proceeding, you must switch to the root user via the command:
$ su -
Step 2: Make a Backup of the sudoers File
It is suggested to create a backup of the sudoers file, as it will be altered soon, which may invoke serious system failure. For example, the following command will copy the “/etc/sudoers” file to the “/etc/sudoers.txt.” The file name and the destination may be different in your case:
# cp /etc/sudoers /etc/sudoers.txt
Step 3: Add the NOPASSWD to the sudoers File
Now, open the sudoers file in editable mode using the following command:
Now, there are two scenarios to grant the NOPASSWD access to the specific user, one is to allow the user to execute all the commands without sudo, or you can allow for specific commands only.
Allow the User to Execute All Commands
username ALL=(ALL) NOPASSWD: ALL
Allow the User For Specific Commands
To allow users to run specific commands without sudo privileges, you must add the path to the commands in the following manner. Most of the Linux command’s executables are placed inside the “/bin/” directory by default.
username ALL = NOPASSWD: Path/To/Command1, Path/To/Command2
Remember to replace the username with your user’s name and make changes as a root user.
If you see the following image, one of the users named linuxuser is allowed to execute all sudo commands without a password. While the user named linux is allowed to execute only the kill command:
Step 4: Verification
Once added, exit the root user’s access (and switch to the user for whom you have made the changes). Now, run any sudo command and check if it asks for the password:
$ sudo apt update
As can be seen, we have yet to be asked for the password while executing a sudo-based command.
In Linux, the users can run any sudo-based command without the sudo keyword by adding the NOPASSWD line for the specific user in the sudoers file. This can be useful in certain situations, such as when you are the only user on a personal computer/machine. However, it is important to note that executing commands without password authentication may pose security risks and should be done with caution.
We have provided a detailed procedural guide to teach you how a sudo command can be executed without the passwords. For more Linux tips and tricks, keep following Linux Genie.