Why is ssh-agent.service running, but when I use ssh-add I get an error connecting to the agent

linuxsshssh-agentsystemd

The ssh-agent service is enabled for the user and is running without errors. (status is active).
But when I try to use ssh, I get the error Could not open a connection to your authentication agent.. I don't understand why this is happening. Earlier ssh used to work right away and I could connect to the server via my key, but now I wanted to connect and I get the error Permission denied (public key), then I tried to add it via ssh-add and I get this.
enter image description here

I googled how to fix it, and found a solution eval $(ssh-agent -s), but it only works for bash, and I'm using fish, and there I googled that you have to set environment variables manually, and then you have to do it every time, it used to work. I googled how to get it to run on its own, and googled that I need to enable the ssh-agent daemon, but I already enabled it and it works, but the error is still there, even if I run bash, same error.

System: Arch linux

Best Answer

I googled how to fix it, and found a solution eval $(ssh-agent -s), but it only works for bash, and I'm using fish

That does not apply here; that's the command to start a new instance of ssh-agent (and more importantly, to set environment variables that ssh-agent outputs when started).

Focus on the semantics of the command before the syntax; if the command does the right thing then you can adapt its syntax to any shell.

and there I googled that you have to set environment variables manually, and then you have to do it every time

You do indeed have to do it every time.

The .service provided by Arch Linux doesn't have any options that would allow ssh-agent to set environment variables for the entire user (it has PassEnvironment=, which actually works in the opposite direction than what the package maintainer thought it does, so in this case it does nothing at all), and systemd doesn't even have a generic mechanism that would allow that.

So the only way to use this service is to manually set SSH_AUTH_SOCK through your login scripts (e.g. through ~/.bashrc or ~/.profile).

it used to work

You were probably using something different, such as GNOME Keyring (which has a PAM module that allows it to automatically set the ssh-agent environment variables at login time) or pam_ssh (likewise).

Related Question