Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have 2 users in my debian server, gooduser and root.

Command my_cmd prompts for a password (it connects to a ssh server).

When I execute my_cmd as gooduser, everything works as expected:

gooduser@debian:/mydir$ my_cmd
someone@192.168.2.122's password: 
Permission denied, please try again.
someone@192.168.2.122's password: 
Permission denied, please try again.
someone@192.168.2.122's password: 
Permission denied (publickey,password).
fatal: The remote end hung up unexpectedly
gooduser@debian:/mydir$

Above, I have ignored the password prompts by pressing enter three times. Why, I will tell you soon.

When I am logged in as root, however, I try to execute my_cmd as user gooduser (I have to, so that any files that are created will be owned by myuser). However, it doesn't prompt me for a password, not even once, and it works as if I had pressed enter 3 times, except I didn't (the "Permission denied" messages are the same as above):

root@debian:/mydir# su - gooduser -c "cd $PWD; my_cmd"
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
fatal: The remote end hung up unexpectedly
root@debian:/mydir#

Why does this happen? Why does su ignore the password prompts? Or, more precisely, why does it seem to press enter on my behalf? More importantly, how can I solve this?

(I know I could configure ssh keys for passwordless login and maybe that's what I'll end up doing, but I would like to know what is the deeper issue here).

share|improve this question
1  
what is $PWD at the time of execution as root? does gooduser have access to that path? –  Frank Thomas Nov 13 '12 at 21:01
add comment

2 Answers

ssh tries to open /dev/tty to get the password, but the system doesn't allow a user other than the one currently logged in on the terminal to do that. Because of this ssh can't read the password.

share|improve this answer
    
Why is that, I can run this with standard prompt su - test -c "ssh test" ? –  week Nov 13 '12 at 21:10
add comment

I ended up using sudo:

sudo -i -u gooduser -H bash -c "cd $PWD; my_cmd"

Using this command, I was prompted for my password as expected. -i was necessary so that $PATH was set up correctly, otherwise my command failed.

As for why su fails, I still don't know. Using su did work in Ubuntu 12.04, but it didn't work in Debian (thus my question), so I'm not sure that qqx's explanation applies.

share|improve this answer
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.