Sudo Password – How to Avoid Being Prompted for a Password by Sudo

command linesusudo

I know I can become root (super user) via the su command but I have to authorize it after entering the commands. Is there a way I can become root and authorize (with password) in one line

Best Answer

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.