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 **

  • 3
    Why not load it into the agent? Commented Nov 3, 2017 at 15:23
  • do you have any leads on how I would do that?
    – wizardzeb
    Commented Nov 3, 2017 at 15:43
  • 1
    You can also remove the passphrase from that key, but that is a solution that exists, but it's not recommended - stackoverflow.com/q/112396/2231796 - Stick with the answer given on your question(use a ssh agent) and you are good to go.
    – user34720
    Commented Nov 3, 2017 at 16:37

1 Answer 1


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


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


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.:

  • You need to do eval $(ssh-agent) because ssh-agent outputs some environment variables (e.g. SSH_AUTH_SOCK, SSH_AGENT_PID) that need to be exported for ssh-add and other commands to work.
    – wisbucky
    Commented May 24, 2019 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .