Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Terminal ssh works, but VS Code Remote - SSH cannot connect or keeps dropping

ErrorCould 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.

  • Applies to VS Code Remote - SSH, OpenSSH client 9.x, Windows 10 and 11, macOS, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 Log
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The 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. 4WARNING: UNPROTECTED PRIVATE KEY FILE! in the log.Key or .ssh permissions are too openFix
    5. 5Connections 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

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

Did this fix it?

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": true

2Fix

Answer the prompt in that terminal. If it still fails, also turn off the local server option.

"remote.SSH.showLoginTerminal": true,
"remote.SSH.useLocalServer": false

3Verify

Reconnect; the window opens without a prompt left unanswered.

ssh my-host echo ok

Confirmed by code.visualstudio.com

Did this fix it?

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 Linux

2Fix

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 Log

Confirmed by code.visualstudio.com

Did this fix it?

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_ed25519

2Fix

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_ed25519

3Verify

Connect again; the warning is gone from the log.

ssh -v my-host echo ok

Confirmed by code.visualstudio.com

Did this fix it?

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 hostname

2Fix

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/sockets

3Verify

Both commands in the check now print the same host name.

ssh my-host hostname; ssh my-host hostname

Confirmed by code.visualstudio.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.