For the longest time I just ran `eval ssh-agent` in the console prior to starting a GUI, but I sometimes need/want to use a graphical login manager, and this created a problem with setting/keeping the SSH_AUTH_SOCK environment variable across applications in the GUI.
I needed a better way, and I wanted a simple one. This solution is based on an idea from penguin359 at serverfault.com:
Firstly, set SSH_AUTH_SOCK to a known (static) value, like /tmp/username.agent, in the shell's rc or profile. ssh will then always try to use that socket.
Second, when starting the agent, use the -a flag to tell it where to put the socket:
ssh-agent -a /tmp/username.agent
Make a script or alias for this to make day-to-day life easier.
I could of course check if ssh-agent is running and only start it if not, and put that in the shell's rc file, but I opted to just keep this as simple as possible.
Now, this works well as long as I start all applications from the shell, but one of the perks of using a full desktop environment like KDE/Plasma is to have a launcher.
To make SSH_AUTH_SOCK available to KDE, I export it from a script in .config/plasma-workspace/env/:
% cat ~/.config/plasma-workspace/env/sshagent.sh
export SSH_AUTH_SOCK="/tmp/username.agent