Skip to content

Add a user ssh-agent service that survives keyring restarts - #114

Merged
diegoferigo merged 2 commits into
mainfrom
diegoferigo/ssh-agent-user-service
Nov 26, 2026
Merged

diegoferigo merged 2 commits into
mainfrom
diegoferigo/ssh-agent-user-service

Conversation

@diegoferigo

@diegoferigo diegoferigo commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

What change is being made

Track a systemd user service that runs ssh-agent on $XDG_RUNTIME_DIR/ssh-agent-user.socket and exports SSH_AUTH_SOCK into the user manager once it runs, a fallback in .bashrc.d/envrc for shells that inherited a dead agent, and AddKeysToAgent yes in .ssh/config. AGENTS.md documents it in a new "SSH agent" section.

Why this change is being made

SSH_AUTH_SOCK pointed at the gnome-keyring socket. After the keyring daemon restarted without its SSH component, that socket refused connections. herdr, a long-lived process, kept using it, so ssh asked for the passphrase on every connection. Ubuntu 22.04 has no gcr-ssh-agent, so a desktop-independent agent on a fixed path is the portable choice.

The unit is not socket-activated because ssh-agent cannot take a socket from systemd. It has its own name so it does not shadow the packaged ssh-agent.service. --update only deploys the files: enable it once per machine with systemctl --user enable --now ssh-agent-user.service. Doing that from --update is left for a follow-up.

Tested

  • pixi run -q hooks: all hooks passed.
  • pixi run -q test: 391 passed (after the review fixes too).
  • The envrc fallback in a clean env -i shell: a dead SSH_AUTH_SOCK and an unset one switch to the socket, a live other agent is kept.
  • The unit runs on this machine (22.04), ssh-add loads the key and ssh ws works in BatchMode.
  • Not tested: a fresh login (whether the unit environment wins over the gnome-keyring autostart) and Ubuntu 24.04.

SSH_AUTH_SOCK pointed at the gnome-keyring socket. After the keyring daemon restarted without its SSH component, that socket refused connections and long-lived processes such as herdr kept the stale path, so ssh asked for a passphrase on every connection. Ubuntu 22.04 has no gcr-ssh-agent to replace it.

Track a systemd user service for ssh-agent on a fixed socket, the environment.d entry that exports it, a fallback in .bashrc.d/envrc for shells that inherited a dead agent, and AddKeysToAgent in ssh/config. The service is not enabled by --update.
@diegoferigo diegoferigo self-assigned this Nov 26, 2026
@diegoferigo
diegoferigo marked this pull request as ready for review November 26, 2026 17:15
The environment.d entry set SSH_AUTH_SOCK on every machine that received the files, but nothing enabled the unit. The distro launchers start an agent only when the variable is unset, so a machine without the enabled unit ended up with no agent. The envrc fallback also aborted shells running under set -e, because ssh-add -l exits 1 or 2.

Drop the environment.d entry and let the unit export the variable once the agent runs. Capture the ssh-add status without tripping set -e, restart the unit always, and fix the AGENTS.md cross-reference and the file tree.
@diegoferigo
diegoferigo merged commit d1fd73b into main Nov 26, 2026
4 checks passed
@diegoferigo
diegoferigo deleted the diegoferigo/ssh-agent-user-service branch November 26, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant