2021-01-17
I have numerous machines I frequently log in to and from via SSH. Typing
in a password every time is tiresome and a password-less key is not an
option. A solution is an SSH agent running in the background, which
prompts for a password once at log in, and stores it for the duration of
the session (until reboot). The following can live in your shell's startup
file (e.g.
~/.zshrc).
if [ ! -S $HOME/.ssh/ssh_auth_sock ]; then
eval "$(ssh-agent)"
ln -sf "$SSH_AUTH_SOCK" $HOME/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=$HOME/.ssh/ssh_auth_sock
ssh-add -l > /dev/null || ssh-add
Then, just copy the needed key between machines with
ssh-copy-id -i ~/.ssh/id_xxx.pub name@host.
MacOS however, has its own provision for this. The following can simply be
added to your ~/.ssh/config file:
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519