sfsacc
sfsacc

Reputation: 81

Error "communication with agent failed" when SSH_AUTH_SOCK is set but SSH_AGENT_PID is not set

I'm using Visual Studio Code v1.67 on Windows 10 v21H2 to connect to an Ubuntu Server 22.04 machine. I'm running an SSH agent (v8.1) on Windows. I've set ForwardAgent yes in my Windows config file. When I start an Ubuntu terminal, the agent appears to be running in Ubuntu and has my Windows key. I can see it via ssh-add -l. SSH_AUTH_SOCK is set to something like /run/user/1000/vscode-ssh-auth-sock-12345678 (a link to /tmp/ssh-XXXXABCDEF/agent.1234), but SSH_AGENT_PID is not set.

If I try ssh -Tvvv [email protected] it does not use the key in the agent and asks for my Ubuntu key password. In the verbose SSH output are the messages:

debug2: get_agent_identities: ssh_agent_bind_hostkey: communication with agent failed
debug1: get_agent_identities: ssh_fetch_identitylist: communication with agent failed

I can start a new agent and add the key on my Ubuntu machine using eval `ssh-agent`; ssh-add. This sets both SSH_AUTH_SOCK and SSH_AGENT_PID. SSH_AUTH_SOCK now has a path like /tmp/ssh-XXXXXXABCDEF/agent.1234. Now, when I test with ssh -T [email protected] it works. I don't have to enter my password. I can do things like git push to a remote via ssh without entering a password.

I would prefer to use the Windows agent key forwarded by VS Code. That way I never have to enter a password. I don't see why ssh cannot access it. This worked before I upgraded from Ubuntu 21.10 to 22.04. Is the problem with openssh? It's v8.9 on Ubuntu Server 22.04. I think it's v8.4 on US 21.10. Or is the problem with VS Code? Could the problem be that VS Code is not setting SSH_AGENT_PID when forwarding the key? Is it necessary to set SSH_AGENT_PID when using the agent?

Edited: I noticed that if I set just SSH_AUTH_SOCK from ssh-agent, and leave SSH_AGENT_PID empty, and then add the key on my Ubuntu machine, it works (ssh -T [email protected], etc). So, it seems SSH_AGENT_PID is not necessary in some cases. The problem could be there's something wrong with the SSH_AUTH_SOCK file created when VS Code forwards the agent.

Help appreciated.

Upvotes: 4

Views: 4281

Answers (1)

Field
Field

Reputation: 459

Same problem occurred to me yesterday when I was trying to forward my key to remote server in VSCode. It turned out the default installation is too old and incompatible with the server's protocol.

Here is how I solve it:

  1. Open PowerShell with elevated privileges

  2. Uninstall the default installation using:

    Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
    
  3. (Optional) Restart if you see something like UninstallPending instead of NotPresent

  4. Check if the default OpenSSH is completely uninstalled

    Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
    
  5. Install latest client only:

    winget install Microsoft.OpenSSH.Beta --override ADDLOCAL=Client
    

    or follow Install Win32 OpenSSH Using MSI if you don't have winget

  6. Check installation and verify the version >= 8.9.1.0:

    Get-Command ssh-agent
    
  7. (Optional) Start the ssh-agent service if you

    Start-Service ssh-agent
    Set-Service ssh-agent -StartupType Automatic
    Get-Service ssh-agent
    
  8. (Optional) Add following lines to %USERPROFILE%/.ssh/config:

    Host server
      ForwardAgent yes
      AddKeysToAgent yes
    

Upvotes: 11

Related Questions