Terminal ssh works, but VS Code Remote - SSH cannot connect or keeps dropping
Could not establish connection to "my-host"Short answer
Plain ssh working proves the network and key are fine, so the fault is in what VS Code adds: a stale server on the host, a login prompt VS Code cannot show, the wrong ssh client, key file modes, or a cluster that sends each connection to a different node. Open Remote-SSH: Show Log first.
Fix it fast
1Find out which cause you have
Prove ssh works outside VS Code, then open the extension's log from the Command Palette (Remote-SSH: Show Log). The last lines say which stage failed.
ssh my-host echo ok # Command Palette (F1): Remote-SSH: Show Log2Match what you see, then jump to its fix
- 1
The log ends with The VS Code Server failed to start, or the session worked yesterday and fails after an update.A stale or broken VS Code Server on the hostFix - 2
The connection hangs at the connecting step, or a passphrase or host-key question never appears.The login waits for something VS Code cannot showFix - 3
The log names an ssh executable you did not expect, or reports that ssh cannot be found.VS Code uses a different ssh client or config than your terminalFix - 4
WARNING: UNPROTECTED PRIVATE KEY FILE! in the log.Key or .ssh permissions are too openFix - 5
Connections drop or the second connection lands on a host without the server that the first one started.The SSH endpoint is a cluster that rotates nodesFix
- 1
What does the error look like?
Could not establish connection to "my-host"Could not establish connection to "my-host": The VS Code Server failed to start.WARNING: UNPROTECTED PRIVATE KEY FILE!
Cause 1 of 5
A stale or broken VS Code Server on the host
1Check
Look at what is installed under your home directory on the host.
ssh my-host 'ls -la ~/.vscode-server'2Fix
Run Remote-SSH: Kill VS Code Server on Host from the Command Palette. If that does not help, remove the server directory and let VS Code install it again.
ssh my-host 'rm -rf $HOME/.vscode-server'3Verify
Connect again; VS Code reinstalls the server and opens the window.
ssh my-host 'ls ~/.vscode-server'Confirmed by code.visualstudio.com
Cause 2 of 5
The login waits for something VS Code cannot show
1Check
Turn on the login terminal so any prompt is visible, and retry.
// settings.json
"remote.SSH.showLoginTerminal": true2Fix
Answer the prompt in that terminal. If it still fails, also turn off the local server option.
"remote.SSH.showLoginTerminal": true,
"remote.SSH.useLocalServer": false3Verify
Reconnect; the window opens without a prompt left unanswered.
ssh my-host echo okConfirmed by code.visualstudio.com
Cause 3 of 5
VS Code uses a different ssh client or config than your terminal
1Check
VS Code needs ssh on the PATH (on Windows it also looks in the default Git for Windows location). See which one the terminal uses.
where ssh # Windows
which ssh # macOS and Linux2Fix
Set the client explicitly in settings.json, and keep the host entry with its IdentityFile in the config file that Remote-SSH: Open Configuration File opens.
"remote.SSH.path": "C:\\Windows\\System32\\OpenSSH\\ssh.exe"3Verify
The Show Log output now starts with the ssh you set.
# Command Palette: Remote-SSH: Show LogConfirmed by code.visualstudio.com
Cause 4 of 5
Key or .ssh permissions are too open
1Check
On macOS and Linux the key, the config and the .ssh folder must not be readable by others.
ls -ld ~/.ssh ~/.ssh/config ~/.ssh/id_ed255192Fix
Apply the modes the VS Code docs list: 700 for the folder, 600 for the config and keys; on the host, 700 for .ssh and 600 for authorized_keys. On Windows make sure only you own and can read the files in .ssh.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/id_ed255193Verify
Connect again; the warning is gone from the log.
ssh -v my-host echo okConfirmed by code.visualstudio.com
Cause 5 of 5
The SSH endpoint is a cluster that rotates nodes
1Check
Ask whether the host name is a load-balanced login node, which gives each connection a different machine.
ssh my-host hostname; ssh my-host hostname2Fix
On macOS or Linux clients, share one SSH session with ControlMaster in your local config, then create the sockets folder.
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 600
mkdir -p ~/.ssh/sockets3Verify
Both commands in the check now print the same host name.
ssh my-host hostname; ssh my-host hostnameConfirmed by code.visualstudio.com
How do I stop it coming back?
- Keep one working Host block with IdentityFile in your ssh config and use it from both the terminal and VS Code.
- After a big VS Code update, run Remote-SSH: Kill VS Code Server on Host once if a connection starts failing.
- Keep key and .ssh modes at the values the docs list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Security
VS Code Remote - SSH fails to set up the dynamic port forward
open failed: administratively prohibited: open failed
The connection is made, but VS Code cannot open its tunnel to the VS Code Server on the host.
4 causes, each with a fixSecurity
VS Code Server will not start on an old Linux host because glibc is too old
Remote - SSH log shows the server download succeeded but the server never starts, on an old distribution
Since VS Code 1.99 the prebuilt server needs a Linux host with glibc 2.28 or newer and libstdc++ 3.4.25 or newer.
3 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fix