How to start ssh-agent as systemd service? There are some suggestions in the net, but they are not complete.
How to add automatically unencrypted keys if ssh-agent service was started successfully? Probably, adding keys from the list of
~/.ssh/.session-keyswould be good.How to set
SSH_AUTH_SOCKin any login session afterwards? The most correct way is to push it from ssh-agent service to systemd-logind service (have no idea if it's ever possible). The plain naive way is just add it to/etc/profile.
-
1What's missing from the suggestions on the net?Mark Stosberg– Mark Stosberg2017-01-24 21:43:14 +00:00Commented Jan 24, 2017 at 21:43
Add a comment
|
2 Answers
- To create a systemd
ssh-agentservice, you need to create a file in~/.config/systemd/user/ssh-agent.servicebecausessh-agentis user isolated.[Unit] Description=SSH key 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 - Add
toSSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket"~/.config/environment.d/ssh_auth_socket.conf. - Finally enable and start this service.
systemctl --user enable --now ssh-agent - And, if you are using ssh version higher than 7.2.
This will instruct the ssh client to always add the key to a running agent, so there's no need toecho 'AddKeysToAgent yes' >> ~/.ssh/configssh-addit beforehand.
Note that when you create the ~/.ssh/config file you may need to run:
chmod 600 ~/.ssh/config
or
chown $USER ~/.ssh/config
Otherwise, you might receive the Bad owner or permissions on ~/.ssh/config error.
-
launchdon OS X is set to start ssh-agent when a Unix socket is accessed (and theSSH_AUTH_SOCKvariable is prepopulated with the path...) (likeinetd, but a Unix socket). This seems possible withsystemdas well. (Whether a system-wide service is an option for a per-user service might be interesting to see....)Gert van den Berg– Gert van den Berg2018-02-26 10:49:32 +00:00Commented Feb 26, 2018 at 10:49 -
I get
Failed to execute operation: Process org.freedesktop.systemd1 exited with status 1when I runsystemctl --user enable ssh-agenton centos7scarba05– scarba052019-03-18 08:51:20 +00:00Commented Mar 18, 2019 at 8:51 -
2You can make ssh-agent exit after your last session by adding
After=systemd-user-sessions.service user-runtime-dir@%i.service dbus.serviceandRequires=user-runtime-dir@%i.serviceto the[Unit]section.Karl Bunch– Karl Bunch2021-04-18 11:48:58 +00:00Commented Apr 18, 2021 at 11:48 -
@AlecMev
Environmentseems to be useful for other services started with systemd that might want to knowSSH_AUTH_SOCK.tsj– tsj2021-04-30 20:55:15 +00:00Commented Apr 30, 2021 at 20:55 -
This setup persists through reSTART in Arch (i.e., logout/login, or i3
$mod<Shift>e). It does not survive arebootor a 'Reddit-tier IT Support stock answer' (i.e., "Turn it off then back on").GT.– GT.2023-04-23 05:36:14 +00:00Commented Apr 23, 2023 at 5:36
This is not supported if you are using centos 7 because it will not support the --user flag of systemctl. See this centos bug report, Systemd User Support is Broken on Delivery