Shell – Automatically fill in ssh identity file password

shell-scriptssh

When I made my ssh key I added a password to the key. Right now I'm trying to write a script that will run some ssh and rsync commands. Unfortunately, since I have the password on my rsa key it prompts for the password every single time. Is there a way that I can prompt the user for the key passphrase once then redirect it into the ssh command?

** this is not an ssh user password. this is the passphrase for the key **

Best Answer

You can use the ssh key agent. The command to launch the agent is:

ssh-agent

Then you'll have to add your key to the agent. To add the default key (~/.ssh/id_rsa) you would use this command:

ssh-add

You'll have to enter your password once in order to add your key.

You might also want to check out this ServerFault post for more information:

Maybe the cleanest solution proposed there was to add an ssh-agent shebang to your script, e.g.:

#!/usr/bin/ssh-agent bash

ssh-add /path/to/ssh-key
ssh root@remotehost "remote commands"

There are also a lot of gotchas associated with using the ssh agent, so you might want to read some articles about general usage advice, e.g.:

Related Question