I'm trying to SSH to a server on startup with a .sh script, but that will require me to enter the password for the account on the server that I'm SSHing to. I did some RTFMing, and I see in "-o" that it has "PasswordAuthentication" but I'm not sure how or if I could use that option. As this will be in a shell script, obviously I'd like to have the password in that file, or in any case not have to enter in the password manually every time the script runs.


I'm on my phone now so I can't give you a detailed answer. The proper solution here in my opinion is to use key based authentication. Basically you use ssh-keygen (I think) to generate a public/private key pair. Since you need script access, enter a blank password when prompted. Then, follow the relevant man pages to install the public key on the server and install the private key in its proper place.

Now you should be able to log in to that server securely and without a password. Note that by securely I mean as long as your user account isn't compromised.


I would suggest that you use ssh-keygen to generate an ssh key and use that to login to your remote ssh server.

The procedure is explained here.


This thread on ServerFault suggests two ways, one with sshpass:

sudo apt-get install sshpass
sshpass -p your_password ssh user@hostname

and one with expect:

#!/usr/bin/expect -f
#  ./ssh.exp password id
set pass [lrange $argv 0 0]
set server [lrange $argv 1 1]
set name [lrange $argv 2 2]

spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"

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.