Use sudo with password as parameter

ccman picture ccman · Aug 14, 2012 · Viewed 147k times · Source

I would like to run sudo with my password as parameter so that I can use it for a script. I tried

sudo -S mypassword execute_command

but without any success. Any suggestions?

Answer

stonesam92 picture stonesam92 · Aug 14, 2012

The -S switch makes sudo read the password from STDIN. This means you can do

echo mypassword | sudo -S command

to pass the password to sudo

However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons