Ubuntu – How to make ‘sudo’ grab the password from a file

12.04sudo

I want to run a command that requires the sudo password say:

sudo apt-get update

Isn't this supposed to work (I have stored the password in a normal text file passwd.txt):

sudo apt-get update <~/passwd.txt

This is my logic for for why it SHOULD work: when the password is required, the user is asked to input the password from the keyboard. But redirecting the stdin to read from the passwd.txt file should work.

Shouldn't it?

Best Answer

sudo doesn't read password from stdin by default. From sudo manpage:

-S  The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.
    The password must be followed by a newline character.

So you should run:

sudo -S apt-get update <~/passwd.txt

Keep in mind that storing passwords in files is not a good practice. You should read: