Use sudo with password as parameter [closed]
Asked Answered
W

5

107

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?

Warner answered 14/8, 2012 at 15:5 Comment(2)
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...Tasker
It's much better to configure sudo properly that it won't ask password for certain program/users/group to avoid such dirty hacks.Farinose
T
244

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

Tawnyatawsha answered 14/8, 2012 at 15:8 Comment(7)
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)?Norward
Or can hackers from outside my local network somehow see the script text as it's executed, under certain circumstances?Norward
This worked like a champ for me on my OSX box! Thanks so much for sharing the solution!Imprecise
I tried this in a Dockerfile but it didn't work. Any suggestions?Simon
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()) })Patrick
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 [email protected] 'echo mypassword | sudo -S -k whoami'Worrell
echo pass | sudo -S bash does not workLichee
H
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
Hygrophilous answered 14/8, 2012 at 15:9 Comment(5)
+1. This is way better (and more secure) than the other methods.Electronarcosis
sticky bit is bad practice no ?Tasker
@Tasker - +s is the setuid bit. +t is the sticky bit.Stress
See this Q/A that pertains to this answer: superuser.com/questions/440363/…Norward
Note that most OSes ignore this flag for interpreted script files. This does work nicely on binaries tho.Pusey
V
30
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
Vassallo answered 14/8, 2012 at 15:8 Comment(2)
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?Carpel
@Bhathiya simply don't use "quotes", for example: echo -e "YOURPASSWORD\n" | sudo -S ls -l /rootVassallo
M
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.

Milklivered answered 14/8, 2012 at 15:10 Comment(0)
T
-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
Tasker answered 14/8, 2012 at 15:12 Comment(1)
This doesn't answer OP's question.Krystakrystal

© 2022 - 2024 — McMap. All rights reserved.