After creating an agent, it automatically detaches itself from the parent process.

someUser@MyPC:~$ eval "$(ssh-agent -s)"
Agent pid 10222
someUser@MyPC:~$ ps -e --forest | grep ssh-agent
  10222 ?        00:00:00 ssh-agent

When I close the terminal and open a new one to create another agent, there are now two agents running.

someUser@MyPC:~$ eval "$(ssh-agent -s)"
Agent pid 10503
someUser@MyPC:~$ ps -e --forest | grep ssh-agent
  10222 ?        00:00:00 ssh-agent
  10503 ?        00:00:00 ssh-agent

Is there a command to connect with the existing ssh-agent, as long as a certain controlling terminal doesn't close or kill it?

1 Answer 1


When you start the ssh-agent, it will give you two environment variables: SSH_AUTH_SOCK and SSH_AGENT_PID. That's why you need the eval "$(...)" around the command to start the agent: it outputs the shell commands to set the variables, and eval causes those commands to be executed within the current shell session.

To connect with an existing agent, set the SSH_AUTH_SOCK environment variable to point to the existing agent's socket. To do that, you could e.g. output the variable and its value to a file after starting it, so other terminals could pick them up from there.

For example:

eval "$(ssh-agent -s)"
echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" > ~/.ssh_auth_sock_info

then in another terminal, to pick up the agent connection information:

. ~/.ssh_auth_sock_info

Likewise, the SSH_AGENT_PID variable will contain the PID of the agent, so you can use it in e.g. a logout script to kill the agent.

If you know the agent PID but not the socket path, if you have the necessary privileges to use lsof, you could use it to find the socket. For example, in you ran sudo lsof -p 10222, you would see the open files and sockets of your first SSH agent. Typically, the file descriptor #3 would be the agent socket.

You must log in to answer this question.

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