How to Execute Sudo Commands Without Password

Are you tired of typing your sudo password any time that you need to execute a command with root privileges? We’ve got good news: it’s possible to disable the sudo password, which will bypass the password prompt whenever you type a command with sudo.

These instructions will work the same whether you’re using Ubuntu, Debian, Arch Linux, Manjaro, Fedora, or any other system that has sudo access configured.

Obligatory warning: Disabling the sudo password is a bad idea unless you’re on a test system or you’re the only user on the computer. Otherwise, anyone logging into your account will have root permissions.

Permanently Disable Sudo Password Prompt

To avoid the sudo prompt ever showing up when you execute a sudo command, follow the instructions below.

1. Type the following command to edit the /etc/sudoers file.

$ sudo visudo

2. At the bottom of this file, append the following line, while replacing linuxnightly with the name of your user account.

linuxnightly ALL=(ALL) NOPASSWD: ALL

Allowing a single user to bypass sudo prompt by editing sudoers file

Save your changes and exit the file when done, and the changes will take effect immediately.

3. If you would like to grant every user the ability to bypass the sudo password prompt, you’ll want to append this code instead.


Allowing all users to bypass sudo prompt by editing sudoers file

All done. If you need to revert these changes, simply delete the line that you appended to the file.

Supply Sudo Password in Command

If you don’t want to permanently disable the sudo password, you could alternatively supply the sudo password in a command. This relies on sudo’s -S option, which will accept input from stdin.

You should also use the -k option, which will ensure that you get prompted for a password, even if you recently supplied it and have cached credentials.

1. As an example, we’ll run the whoami command with sudo privileges. Notice we supply our password with the echo command first.

$ echo 'password' | sudo -S -k whoami

2. Note that the command above will show up in your terminal history, and also will be viewable by anyone looking over your shoulder. A better method is to put your sudo password in a secure file.

$ echo 'password' > ~/.sudopass.txt
$ chmod 400 ~/.sudopass.txt

3. Now you can reference this file to supply a password in your sudo command.

$ cat ~/.sudopass.txt | sudo -S -k whoami

Leave a Comment

Your email address will not be published. Required fields are marked *