SSH Agent Config Fragment

SSH Agent

This is the fragment to make sure the ssh-agent is running when you open the terminal.

Put this in ~/.config/fish/conf.d/ssh-agent.fish.

if status is-login
    and status is-interactive
    if test -n $SSH_KEYS_TO_AUTOLOAD
        keychain --eval $SSH_KEYS_TO_AUTOLOAD | source
    else
        echo "SSH_KEYS_TO_AUTOLOAD environmentvariable not set"
        echo "add key-file-name(s) to this variable"
    end
end

Note: The default key type has changed so the default file name is no longer id_rsa it's id_ed25519.

The documentation for the status comand says that is-interactive is actually a sub-command (not an option, although I'm calling it like it is one) and that it:

Returns 0 if fish is interactive - that is, connected to a keyboard.

Which is an oddly unintuitive description. I think it means that it won't pass the conditional if I try to run the fragment as a function… I can't remember why I used it, exactly ,but I think it's something you need if you want it to be loaded when you login.

Although the name keychain makes it sound like it's a general password manager, according to the man-page it's meant specifically to manage the ssh-agent. It will check if you already have an ssh-agent running and only start one if you don't. This way you don't end up starting one for every screen you open up.

The --eval option sends the SSH-Agent message to stdout, --quiet suppresses other messages, --quick tells it to use a running ssh-agent if it finds one without verifying the keys and then we pipe it to source to load the environment.

The SSH_KEYS_TO_AUTOLOAD Variable

Note: This expects you to put the names of keys into the SSH_KEYS_TO_AUTOLOAD variable at the command line. It isn't set in the code-snippet so that you can change it/them by updating the environment variable instead of updating the file, but this means you have to remember to set it.

Add A Key

set --universal --append SSH_KEYS_TO_AUTOLOAD ~/.ssh/<key-file>

Remove A Key

set --universal --erase SSH_KEYS_TO_AUTOLOAD[<index of key>]

Links