ssh-agent not getting set up (SSH_AUTH_SOCK, SSH_AGENT_PID env vars not set)

For the

$ eval `ssh-agent -s`

construct to work when put in a “startup script”, your session, and ultimately the terminal where you expect the environment, must be descendants (by fork and exec) of that script. The reason is that the output of ssh-agent -s, when evaluated, sets environment variables in the shell calling eval. Form there, they may be handed down, and they may be lost on the way as well.

So if ssh-agent is run by script A somewhere during login, but the terminal B in which you start you shell script is not a descendant of A, then you cannot see the environment in B.

If you happen to have ssh-agent started as systemd --user service, then you may have to use convention instead: Don't let ssh-agent specify the variables, but use common knowledge when starting the agent, and when starting the session. E.g., my ~/.config/systemd/user/ssh-agent.service looks like this:

[Unit]
Description=SSH agent

[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK

[Install]
WantedBy=default.target

And in my ~/.profile I have the line

export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket"

Note that %t in the former corresponds to ${XDG_RUNTIME_DIR} in the latter.

Note: I'm not happy about this!