:: Re: [DNG] Using ssh-agent in multip…
Top Page
Delete this message
Reply to this message
Author: Tom
Date:  
To: dng
Subject: Re: [DNG] Using ssh-agent in multiple logins
On 29/3/2026 02:06, Lars Noodén via Dng wrote:
> On 3/28/26 16:25, Tom via Dng wrote:
>> Is it possible to have only one ssh-agent process running for my user
>> account and access it from multiple logins?
>
> Yes, copy the SSH_AUTH_SOCK variable there into each login session.
>
> e.g.
>
>  $ set | grep SSH_AUTH
>
> then in each new login session:
>
>  $ export SSH_AUTH_SOCK=/tmp/ssh-MVilHMx3nEus/agent.2321
>
> Unless you work out a script for that, however, you'll have to do that
> manually, starting with identifying the socket path for the agent.
>
> What problem are you trying to solve?
>
> If you are passing through one or more bastions/jumphosts then you
> should probably use -J for ProxyJump instead.
>
> /Lars


Hi Lars,

Thanks for the tip.

I found a .bash_profile script that does similar.

https://www.zonca.dev/posts/2024-10-02-ssh-agent

https://gist.github.com/darrenpmeyer/e7ad217d929f87a7b7052b3282d1b24c

---8<---

if shopt -q login_shell; then

     # SSH agent
     ssh_pid_file="${HOME}/.config/ssh-agent.pid"
     SSH_AUTH_SOCK="${HOME}/.config/ssh-agent.sock"
     if [[ -z "${SSH_AGENT_PID}" ]]; then
         # no PID exported, try to get it from pidfile
         SSH_AGENT_PID=$(cat "${ssh_pid_file}")
     fi


     if ! kill -0 "${SSH_AGENT_PID}" &> /dev/null; then
         # the agent is not running, start it
         rm "${SSH_AUTH_SOCK}" &> /dev/null
         >&2 echo "Starting SSH agent, since it's not running; this can 
take a moment"
         eval "$(ssh-agent -s -a "${SSH_AUTH_SOCK}")" || true
         echo "${SSH_AGENT_PID}" > "${ssh_pid_file}"
         ssh-add -A 2> /dev/null


         >&2 echo "Started ssh-agent with '${SSH_AUTH_SOCK}'"
     else
         >&2 echo "ssh-agent on '${SSH_AUTH_SOCK}' (${SSH_AGENT_PID})"
     fi
     export SSH_AGENT_PID
     export SSH_AUTH_SOCK


     if ! ssh-add -l &> /dev/null; then
         echo "Adding keys..."
         ssh-add ~/.ssh/id_ed25519
     fi


fi

---8<---

I wasn't sure if there was a more appropriate directory to put the .pid
and .sock as I don't see /run/user on my machine. I'm guessing that is
only created by some sort of desktop environment manager.

--
Tom