Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I suspect su doesn't have any way to allow passing the password from the shell instead of being prompted for one, for security reasons (couldn't find anything in the MAN). Is this true?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

That's true, there is no way to have su(1) redirect its input from a place other than the tty. But there are libraries and programs to simulate a terminal for automated scripting, for example, Expect. Something similar to:

stty echo
log_user 0
set timeout 2
if [catch "spawn su root" reason] {
    send_user "Failed to spawn su: $reason\n"
    exit 1
}
expect "Password:"
send "$PASSWORD\r"
expect "\r\n"
#set timeout 5
log_user 1
expect {
    "# " {send_user "\nsu successful.\n"}
    timeout {send_user "\nsu timed out.\n"; exit 1}
    "incorrect" {send_user "\nsu failed.\n"; exit 1}
}
share|improve this answer
add comment

It's an incredibly bad idea, but if you really want to do it you can use sudo's NOPASSWD option:

In /etc/sudoers: 
# User privilege specification
johndoe  ALL=(ALL) NOPASSWD:ALL

Then user johndoe can do sudo su and wind up with a root prompt without ever being asked for a password.

I reiterate, though, that this is a REALLY BAD IDEA in most circumstances. Sudo, preferably /without/ the nopasswd flag is significantly safer.

share|improve this answer
    
Reread the question, he wants to do echo "password" | su or something like that - NOT get rid of the password, NOT use sudo (which is less secure than su...) –  mathepic Feb 21 '11 at 17:36
1  
@mathepic: If su is only being used for a specific command, giving the user sudo powers to run that command is a lot better security than storing the root password in a file in most use cases. –  Gilles Feb 21 '11 at 21:20
    
@Gilles: correct but a certain distribution has sort of changed the normal use of sudo. I do like sudo for what it was meant to be used as - but not as a replacement for su. –  mathepic Feb 21 '11 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.