2

This is the first time it has happened to me where I am using the su command and it actually displays the password on the terminal and doesn't stay hidden. Here is my code snippet:

 sshpass -p "password" ssh -q [email protected] "su -lc 'mkdir temp/'"

Code explanation: I am accessing a remote server and trying be root on that server to create a folder. In doing so I have to use the su command and it prompts me for the password. When I enter the password, it gets displayed and doesn't stay hidden. How do I fix that?

5
  • Use sudo don't use su here.
    – slm
    Jun 2, 2014 at 18:59
  • @slm sudo doesn't work. I keep getting an error every time I use it.
    – Redson
    Jun 2, 2014 at 19:10
  • Do you have sudo setup to run this?
    – slm
    Jun 2, 2014 at 19:29
  • @slm Yes I do, but it still is giving me an error
    – Redson
    Jun 2, 2014 at 19:31
  • If you use the command sshpass -p "password" ssh -t -q [email protected] "su -lc 'mkdir temp/'", does it work? su turns off echo on its tty, but when ssh is given a command to run on the remote host, it doesn't allocate a tty unless it is given a -t option. Jun 3, 2014 at 0:22

3 Answers 3

3

In general please AVOID using sshpass with a password.
Everything you wrote can be observed by other users with the ps command and this SHOULD be of concern to you.
Second problem there is here, I don't see you specifying -t option. This means you have disabled

Defaults    requiretty

In your /etc/sudoers, which can have security implications.

If you still insist on using sshkeypass, try:

sshpass -p "password" ssh -qt [email protected] "su -lc 'mkdir temp/'"

You can set up easily a generate ssh key just to do this job and then remove it.

ssh-keygen -t rsa

Follow the instructions on the screen,

ssh-copy-id user@server1

Or you can also do:

cat ~/.ssh/id_rsa.pub | ssh user@server1 "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"
1

Two things here:

  1. use ~/.ssh/authorized_keys at [email protected] to login remotely, without interactive password, instead of having the password on command line from client machine. This would also allow the job to run automatedly via cron or whatever.

  2. configure /etc/sudoers on remote box 74.11.11.11 to allow the command to execute as root user, by the user that is connecting, in this case: username.

So in your example, /etc/sudoers on 74.11.11.11 would allow user username to execute mkdir, as root without a password.

If you have tried this, and it is not working for you, run man sudoers.

1

Besides the suggestions to not use sshpass, it's possible to keep it "hidden" from the command line:

  • Edit your /etc/profile and paste there:
    export SSHPASS='my_pass_here'
    
  • Use the -e argument with sshpass command
    $ sshpass -e ssh [email protected] 'ls -ll' 
    
  • Another option is to save your password in a different file and use the -f argument:
    $ sshpass -f password_filename ssh [email protected] 'ls -la' 
    

But the best solution is to follow the @Hristo Mohamed suggestion:

In general please AVOID using sshpass with a password.

You can set up easily a generate ssh key just to do this job and then remove it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .