How to avoid being prompted for a password by sudo?

Well, the only thing I can think of is

echo 'password' | sudo -S command

The -S flag makes sudo read the password from the standard input. As explained in man sudo:

-S, --stdin

Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.

So, to run ls with sudo privileges, you would do

echo 'password' | sudo -S ls

Note that this will produce an error if your sudo access token is active, if you don't need to enter your password because you've already done so recently. To get around that, you could use -k to reset the access token:

echo 'password' | sudo -kS ls

I don't know of any way of getting you into an actual root shell (like su or sudo -i) do. This might be enough for what you need though.


In terminal run the visudo command to edit the sudoers file:

sudo visudo

and add the following line to the sudoers list

username ALL = NOPASSWD : ALL

Note: Replace username with your real UserName in above line.


The echo 'password' | sudo -kS ls solution works, but it has a few security drawbacks, most of which have already been mentioned in the comments to terdon's answer. Thus, I would like to suggest a different approach:

If it is only one command that you frequently need to execute, e.g. apt-get upgrade, you can configure your system such that sudo someCommand does not require a password.

To do that, run visudo and enter something similar to the following:

myusername ALL=(ALL) NOPASSWD: /usr/bin/apt-get upgrade

Note that if you enter a command without an argument (e.g. without upgrade in this example), the user will be allowed to run it with any argument, so use with care.