I trying to use the openssh ssh agent systemd user service on Ubuntu 24.04.
Starting from Ubuntu 16.10 and onwards, the openssh-client
package contains a ssh-agent.service
user service file :
$ lsb_release -sr
No LSB modules are available.
24.04
$ dpkg -S user/ssh-agent.service
openssh-client: /usr/lib/systemd/user/ssh-agent.service
$ systemctl --user cat ssh-agent.service
# /usr/lib/systemd/user/ssh-agent.service
[Unit]
Description=OpenSSH Agent
Documentation=man:ssh-agent(1)
Before=graphical-session-pre.target
ConditionPathExists=/etc/X11/Xsession.options
Wants=dbus.socket
After=dbus.socket
[Service]
# If you need to pass extra arguments to ssh-agent, you can use "systemctl
# --user edit ssh-agent.service" to add a drop-in unit with contents along
# these lines:
# [Service]
# ExecStart=
# ExecStart=/usr/lib/openssh/agent-launch start -- -t 1200
ExecStart=/usr/lib/openssh/agent-launch start
ExecStopPost=/usr/lib/openssh/agent-launch stop
$
So I tried to start it but it is not active :
$ systemctl --user start ssh-agent.service
$ systemctl --user is-active ssh-agent.service
inactive
$
After some research I have set the SSH_AUTH_SOCK
variable :
$ ssh myUbuntu-24-04-Server
$ export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/openssh_agent
$ systemctl --user stop ssh-agent.service
$ systemctl --user start ssh-agent.service
$ ls $SSH_AUTH_SOCK
ls: cannot access '/run/user/1000/openssh_agent': No such file or directory
$ systemctl --user is-active ssh-agent.service
inactive
$ systemctl --user status ssh-agent.service
○ ssh-agent.service - OpenSSH Agent
Loaded: loaded (/usr/lib/systemd/user/ssh-agent.service; static)
Active: inactive (dead)
Docs: man:ssh-agent(1)
Feb 21 17:41:56 myUbuntu-24-04-Server systemd[118809]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:08:15 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:10:31 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:11:24 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:18:49 myUbuntu-24-04-Server systemd[119442]: Started ssh-agent.service - OpenSSH Agent.
$
However, this service starts fine on a previous Ubuntu LTS (22.04) :
$ ssh myUbuntu-22-04-Server
$ lsb_release -sr
22.04
$ export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/openssh_agent
$ systemctl --user start ssh-agent.service
$ systemctl --user status ssh-agent.service
● ssh-agent.service - OpenSSH Agent
Loaded: loaded (/usr/lib/systemd/user/ssh-agent.service; static)
Active: active (running) since Fri 2025-02-21 18:40:55 CET; 4min 17s ago
Docs: man:ssh-agent(1)
Main PID: 23068 (ssh-agent)
Tasks: 1 (limit: 19005)
Memory: 1.1M
CPU: 6ms
CGroup: /user.slice/user-1000.slice/[email protected]/app.slice/ssh-agent.service
└─23068 ssh-agent -D -a /run/user/1000/openssh_agent
Feb 21 18:40:55 myUbuntu-22-04-Server systemd[22133]: Started OpenSSH Agent.
Feb 21 18:40:55 myUbuntu-22-04-Server agent-launch[23070]: dbus-update-activation-environment: setting SSH_AUTH_SOCK=/run/user/1000/openssh_agent
Feb 21 18:40:55 myUbuntu-22-04-Server agent-launch[23070]: dbus-update-activation-environment: setting SSH_AGENT_LAUNCHER=openssh
Feb 21 18:40:55 myUbuntu-22-04-Server agent-launch[23068]: SSH_AUTH_SOCK=/run/user/1000/openssh_agent; export SSH_AUTH_SOCK;
Feb 21 18:40:55 myUbuntu-22-04-Server agent-launch[23068]: echo Agent pid 23068;
$ ssh-add -l
The agent has no identities.
$
EDIT0 : My bad, setting the SSH_AUTH_SOCK
variable manually was not necessary.
I re-tested on Ubuntu 22.04 and it works fine without setting the
SSH_AUTH_SOCK
variable manually.
But on Ubuntu 24.04, here is what I get :
$ ssh -X myUser@myUbuntu-24-04-Server
myUser@myUbuntu-24-04-Server:~$ echo $XDG_RUNTIME_DIR
/run/user/1000
myUser@myUbuntu-24-04-Server:~$ echo $SSH_AUTH_SOCK
myUser@myUbuntu-24-04-Server:~$ grep use-ssh-agent /etc/X11/Xsession.options
use-ssh-agent
myUser@myUbuntu-24-04-Server:~$ unset SSH_AUTH_SOCK
myUser@myUbuntu-24-04-Server:~$ systemctl --user start ssh-agent.service
myUser@myUbuntu-24-04-Server:~$ systemctl --user status ssh-agent.service
○ ssh-agent.service - OpenSSH Agent
Loaded: loaded (/usr/lib/systemd/user/ssh-agent.service; static)
Active: inactive (dead)
Docs: man:ssh-agent(1)
Feb 21 17:41:56 myUbuntu-24-04-Server systemd[118809]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:08:15 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:10:31 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:11:24 myUbuntu-24-04-Server systemd[119096]: Started ssh-agent.service - OpenSSH Agent.
Feb 21 18:18:49 myUbuntu-24-04-Server systemd[119442]: Started ssh-agent.service - OpenSSH Agent.
Feb 24 17:54:39 myUbuntu-24-04-Server systemd[151016]: Started ssh-agent.service - OpenSSH Agent.
Feb 24 17:56:01 myUbuntu-24-04-Server systemd[151016]: Started ssh-agent.service - OpenSSH Agent.
Feb 24 18:15:38 myUbuntu-24-04-Server systemd[151355]: Started ssh-agent.service - OpenSSH Agent.
Feb 24 18:17:06 myUbuntu-24-04-Server systemd[151355]: Started ssh-agent.service - OpenSSH Agent.
myUser@myUbuntu-24-04-Server:~$ systemctl --user is-active ssh-agent.service
inactive
myUser@myUbuntu-24-04-Server:~$ ssh-add -l
Could not open a connection to your authentication agent.
myUser@myUbuntu-24-04-Server:~$
Can you help me ?
The reason that the service isn't "Active" is because of several reasons:
use-ssh-agent
defined in/etc/X11/Xsession.options
.$SSH_AUTH_SOCK
prior to startingssh-agent.service
.SSH_AUTH_SOCK
is also defined within Systemd --user scope bygpg-agent-ssh.socket
.Let's first look at the
ssh-agent.service
unit file. When the unit is started, the script/usr/lib/openssh/agent-launch
is called by the following line:Looking at that script, we have the following:
We can glean a few things from this:
$XDG_RUNTIME_DIR
needs to be defined as an environment variable$SSH_AUTH_SOCK
needs to be undefineduse-ssh-agent
needs to be defined in/etc/X11/Xsession.options
Next, look at the output of
systemctl --user show-environment
. This will show your Systemd --user scope environment variables:Notice that
SSH_AUTH_SOCK
is defined. This will preventssh-agent.service
from starting and defining this variable.When
ssh-agent
is run, it wants to defineSSH_AUTH_SOCK
. Quoting the ssh-agent(1) manpage:Anyway, in the default state for Ubuntu 24.04,
SSH_AUTH_SOCK
is defined from a socket unit file calledgpg-agent-ssh.socket
, which is why it's available in Systemd --user scope and visible in the output ofsystemctl --user show-environment
. Take a look at the contents of thegpg-agent-ssh.socket
unit file and you'll see howSSH_AUTH_SOCK
is defined withExecStartPost=
:The Fix
For Ubuntu 24.04, to enable
ssh-agent
you need to do the following:Define
use-ssh-agent
in/etc/X11/Xsession.options
:Prevent
SSH_AUTH_SOCK
from being defined in Systemd --user scope at boot bygpg-agent-ssh.socket
. This can be done by either of the following options:Option 1:
Create an
override.conf
file that clearsExecStartPre
,ExecStartPost
,ExecStopPre
, andExecStopPost
with the following steps:Run
systemctl --user edit gpg-agent-ssh.socket
to edit and create anoverride.conf
file. Add the following:Save and exit.
Then reload the configuration changes with
systemctl --user daemon-reload
.Option 2:
Simply mask
gpg-agent-ssh.socket
. This will prevent the socket from being created.Run
systemctl --user mask gpg-agent-ssh.socket
.Then reload the configuration changes with
systemctl --user daemon-reload
.Unset
SSH_AUTH_SOCK
in Systemd --user scope, which was previously defined withgpg-agent-ssh.socket
unit file. You need to unset this prior to startingssh-agent.service
:Add an
[Install]
section in anoverride.conf
file forssh-agent.service
. This will allow you to "enable" the service and have it start at boot.Then reload the configuration changes with
systemctl --user daemon-reload
.Enable and start
ssh-agent.service
Export
SSH_AUTH_SOCK
in.bashrc
so that this environment variable is available globally. Otherwisessh-add
will not see an available socket since it cannot see Systemd --user scope variables.Add the following to the bottom of
.bashrc
:export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/openssh_agent"
Then reload
.bashrc
:After the above configuration:
You will have a global
SSH_AUTH_SOCK
variable:This will match the Systemd --user scope variable:
ssh-agent
will be enabled and active (running):The output of
ssh-add -l
will not error out: