Start ssh-agent on login
Old question, but I did come across a similar situation. Don't think the above answer fully achieves what is needed. The missing piece is keychain
; install it if it isn't already.
sudo apt-get install keychain
Then add the following line to your ~/.bashrc
eval $(keychain --eval id_rsa)
This will start the ssh-agent
if it isn't running, connect to it if it is, load the ssh-agent
environment variables into your shell, and load your ssh key.
Change id_rsa
to whichever private key in ~/.ssh
you want to load.
Some useful options for keychain:
-q
Quiet mode--noask
Don't ask for the password upon start, but on demand when ssh key is actually used.
Reference
https://unix.stackexchange.com/questions/90853/how-can-i-run-ssh-add-automatically-without-password-prompt
On Arch Linux, the following works really great (should work on all systemd-based distros):
Create a systemd user service, by putting the following to ~/.config/systemd/user/ssh-agent.service
:
[Unit]
Description=SSH key agent
[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
[Install]
WantedBy=default.target
Setup shell to have an environment variable for the socket (.bash_profile, .zshrc, ...
):
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
Enable the service, so it'll be started automatically on login, and start it:
systemctl --user enable ssh-agent
systemctl --user start ssh-agent
Add the following configuration setting to your local ssh config file ~/.ssh/config
(this works since SSH 7.2):
AddKeysToAgent yes
This will instruct the ssh client to always add the key to a running agent, so there's no need to ssh-add it beforehand.
Please go through this article. You may find this very useful:
https://web.archive.org/web/20210506080335/https://mah.everybody.org/docs/ssh
Just in case the above link vanishes some day, I am capturing the main piece of the solution below:
This solution from Joseph M. Reagle by way of Daniel Starin:
Add this following to your
.bash_profile
SSH_ENV="$HOME/.ssh/agent-environment" function start_agent { echo "Initialising new SSH agent..." /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" echo succeeded chmod 600 "${SSH_ENV}" . "${SSH_ENV}" > /dev/null /usr/bin/ssh-add; } # Source SSH settings, if applicable if [ -f "${SSH_ENV}" ]; then . "${SSH_ENV}" > /dev/null #ps ${SSH_AGENT_PID} doesn't work under cywgin ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { start_agent; } else start_agent; fi
This version is especially nice since it will see if you've already started ssh-agent and, if it can't find it, will start it up and store the settings so that they'll be usable the next time you start up a shell.
Add this to your ~/.bashrc
, then logout and back in to take effect.
if [ ! -S ~/.ssh/ssh_auth_sock ]; then
eval `ssh-agent`
ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
ssh-add -l > /dev/null || ssh-add
This should only prompt for a password the first time you login after each reboot. It will keep reusing the same ssh-agent
as long as it stays running.