Score:1

The ssh-agent process is not visible to the user who created it but is visible to root

tm flag

I have adopted the following snippet from Visual Studio Code's documentaion to create an ssh-agent on login:

if [ -z "$SSH_AUTH_SOCK" ]; then
   # Check for a currently running instance of the agent
   RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
   if [ "$RUNNING_AGENT" = "0" ]; then
        # Launch a new instance of the agent
        ssh-agent -s &> .ssh/ssh-agent
   fi
   eval `cat .ssh/ssh-agent`
fi

However, I see that on each login, a new ssh-agent is created when my ~/.zprofile is sourced, even if I have a few other sessions open.

While debugging the issue, I realized that a call to eval "$(ssh-agent -s)" creates the agent and prints its PID on the terminal. However, when I invoke ps, pgrep, htop, or similar commands, they do not show the ssh-agent process. If I rerun the same commands with sudo, I can find the process.

What can I do to make the ssh-agent process visible to the user who called it, so they can use the same agent in all their sessions?

Score:1
gu flag

I have something similar that's much simpler using a user service.

[Unit]
Description=SSH Authentication Agent
Documentation=man:ssh-agent
Requires=run-user-%U.mount

[Service]
Type=exec
ExecStart=/usr/bin/ssh-agent -a %t/ssh-agent.sock -D
Restart=on-failure

RuntimeDirectory=ssh
RuntimeDirectoryMode=0700
KillMode=process
KillSignal=SIGTERM

[Install]
WantedBy=default.target

You just have to enable the service for the user (systemctl --user enable --now ssh-agent.service) and it will start with the first session. If you need to enable it for all users, use systemctl --global enable ssh-agent.service.

Add this to ~/.zprofile (or /etc/zsh/zprofile for all users):

# SSH_AGENT_PID isn't really necessary since it's mainly used to terminate the agent with `ssh-agent -k`
typeset -x SSH_AGENT_PID="$( systemctl --user show --property=MainPID --value ssh-agent.service )"
typeset -x SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh/ssh-agent.sock"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.