Note: Adding a few lines to the .<name of shell>rc will not solve the problem here, since this particular ssh key has a password and that would not eliminate the need to keep typing it.
So I don't really understand how ssh-agent works under the hood. I just use ssh-agent and ssh-add ~/.ssh/id_rsa every time I need to add a key to access some remote resource. One I've added the key once, I don't need to add it again for the same "shell session" ("shell session" is probably not the appropriate jargon).
Unfortunately, I'm creating new shell sessions all the time. I'm running zsh under tmux on OS X and have a ssh key creatively named id_rsa. That ssh key has a password associated with it.
Every time I start a new shell I have to do the following
$ eval `ssh-agent`
$ ssh-add ~/.ssh/id_rsa
<type password>
which is really irritating.
I've noticed in the output of ssh-agent that the SSH_AGENT_PID environment variable is different every time. My hunch is that this environment variable and the SSH_AUTH_SOCK are the reason that keys don't need to be re-added within a single shell session. When I call the ssh program, it will use these environment variables to communicate with the ssh-agent and authentication will succeed.
I'm wondering if there's a way of sharing ssh-agents between sessions. Maybe the right approach is to add my SSH keys before starting tmux and configure tmux to preserve the SSH_AUTH_SOCK and SSH_AGENT_PID environment variables. I'm really not sure. What's the standard way of solving this problem?
ssh-addfrom the shell startup script would of course require writing the password each time, but making sure you can connect to an existing agent isn't the same. But yes, I admit to missing the mention of OS X. – ilkkachu Jul 30 '16 at 07:21ssh's own tools and designated exit codes to check the socket is working. I use a hybrid of the two. – Walf Oct 20 '23 at 04:36