Mastodon hachyterm.io

When you create a new SSH key for a remote connection, you will need to add that key to the ssh-agent.

For example, the GitHub documentation tells you to start the ssh-agent in the background:

$ eval "$(ssh-agent -s)"
> Agent pid 59566

And then add the key:

$ ssh-add ~/.ssh/id_rsa

What happens when you use fish shell instead of the standard bash shell?

$ eval "$(ssh-agent -s)"
> fish: $(...) is not supported. In fish, please use '(ssh-agent)'.

Ok, fish, I now remember that your syntax is not bash-compatible …

$ eval "ssh-agent -s"
> SSH_AUTH_SOCK=/tmp/ssh-qSWfVCBEZQcc/agent.4858; export SSH_AUTH_SOCK;
SSH_AGENT_PID=4859; export SSH_AGENT_PID;
echo Agent pid 4859;

Great. Can I now add my key to ssh-agent?

$ ssh-add ~/.ssh/id_rsa
> Could not open a connection to your authentication agent.

Why?

Fish doesn’t use the export command, so when you run the eval command it doesn’t set environment variables for ssh-agent.

Instead you have to use the set -x command.

$ set -x SSH_AUTH_SOCK /tmp/ssh-qSWfVCBEZQcc/agent.4858
$ set -x SSH_AGENT_PID 4859

Alternative:
Use the csh-style option -c (see Arch Wiki):

$ eval (ssh-agent -c)
> Agent pid 4859

Now you can run ssh-add.

I like fish but sometimes it’s frustrating that you have to remember that it is different from bash.

Start ssh-agent automatically

The problem is that every time you reboot, your ssh-agent-session is lost and you have to add the key again.

As a workaround you can install a small utility called fish_ssh_agent (or: fish-sshagent) which will start the ssh-agent when running fish shell.

Add this line to ~/.ssh/config:

AddKeysToAgent yes

Further Reading