How can I get gpg-agent to work with ssh keys? The following script that I’ve put in /etc/profile.d sets it up, but I’d like to move my user daemons (such as gpg-agent) over to systemd --user.

$ cat gpg-agent.sh
#!/bin/sh

envfile="${HOME}/.gnupg/gpg-agent.env"
if test -f "$envfile" && kill -0 $(grep GPG_AGENT_INFO "$envfile" | cut -d: -f 2) 2>/dev/null; then
    eval "$(cat "$envfile")"
else
eval "$(gpg-agent --daemon --enable-ssh-support --write-env-file "$envfile")"
fi
export GPG_AGENT_INFO

I’ve written the following user service, and it should do the same thing, but it doesn’t seem to work:

$ cat ~/.config/systemd/user/gpg-agent.service
[Unit]
Description=GnuPG private key agent
Wants=environment.target
Before=environment.target
IgnoreOnIsolate=true

[Service]
Type=forking
Environment=GPG_ENVFILE=%t/gpg-agent.info
ExecStart=/usr/bin/gpg-agent --daemon --enable-ssh-support --use-standard-socket --write-env-file ${GPG_ENVFILE} ExecStartPost=/bin/sh -c "xargs systemctl --user set-environment < ${GPG_ENVFILE}"
ExecStopPost=/bim/rm %t/gpg-agent.info
Restart=on-abort

[Install]
WantedBy=default.target

Both the script and the service file start gpg, create an environment file, and export the variables. But for some reason, gpg-agent doesn’t store keys or anything if run as a service. I don’t know why.

Can anyone help?

Reply via email to