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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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
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='password' sshpass -e your commands here
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
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='password' sshpass -e your commands
-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
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 (
scp) to the remote machine and accept its public key.
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.