I've been working on a series of scripts to perform tasks on my remote server, but have gotten stuck at how to log in via ssh mid script, as I cannot enter the password automatically. Is there a way to do so?

echo "logging in.. \n" ssh username@IP somehow hardcode in password nextCommand

  • You can set up public key authentication without a passphrase, or you can use an authentication agent. I would suggest the latter if it is reasonable. – user Jul 19 '13 at 18:10

There is no way to send the password via parameter. You can use ssh keys. In your client machine:

ssh-keygen -t rsa

This will create a file with your public ssh key on your ~/.ssh/ named id_rsa.pub Then, you must add the content of the id_rsa.pub file on your server machine (you can copy it via scp or ftp), and run something like:

cat id_rsa.pub >> ~/.ssh/.authorized_keys

That worked for me.


As @MichaelKjörling said, set up public key authentication:

ssh-copy-id user@host

Now you can ssh to that user@host without a password.

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.