We already know how to use keys in order to connect through Secure Shell, but, there is an issue, it requires unlocking private key with a secret passphrase upon each connection.
To avoid this, we need to use ssh-agent, a program that runs in background and stores your keys in memory.
# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
Agent pid 69599
Add the SSH key to the ssh-agent
$ ssh-add ~/.ssh/id_rsa
Remember that we need to already have our key associated with the remote agent such as a github repo a ssh host.
So, let's configure:
Create or open up the file at ~/.ssh/config
Enter the following text, replacing myhost.com with our server domain name or IP
Host myhost.com
ForwardAgent yes
This can also be done with command line args, if we don't want to create a config file,using -A flag with the ssh command.
ssh -A user@myhost.com
MacOS X modifies SSH agent so that it is started via the Mac OS X launchd service on demand (i.e. it will be launched on first use). This is important only if you're on Mac OS X Leopard 10.5.1 or below. If you're on anything newer, you may no longer need to do this, so feel free to skip this step if the agent is already running on startup
To enable SSH agent starting automatically on demand (this happens by integrating SSH agent with launchd) you need to open a terminal and run:
$ sudo touch /var/db/useLS
To store the passphrase for your default key in the Keychain open a Terminal and run:
$ ssh-add -K
And to store the passphrase for a different key run:
$ ssh-add -K /path/to/private/key/file
When prompted for your passphrase enter it and that is it.
You will never need to run ssh-add nor enter your passphrase again.