104

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?

ccman
  • 1,283
  • 2
  • 10
  • 10
  • 3
    you should just check if your script is run by "root". It's bad to echoing the password, it will be found in the history... – kbdjockey Aug 14 '12 at 15:10
  • 5
    It's much better to configure `sudo` properly that it won't ask password for certain program/users/group to avoid such dirty hacks. – rush Aug 14 '12 at 15:15

5 Answers5

238

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

stonesam92
  • 4,307
  • 2
  • 19
  • 24
  • How about putting it in a dedicated folder giving both it and the folder execute-only permissions: `sudo chmod -R 0100 myScriptFolder`? Wouldn't that solve the security issues (provided no one uses your computer as root but you)? – Brōtsyorfuzthrāx Sep 11 '15 at 11:46
  • Or can hackers from outside my local network somehow see the script text as it's executed, under certain circumstances? – Brōtsyorfuzthrāx Sep 11 '15 at 11:50
  • This worked like a champ for me on my OSX box! Thanks so much for sharing the solution! – Wulf Jun 24 '16 at 18:14
  • I tried this in a Dockerfile but it didn't work. Any suggestions? – timebandit Jun 19 '17 at 11:52
  • I got it in NodeJS (TypeScript) with the following @timebandit: const child = spawn("sudo", ["-S","-k","echo","Hello"]) child.stderr.on('data', function (data) { console.log('stdErr:', data.toString()); child.stdin.write(password + '\n'); }); child.stdout.on('data', (data)=>{ console.log('Received:', data.toString()) }) – Jason Nichols Jan 24 '22 at 15:14
  • works in ubuntu 2004. if you want to use it with sshpass you need escape it . like sshpass -p mypassword ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@127.0.0.5 'echo mypassword | sudo -S -k whoami' – bronze man Jul 07 '22 at 05:20
70

You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):

sudo chmod +s myscript
perreal
  • 94,503
  • 21
  • 155
  • 181
30
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
matteomattei
  • 650
  • 5
  • 9
  • This doesn't work when I have an argument for my command. for example. `echo -e "YOURPASSWORD\n" | sudo -S "run -x"`. It says `sudo: run -x: command not found`. Any idea how to get around this? – Bee Oct 18 '17 at 08:00
  • 1
    @Bhathiya simply don't use "quotes", for example: echo -e "YOURPASSWORD\n" | sudo -S ls -l /root – matteomattei Oct 19 '17 at 08:20
4

One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.

Lucas Holt
  • 3,826
  • 1
  • 32
  • 41
-5
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
kbdjockey
  • 891
  • 6
  • 8