The sudo command is an excellent part of the Linux command-line. It allows users to execute root commands without needing to log into root, protecting their security. The problem is, to use the sudo command, you’ll need to enter your password.
Having to enter your password to execute the sudo command is undoubtedly an excellent security feature, but it can be incredibly tedious and annoying. If you’re OK with the trade-off in security features, you can make it so that password isn’t required to use sudo.
Method 1 – running sudo commands without password temporarily
The easiest way to run sudo commands without a password is to do it temporarily — meaning no editing to the system files to change settings. To do this, the sudo -s command is used.
The sudo -s command grants the user a Sudo shell. Essentially, you log into the terminal with your user and password and are given a root shell. You’ll then be able to enter any command as if you were doing them with the sudo command.
To use the sudo -s command, start by opening up a terminal. Then, enter the command below.
You’ll then see your terminal prompt logged in to the Sudo shell as root. From here, enter any command you’d like to run with the sudo command without having to enter a password.
It is possible to access the Sudo shell at any time, in any user. To exit the Sudo shell, enter the exit command.
Method 2 – adding passwordless sudo to Linux via sudoers file
If you want to execute Sudo commands without having to enter the Sudo shell each time, you can enable passwordless sudo. Passwordless sudo is a configuration file change that, when enabled, will make every sudo command run without a password.
However, before we begin, please understand that passwordless sudo is a considerable security risk. If you have a weak password set for your user account and then you enable passwordless sudo, a malicious attacker may be able to infiltrate your system. Be sure that your user account’s password is secure by changing the password.
To change your user account’s password, start by opening up a terminal window. When the terminal window is open, execute the passwd command.
After executing the passwd command, you’ll be asked to change the password to your Linux user account. Be sure to enter something secure and memorable. When your password is changed, follow the step-by-step instructions below to enable passwordless sudo via the sudoers file.
Step 1: Execute the visudo command to open up the sudoers file for editing. You should always use the visudo command to edit this file rather than
/etc/sudoers, for security purposes.
sudo EDITOR=nano visudo
Please note that if the visudo command does not work with the sudo command, you can also access it by using su to log into root.
su - EDITOR=nano visudo
Step 2: Once inside of the Nano editor, locate the line of code
root ALL=(ALL) ALL and press the Enter key on the keyboard to write a new line directly under it.
After pressing the Enter key, write out a new line of code. Be sure to change “user” in the code line below to your user account, or the code will not work.
user ALL=(ALL) NOPASSWD: ALL
Step 3: Press the Ctrl + O button to save the edits to the configuration file. After saving the edits, exit the editor by pressing the Ctrl + X button.
With the configuration file changed, you’ll be able to execute any sudo command without the need to enter a password!
Allow specific commands to be exected without sudo password
If you don’t want to have the sudo command work without a password for every single terminal command, you can restrict it so that only specific things can run without a password. Here’s how to set it up.
First, open up the sudoers file with the visudo command below.
sudo EDITOR=nano visudo
Inside of the Nano text editor, look through and find the
root ALL=(ALL) ALL line of code. Then, press the Enter key to create a new line directly below it. After creating a new line, add in the following code, but be sure to change “user” to your username.
user ALL=(ALL) NOPASSWD:
After writing out the code above, add the commands you would like to run without a password. For example, to make the cp command work in sudo without a password, you’d do:
user ALL=(ALL) NOPASSWD:/usr/bin/cp
To add multiple commands, separate them with a “,”. When done editing the sudoers file, press the Ctrl + O button combination on the keyboard to save the edits. Then, press Ctrl + X to close Nano.