129

ssh-add alone is not working:

Error connecting to agent: No such file or directory

How should I use that tool?

4
  • 2
    Dear moderators, I was ask to moved this question to this site from StackOverflow, but then I got downvote again? Can you at least tell me why? Commented Aug 24, 2018 at 7:45
  • 1
    I'm guessing you got the downvotes because you fail to explain what your current setup is (with regards to SSH and the SSH agent), and what you may have changed/done when it suddenly no longer worked the way it used to. You also don't mention the command that gives rise to the error message (so we have to assume it's just a plain ssh-add with no options). There is also no reference to the tmux tag in the question itself. For more feedback, ask about your question on the Meta site.
    – Kusalananda
    Commented Aug 24, 2018 at 7:58
  • 1
    Please read our FAQ, take the tour and lurk for a while to get a feel of the forum use. Commented Aug 24, 2018 at 8:07
  • 3
    Thank you for all the moderators who helped me to improve this questions! Commented Aug 24, 2018 at 12:27

5 Answers 5

140

The client tool ssh-add wants to communicate with the background process ssh-agent. You need to start the ssh-agent process first. On Linux, an Shell uses the environment variables SSH_AUTH_SOCK and SSH_AGENT_PID to identify the correct process to talk to.

You can start ssh-agent in multiple ways. Either by starting a new shell

ssh-agent bash

or by evaluating the script returned by ssh-agent in your current shell.

eval "$(ssh-agent)"

I suggest using the second method, because you keep all your history and variables.

3
  • 6
    Thanks it works for me! Since I'm using fish, the command is ssh-agent fish. Commented Aug 24, 2018 at 7:43
  • 3
    what does this do and why is it necessary? Commented Jan 21, 2020 at 15:16
  • @WillSheppard It starts a new session with a running ssh-agent, which manages your SSH keys.
    – Panki
    Commented Jan 21, 2020 at 18:33
58

In Windows PowerShell (run as admin):

  1. Check the current status of ssh-agent:

    Get-Service | ?{$_.Name -like '*ssh-agent*'} | select -Property Name, StartType, Status

  2. Enable the Service if it is disabled:

    Set-Service -Name ssh-agent -StartupType Manual

  3. Start the Service:

    Start-Service ssh-agent

  4. Add your key as before:

    ssh-add <path to the key>

0
36

The SSH agent is not running, or the environment variables that it sets are not available in the current environment (most importantly SSH_AUTH_SOCK), or they are set incorrectly (pointing to a dead agent).

You could start the agent in the current shell session using

eval "$(ssh-agent)"

or start a new shell session through the agent using

ssh-agent fish

(replace fish with whatever shell you are using). But since you say that you used to be able to use ssh-add without this, it leads me to believe that you've accidentally killed the agent (or it has terminated due to some other reason). The error message makes me think that the SSH_AUTH_SOCK environment variable is actually set, but that ssh-add can't find a valid communication socket at that path.

It would not surprise me if your usual way of doing things would work again if you completely logged out and logged in again, or rebooted the machine.

1
  • Yes, I think I killed the agent accidentally, perhaps due to restarting the machine. Commented Aug 24, 2018 at 8:26
2

MacOS

Big Sur

In my edge case, the problem was I had somehow turned off the service, or it was trying to use bash instead of zsh. Not really sure, but this did the trick:

ssh-agent zsh

1
  • That works on zsh!
    – abu_bua
    Commented Dec 19, 2022 at 3:57
-1

What I just stumbled uppon is that I couldn't add the key no matter what I tried. Reinstall openssh, start it in systemd, stop, restart, start ssh-agent, kill all ssh-agent processess running and try with a fresh one... I would always end up with that file or directory not found. In the end I removed all SSH keys that I had in my ~/.ssh, created a fresh one that I custom named, and it still didn't work. What did work is when I renamed that key to id_rsa. For some reason the agent picked that up automatically without the need to add the key to it and started working. Happened on Arch.

3
  • This does not seem to be the issue in this particular question though. Here, the ssh-add command can't communicate with the SSH agent. In your case, the ssh-add command could not read your key (possibly due to a permission or ownership issue; you may have used sudo to generate the key or to create the ~/.ssh directory).
    – Kusalananda
    Commented Dec 29, 2022 at 10:18
  • The author stated that "ssh-add alone is not working: Error connecting to agent: No such file or directory", which is a vague question by itself and can be a variety of things and which is also what I experienced. I do not understand how this is a different topic. @Kusalananda Commented Feb 2, 2023 at 12:19
  • The error message that is given in the question is exactly what the SSH client says when it can not find the socket file which the $SSH_AUTH_SOCK environment variable points to. If the variable is unset (if you never started the agent), ssh-add says Could not open a connection to your authentication agent.
    – Kusalananda
    Commented Feb 2, 2023 at 12:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.