Have you ever gotten annoyed when executing a specific command that requires sudo to ask for a password?

If you are a root user, you don't have to type the password while executing any system-level command. However, regular users with sudo privileges must repeatedly type the same password while running the sudo command per session.

Let’s take an apt command that requires sudo privileges and asks for the password. This can all be avoided using the below-mentioned method.

Run Particular Command without Sudo Password

We must add a new entry in the sudoers file to avoid typing passwords for each command requiring sudo privileges.

For that, execute the below command to open the sudoers file.

  • Recommended
  • sudo visudo

In some Linux distributions, the visudo command is not provided. In such cases, open the sudoers file using the below command.

  • Not Recommended
  • sudo nano /etc/sudoers

Below is the behavior of any of the above commands.

Executing visudo command to open the sudoers file

After executing the above command, open another terminal and take the command requiring sudo privileges, which you want to avoid typing the password.

In my case, it is an apt command; for that, I have to find the absolute path of the command using the type or whereis command, as shown below.

  • type apt

Below is the behavior of the above command.

Finding the absolute path of the command

As you can see above, the absolute path of the apt command is the /usr/bin/apt path. You can replace the command with something else and note down the path, then go back to the previous terminal window and add the below command at the end of the sudoers file.

ubuntushell ALL=(ALL) NOPASSWD: /usr/bin/apt

Make sure to replace "ubuntushell" with your username and replace the /usr/bin/apt path with your own command path. Then add this line of code at the end of the sudoers file, as shown below.

Editing sudoers file

Save and re-execute the same command. In my case, the apt command with sudo privileges will not ask for the password again, as shown below.

Executing the "sudo apt" command

Run Multiple Commands without a Sudo Password

Want to add more commands to avoid typing sudo passwords? Then add the absolute path of all commands, separated by commas, as shown below.

username ALL=(ALL) NOPASSWD: /usr/bin/apt, /usr/bin/systemctl

As you can see above, I have added the systemctl command, which requires sudo privileges separated by commas. However, you can add as many as you want.

Run All Commands without Sudo Password [Not Recommended]

I think you definitely got bored typing passwords. You can execute all the commands requiring sudo privileges without a password (not recommended).

For that, remove all the absolute paths of command after NOPASSWD and replace them with ALL, as shown below.

username ALL=(ALL) NOPASSWD: ALL

After adding the line in the sudoers file, any command requiring sudo privileges will not ask for the password.

Wrap Up

If you want to revert and allow them to authenticate, remove that line of code using the same method.

All though we do not encourage you to allow all sudo required commands to run without a password, except if it is your personal computer.

🔗 Listing All Users in a Linux System

🔗 Add Regular User to Sudo Group using Usermod Command