I've got an issue where ssh-agent does not start automatically on login. I have tried writing a service, configuring a systemd autostart script and a bunch of other things.
Another thing I've noticed, is that even if I run eval $(ssh-agent -s)
, the agent will only work until I restart the terminal. Closing and opening the terminal again, then running ssh-add -l
gives the dreaded Could not open a connection to your authentication agent.
message.
The service that I wrote, with some guidance, looks like this:
[Unit]
Description=SSH agent (ssh-agent)
[Service]
Type=simple
Environment="SSH_AUTH_SOCK=%t/ssh-agent.socket"
Environment="DISPLAY=:0"
ExecStart=ssh-agent -D -a $SSH_AUTH_SOCK
ExecStop=kill -15 $MAINPID
[Install]
WantedBy=default.target
However, it doesn't work, despite starting and running successfully. Here's the systemctl
output:
>> systemctl --user status ssh-agent
● ssh-agent.service - SSH agent (ssh-agent)
Loaded: loaded (/home/david/.config/systemd/user/ssh-agent.service; enabled; preset: enabled)
Active: active (running) since Mon 2023-01-30 20:08:14 GMT; 9min ago
Process: 10959 ExecStartPost=/bin/sleep 3 (code=exited, status=0/SUCCESS)
Main PID: 10958 (ssh-agent)
Tasks: 1 (limit: 18864)
Memory: 840.0K
CPU: 8ms
CGroup: /user.slice/user-1000.slice/[email protected]/app.slice/ssh-agent.service
└─10958 ssh-agent -D -a /run/user/1000/ssh-agent.socket
Jan 30 20:08:11 pleb-box systemd[839]: Starting SSH agent (ssh-agent)...
Jan 30 20:08:11 pleb-box ssh-agent[10958]: SSH_AUTH_SOCK=/run/user/1000/ssh-agent.socket; export SSH_AUTH_SOCK;
Jan 30 20:08:11 pleb-box ssh-agent[10958]: echo Agent pid 10958;
Jan 30 20:08:14 pleb-box systemd[839]: Started SSH agent (ssh-agent).
>> echo $SSH_AGENT_PID
>> echo $SSH_AUTH_SOCK
>> ssh-add -l
Could not open a connection to your authentication agent.
As you can see, it doesn't even export the two environment variables, despite it being the second log line. Trying to echo
them, returns empty values.
I use zsh, but this happens on bash too.
Here's some of my system information:
>> uname -a
Linux pleb-box 5.15.89-1-MANJARO #1 SMP PREEMPT Wed Jan 18 20:37:11 UTC 2023 x86_64 GNU/Linux
>> zsh --version
zsh 5.9 (x86_64-pc-linux-gnu)
>> konsole --version
konsole 22.12.1
>> systemctl --version
systemd 252 (252.4-2-manjaro)
+PAM +AUDIT -SELINUX -APPARMOR -IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT -QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK +XKBCOMMON +UTMP -SYSVINIT default-hierarchy=unified
>> ssh -v localhost
OpenSSH_9.1p1, OpenSSL 3.0.7 1 Nov 2022
...
Any help would be greatly appreciated.
ssh-agent
isn't supposed to start at boot. Rather, it's supposed to be an early process leader when you log in so that it can make$SSH_AUTH_SOCK
and$SSH_AGENT_PID
available to processes via the environmentType=simple
makes systemd think the relevant process exited. The agent does persist between terminal sessions, the variables don't; they are important. Please see the first part of this answer.