Is it possible to write a script that run su
or sudo
command without typing password?. I googled and its said that su
doesn't read password from buffer. So is there any solution?
Asked
Active
Viewed 4,131 times
2 Answers
2
Two approaches as far as I can tell:
Good idea
When setting up sudo
for a specific user or group, you can use the ALL=NOPASSWD
option in the /etc/sudoers
file.
Some pointers on how to do that:
Bad idea
You can use sudo
with the -S
option to give the password through standard input instead of the terminal device. For example, you can write down the password in a file, and then do cat myPass.txt | sudo -S your_command
.
0
You can use expect
for this:
#!/usr/bin/expect
set password "mypassword"
spawn sudo mkdir -v newdir
expect "Password:"
send -- "$password\n"
expect "mkdir: created directory 'newdir'"

Jerry Epas
- 359
sudoers
list would be the secure way, in my opinion. Having passwords in plain text out there is always a bad idea, but I just wanted to let the OP know that it can be done, whether it's a good or a bad idea... – jimm-cl Jan 02 '15 at 15:03sudoers
list can be setup to allow one specific command to be run. Take a look at this and this. – jimm-cl Jan 02 '15 at 15:06NOPASSWD
approach over putting the password in a file. – Graeme Jan 02 '15 at 15:10chown root:buadmins myscript
, and give the appropriate execute permissions, for examplechmod 755
. Then set uid, for examplechmod ug+s myscript
Then when the user runs the script it will run as root. – PaperMonkey Jan 02 '15 at 17:06