Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing su password in shell script

Tags:

shell

sudo

su

How can password be passed in a shell script using su(without sudo and except)?. I have tried echo "password" | su root -c .But it didnt work.

like image 991
Vijay Kumar Badugu Avatar asked Sep 10 '25 01:09

Vijay Kumar Badugu


1 Answers

The best way of doing this is with sudo, but since you don't want the best solution, you can you can use script instead:

{ sleep 3; echo "yourpassword"; } | script -q -c 'su -c whoami' /dev/null

This will print root, the output of whoami.

Please make sure to try this command verbatim (with password replaced) before trying to adapt it to run your own commands, since adapting it is difficult and error prone.

like image 81
that other guy Avatar answered Sep 13 '25 06:09

that other guy