ssh-add alone is not working:
Error connecting to agent: No such file or directory
How should I use that tool?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to initialize the agent first.
You can do this in multiple ways.
Either by starting a new shell
ssh-agent bash
or by evaluating the variables 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.
Method 2
In Windows PowerShell (run as admin):
-
Check the current status of ssh-agent:
Get-Service | ?{$_.Name -like '*ssh-agent*'} | select -Property Name, StartType, Status -
Enable the Service if it is disabled:
Set-Service -Name ssh-agent -StartupType Manual -
Start the Service:
Start-Service ssh-agent -
Add your key as before:
ssh-add <path to the key>
Method 3
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.
Method 4
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0