Harvett
Harvett

Reputation: 178

How to avoid typing passphrase for public key when using ssh

If I want to use password, I can do it by:

`sshpass -f <(printf '%s\n' your_password) ssh user@hostname`

But sshpass seems not working for public key. I tried the following command and it failed:

`sshpass -f <(printf '%s\n' your_passphrase) ssh -o PreferredAuthentications=publickey user@hostname`

Is there any good way to do so?

Upvotes: 2

Views: 11158

Answers (2)

tigernotiger
tigernotiger

Reputation: 91

You can use the sshpass option -P to detect the passphrase prompt:

sshpass -Ppassphrase -f <(printf '%s\n' your_passphrase) ssh -o PreferredAuthentications=publickey user@hostname

Upvotes: 7

Shachar Shemesh
Shachar Shemesh

Reputation: 8603

Why on earth would you want to use sshpass in order to put in the public key?

If you want to use your public key with no password prompt from an interactive situation, just use an ssh agent. At least on Ubuntu, it runs by default. All you have to do is add the keys you want to it and enter the password once.

If you want to do so from a script, just create an unencrypted version of the key. It is as secure (if not more secure) than using sshpass, and saves the hassle of using another program.

On a personal note, I wrote, from scratch, several open source projects. Of the four or so that gained some popularity, none surprised me more than sshpass. It was a single day of work meant to solve a specific corner case. I get it that it turned out to be much more useful than I originally anticipated, but I really don't see any case where it makes sense to use it if public key authentication is an option.

Upvotes: 0

Related Questions