I have a remote server. I planned to use public keys to connect over ssh without typing the password but the remote system rejects public key authentication.

So what is the best way to do it? What about using sshpass?

  • 3
    What do you mean by "router doesn't allow it"? – mtahmed May 28 '14 at 23:31
  • @mtahmed, I tried to share public key but it fails to connect. the network administrator explained me later that the routing configuration blocks public key use. – Nadina May 28 '14 at 23:36
  • 5
    A router cannot know whether you are using passwords or public keys for authentication. Whatever is happening, this is not the reason. Public key authentication would have to be rejected on the server itself. – Gilles 'SO- stop being evil' May 28 '14 at 23:36
  • 2
    @Gilles, i've just asked to the system admin of the server, and you're right. The remote ssh server rejects public key authentication. I think the network administrators just want to get rid of me ! – Nadina May 28 '14 at 23:42
  • Related: unix.stackexchange.com/questions/38737/… – slm May 28 '14 at 23:57

You can use sshpass if you are the only user of you system. But it's definitely a very bad idea. Everybody could get your passord if he has a shell access to your machine.

Once you can't exchange public keys. I suggest you to use expect. You'll need to secure an exp file containing your password. It will be stored on your hard disk, so every user having the sudo or root access can hack it. But at least it's really more secure than sshpass.

Here's how to do it

| improve this answer | |
  • I am the only user of my machine. ssh server is disabled. so it's the only problem with sshpass? – Nadina May 28 '14 at 23:39
  • As you describe the situation you're in an enterprise network there may be some administrator who can access your machine to update your system/software/security policy. But anyway sshpass is a bad practice. – Slyx May 28 '14 at 23:50
  • can i use it for file transfer? – Nadina May 28 '14 at 23:53
  • 1
    @Nadina, Yes you can. See this example – Slyx May 28 '14 at 23:56


From the link that I used as a reference, you can find more options.

If you have sshpass installed, you can automate the ssh connection so you don't need to type your password for each machine:

  sshpass -e  your commands here

1. Install sshpass

This is a nifty little program that allows you to pass an ssh password as a command line parameter. This is, obviously, not a very secure solution and I highly recommend you read the "Security Considerations" section of man sshpass.

Anyway, it is probably available in your distribution's repositories, on Debian-based systems it can be installed with

sudo apt-get install sshpass

I can't check since I don't have a RedHat based machine but as far as I can tell from searching here, it should be installable on Fedora with

sudo yum install sshpass

Once you have it installed, you can simply run

  sshpass -e your commands

The -e option tells sshpass to get the password from the SSHPASS variable. This is a bit more secure than giving it as a parameter with the -p option.


This will fail silently if the server you are connecting to is unknown, if its public key is not stored in your machine. If this does not seem to be working, just connect once (ssh or scp) to the remote machine and accept its public key.


script to automate scp in network

| improve this answer | |

As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:

read -s PASS; sshpass -p $PASS ssh <user>@<host adress>

After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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