vscode: SSH Agent Forwarding not working with Remote SSH on MacOS (still a problem)
The title is the same as microsoft/vscode-remote-release#2671 because I’m having precisely the same problem, and that issue was closed without a resolution. In short, ssh agent forwarding does not work in VSCode, despite working just fine when I SSH in via the terminal. It seems to be due to a stale / cached SSH_AUTH_SOCK
.
The workaround suggested in microsoft/vscode-remote-release#2671 no longer works, but a minor modification does it for me. See below.
VSCode Version:
Version: 1.53.2
Commit: 622cb03f7e070a9670c94bae1a45d78d7181fbd4
Date: 2021-02-11T11:45:54.515Z
Electron: 11.2.1
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Darwin 20.6.0
Local OS Version: MacOS 11.6.1
Remote OS Version: Debian GNU/Linux 10 (buster) (GNU/Linux 4.19.0-18-cloud-amd64 x86_64)
Remote Extension/Connection Type: SSH
Steps to Reproduce:
- set up ssh config with a host you want to forward your ssh agent keys to and set ‘ForwardAgent yes’
- Connect via terminal ssh and perform an operation requiring your forwarded key (in my case,
ssh -T git@github.com
) - launch VSCode and validate that $SSH_AUTH_SOCK is set
- Connect via Remote-SSH, then use the VSCode terminal to run
ssh -T git@github.com
. This time, I receive a permission denied error.
Does this issue occur when you try this locally?: NA Does this issue occur when you try this locally and all extensions are disabled?: NA
Possible cause
The environment variable SSH_AUTH_SOCK
appears to be stale. A workaround is to run
export SSH_AUTH_SOCK=$(ls -t /tmp/ssh-**/* | head -1)
which sets the socket to the newest one. Note that this workaround is quite fragile, as I had to modify the previous workaround to make it work now.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 74
- Comments: 92 (9 by maintainers)
I was able to get agent forwarding working again by setting
remote.SSH.useExecServer: false
. It looks like the issue you and I are experiencing was reported here: https://github.com/microsoft/vscode-remote-release/issues/8926I kept doing this for a few days after that post, and the issue still occurred but was a lot less frequent. So I then disabled
Remote.SSH: Use Local Server
altogether and haven’t had issues since.Same issue here, closing and recreating the terminal fixes it, but terminals cerated when vscode starts have a stale SSH_AUTH_SOCK
This is now happening from my coworker (@gamegamer43) and myself since the latest update. It seems like this command is not actually running:
When I run the command above manually the key is correctly passed and the symlink exists.
If I then reload the window (Developer: Reload Window). The same thing happens again:
Output window shows:
I run that same command and it now works.
This issue seems to have started again today. vscode can’t seem to fix this issue. Keeps coming back every year. see https://github.com/microsoft/vscode/issues/175806 This suggestion worked for me, https://github.com/microsoft/vscode-remote-release/issues/9662#issuecomment-2010515888 no idea why.
Ok, I’m able to reproduce now. These are all run from within the VSCode terminal. Here’s the initial situation:
We can see that the SSH_AUTH_SOCK variable is not pointed at an extant ssh agent. Now we apply the workaround:
Was experiencing this issue today. Can confirm that
"remote.SSH.useExecServer": false,
fixes it.You are awesome!
+1 on this In my case, agent is forwarding with
ssh
in cmd (this means config file is set correctly). But in VS Code, agent not forwarding, until I changed this settingremote.SSH.useExecServer
to false.Update: unfortunately, it seems this works randomly. Now,
ssh-add -l
shows the key, butgit pull
fails with error “Permission denied (publickey).”I hit this again, apparently triggered by logging from a terminal outside of VSCode. Previous workarounds failed, including restarting the terminal.
Current workaround
SSH into machine outside of VSCode, but forwarding remote agent:
ssh -A me@my-machine
.In this terminal, echo the $SSH_AUTH_SOCK variable:
which will produce something like
In the VSCode integrated terminal, set the environment variable:
This seems to work for me as a workaround:
(link the latest ssh agent socket to $SSH_AUTH_SOCK)
(I have this problem with a linux -> linux ssh remote.)
Following this thread. I am also experiencing the same issue, trying to perform git operations in a remote connection vscode shell, with the
ForwardAgent
option set to True on my ssh_config file (MacOS 13.4.1). In a separate shell, not on vscode, I can perfectly execute this commandssh -T git@github.com
with correct authn but not on the plugin vscode terminal, where I receive aPermission denied (publickey)
error. From the log files, it seems that the vscode terminal is looking for the wrong pubkey (the login ssh key, not the github forwarded key).I can confirm that this still an issue, and that it is not specific to Mac, as we have both Mac and Linux users affected by it. On the working servers it shows the
"Update SSH_AUTH_SOCK" terminal command done
as usual, and on the non-working servers it does not. We are loading from workspace files on the remote hosts.I’ve commented on https://github.com/microsoft/vscode-remote-release/issues/7995 too but this looks like a race condition in the loading of the remote-ssh extension where the ssh socket creation code requires a terminal backend to be registered, but it is run before registerTerminalBackend is called
Hmm… I don’t think that’s the source of the problem. In particular, when I use
ssh
from the command line (using the same user/host that I use in VSC), I don’t have this issue. I don’t have any special settings forIdentityAgent
in either my local or remote machine.I’m often able to trigger the issue this way:
ssh -T git@github.com
in VSCode terminal.export SSH_AUTH_SOCK=$(ls -t /tmp/ssh-**/* | head -1)
.ssh -T git@github.com
succeeds.Developer: Reload Window
).ssh -T git@github.com
fails.Even weirder, after repeating steps 2 and 3 a few times, it’s started using the correct socket after reload.
Any news on this? This problem is still happening and I am not able to get a persistent agent with any of the newer stable or insiders versions of VSCode.
Just chiming in, I have encountered this a few times now (including today, right now lol). Works for months, and suddenly the stale SSH behavior pops up. On 1.82.0 locally. My steps to reproduce:
ssh -T git@github.com
, successful auth~/.vscode-server
Edit: to be clear, using Agent forwarding from the host that has worked previously
Any progress on this? Another VS code update and git is still broken
restarting the host system fixes this for me, so it is most likely a vscode server side issue
Workaround that finally worked for me:
I need to use the local server, so this turnaround was not a solution for me
I made a shim script that I source in my
.zshrc
to reduce the pain around that issue. It is just a variation of other solutions posted above, I only added a check that the last vscode ssh sock is a valid symlink, otherwise I need to open a new vscode window thru the Remote SSH GUI (Connect to Host…).and then in my
.zshrc
source <path_of_the_above_script.sh>
This also worked for me! 🤷
Great tip @madalinignisca , thanks! I had to remove the binaries
rm -rf ~/.vscode-server/bin
for this to workAt the remote, presumably? @micahcarroll
Just in case anyone else has this issue, and
"remote.SSH.useExecServer": false
(or other things that were mentioned above) does not fix it for them:Deleting
~/.vscode-server
fixed it for meUPDATE: it only fixed it temporarily 😢 Currently tried the
"remote.SSH.useExecServer": false
approach, and will report back if it doesn’t work for me long-termJust encountered the same problem, it seems you can add
export SSH_AUTH_SOCK=$(ls -t /tmp/ssh-**/* | head -1)
to your.bashrc
/.zshrc
/whatever to avoid being annoyed 😄(in addition to the details above)
With workaround it worked on Windows with Ubuntu Server SSH host.
Extra touches:
remote.SSH.useLocalServer
left TRUE, as it hadn’t influenced the issueremote.SSH.remoteServerListenOnSocket
set to TRUESteps (in addition to details above):
winget install Microsoft.OpenSSH.Beta
$SSH_AUTH_SOCK
in the integrated terminal within remote SSH session:export SSH_AUTH_SOCK=$(ls -td /tmp/ssh**/* | head -n1)
Just for the reference:
$SSH_AUTH_SOCK
was/run/user/1000/vscode-ssh-auth-sock-*
upon connection to remote host.$SSH_AUTH_SOCK
became/tmp/ssh-XXXXBHX4WS/agent.*
after step 2 applied.I can confirm this is not a Mac-specific problem:
Regular terminal (Windows Terminal PS session):
ssh -vT git@github.com
is authenticated successfully.Integrated terminal (VSCode Remote SSH session):
ssh-add -l
shows forwarded key fine.ssh -vT git@github.com
ends with dreaded “git@github.com: Permission denied (publickey)”.I tried lots of permutations at this point.
Steps:
$SSH_AUTH_SOCK
didn’t match to the current agent at the remote sessionexport SSH_AUTH_SOCK=$(ls -t /tmp/ssh-**/* | head -1)
Permission denied
Context:
terminal.integrated.inheritEnv
andremote.SSH.enableAgentForwarding
are both enabled in VSCode.Also facing the same issue, workaround is also working. Just wondering why VSCode server creates a symlink in
/run/user/$uid/... to /tmp/ssh*
Sounds the link is not created. From Output console:
Updating $SSH_AUTH_SOCK: ln -f -s "/tmp/ssh-zP7cLxc6yg/agent.282444" "/run/user/1001/vscode-ssh-auth-sock-956100014"
Then :
If I create the link manually, then the magic occurs and all works well.
Why not define $SSH_AUTH_SOCK to /tmp/…, or fix the link creation issue ?
in my current setup, if I have ssh local server enabled, I connect, and it doesn’t work, I can run: “Restart extension host”, and then it starts working. I might need to re-open a terminal if it was already open.
This worked for me. I had to stop disabling useLocalServer because of what @madalinignisca stated.
Can we get an update on this? I’ve never had an issue before with remote-ssh and for the last month it has been very flakey. Sometimes killing the remote server or local server works. Sometimes I need to restart my computer. I have ssh forwarding configured properly and it works from a regular terminal, just not vscode. I noticed this started happening for me when I upgrade to 1.76. I downgraded to 1.75 and that fixed it but this morning it started happening again (still on 1.75).
edit: Looks like killing every vscode window and relaunching the app has solved my issue for the time being.
Disabling “use local server” did not work for Windows client (1.76.2) and Rhel8 host. I ended up crafting the below code and injecting it into my
.bashrc
Fresh installation of MacOS Ventura, added keys to ssh-agent, installed VSCode, reusing my synced ssh config file (identical same one from my Debian Linux laptop) and ssh forwarding not working on the Mac. Works on my Linux laptop although. Nothing special on that machine, as reinstalled about 3 weeks ago, with all working, no issues.
Killing vscode server on the remote machine and reconnecting fixed the issue. I think that vscode server running on the remote machine is expecting the old agent connection.
Is there a way to make the running process exit when vscode disconnects?
This is also happening in windows 10.
As refered as previous comment, below command was logged but not executed.
The symbolic link was not created on the destination.
And ssh-agent in local is not forwarded to remote.
After run the below command, symbolic link was created and ssh-agent was forwarded successfully.
Below is My VSCode version