Skip to content

Add compatibility with systemd user unit ssh-agent.service #4

@raffitz

Description

@raffitz

Is your feature request related to a problem? Please describe.

As per the Arch Wiki's article on SSH Keys, it is possible to start a systemd user unit (ssh-agent.service), to start an ssh-agent on login.

Describe the solution you'd like

I would like the module to check for the existence of the systemd unit's authentication socket, and use it if it exists. If not, proceed with the current behaviour.

Describe alternatives you've considered

My current workaround is to have file ~/.ssh-agent set to mode 0400, with the following content:

#!/bin/zsh

if [[ ! -S "$SSH_AUTH_SOCK" ]]; then
	if [[ ! -n "$XDG_RUNTIME_DIR" ]] || [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
		export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
		if [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
			mkdir -p "$XDG_RUNTIME_DIR"
			chmod 700 "$XDG_RUNTIME_DIR"
		fi
	fi
	if [[ -S "$XDG_RUNTIME_DIR/ssh-agent.socket" ]]; then
		export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
	else
		if command -v ssh-agent &>/dev/null; then
			eval "$(ssh-agent -s)"
		fi
	fi
fi

(the shebang is only for auto syntax highlighting detection)
If the SSH_AUTH_SOCK variable isn't already set (which it will for ssh logins with agent forwarding), check if XDG_RUNTIME_DIR is set. If not, set it to the default value. If the directory doesn't exist, it is created (I don't think this ever happens, and it might yield permission issues if it does. I'm not too concerned).
If, within the XDG_RUNTIME_DIR, there is an ssh agent auth socket, then use it. Otherwise, spin up a new ssh-agent process.

Additional context

This keeps the ssh-agent process being managed by systemd. If for any reason I want to restart it, I can issue a systemctl restart command. If I want to check on its status, I can run systemctl status.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions