When you try to connect to a remote server via SSH in VS Code and see errors like "Could not establish connection to 'hostname'", "Permission denied (publickey,password)", or "Host key verification failed", the issue could be on the client side, the server side, or in the connection itself. Let's systematically diagnose and fix these problems.

Prerequisites Check

Before diving into solutions, verify the basics:

  1. 1.The Remote - SSH extension is installed in VS Code
  2. 2.You can SSH to the server from a regular terminal: ssh user@hostname
  3. 3.The server is reachable (try ping hostname)

If regular SSH works but VS Code's SSH doesn't, the issue is VS Code-specific. If regular SSH also fails, the issue is with your SSH configuration or the server.

Solution 1: Fix Host Key Verification Issues

Error message: "Host key verification failed" or "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!"

Cause: The server's host key doesn't match what's stored in your known_hosts file. This happens after server reinstallation, IP address change, or man-in-the-middle attacks.

Step 1: Open a terminal and try connecting manually:

bash
ssh user@hostname

You'll see the specific error message.

Step 2: If the server was legitimately reinstalled or changed, remove the old host key:

bash
ssh-keygen -R hostname
# or for IP-based connection:
ssh-keygen -R 192.168.1.100

Step 3: Reconnect to accept the new host key:

bash
ssh user@hostname

Type "yes" to accept the new fingerprint.

Step 4: Now try connecting again from VS Code.

Solution 2: Configure SSH Config File Properly

VS Code uses your SSH config file. An incorrect or missing configuration causes many connection issues.

Step 1: Locate your SSH config file: - Windows: C:\Users\YourUsername\.ssh\config - macOS/Linux: ~/.ssh/config

Step 2: If it doesn't exist, create it:

bash
mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

Step 3: Add a host configuration:

bash
Host myserver
    HostName 192.168.1.100
    User myusername
    Port 22
    IdentityFile ~/.ssh/id_rsa

Step 4: Now connect in VS Code using the Host alias ("myserver" in this example) rather than the raw IP or hostname.

Solution 3: Fix SSH Key Authentication

Error message: "Permission denied (publickey,password)" or "Authentication failed"

Step 1: Verify your SSH key exists:

bash
ls -la ~/.ssh

You should see id_rsa and id_rsa.pub (or id_ed25519/id_ed25519.pub for Ed25519 keys).

Step 2: If no keys exist, generate one:

bash
ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept default location, then set a passphrase (or leave empty for no passphrase).

Step 3: Copy the public key to the server:

bash
ssh-copy-id user@hostname

If ssh-copy-id isn't available (Windows), manually copy:

```bash # Windows PowerShell type $env:USERPROFILE\.ssh\id_rsa.pub | ssh user@hostname "cat >> .ssh/authorized_keys"

# macOS/Linux cat ~/.ssh/id_rsa.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" ```

Step 4: Verify key authentication works:

bash
ssh user@hostname

If this works, VS Code should also connect.

Step 5: If using a non-default key path, specify it in your config:

bash
Host myserver
    HostName hostname.com
    User myuser
    IdentityFile ~/.ssh/my_custom_key
    IdentitiesOnly yes

The IdentitiesOnly yes line prevents SSH from trying other keys.

Solution 4: Handle SSH Agent Issues

VS Code sometimes can't access your SSH key if it has a passphrase and the agent isn't running.

Step 1: Start the SSH agent:

Windows (PowerShell as Administrator): ``powershell # Enable SSH agent service Set-Service ssh-agent -StartupType Automatic Start-Service ssh-agent

macOS: ``bash # SSH agent starts automatically, but verify: eval "$(ssh-agent -s)"

Linux: ``bash eval "$(ssh-agent -s)"

Step 2: Add your key to the agent:

bash
ssh-add ~/.ssh/id_rsa

Step 3: In VS Code settings, enable the agent:

json
"remote.SSH.enableAgentForwarding": true

Solution 5: Fix Server-Side VS Code Server Issues

Error message: "The VS Code Server failed to start" or "Could not fetch remote environment"

Cause: VS Code installs a server component on the remote machine. If this fails, the connection won't work.

Step 1: Check if you have write permissions on the remote server:

bash
ssh user@hostname "mkdir -p ~/.vscode-server && touch ~/.vscode-server/test && rm ~/.vscode-server/test && echo 'Permissions OK'"

Step 2: Check disk space on the remote:

bash
ssh user@hostname "df -h ~"

If the disk is full, clean up space.

Step 3: Check if the architecture is supported. VS Code Server runs on x86_64 and ARM64. Check the remote:

bash
ssh user@hostname "uname -m"

Step 4: Clear the VS Code server installation if it's corrupted:

bash
ssh user@hostname "rm -rf ~/.vscode-server"

Then reconnect from VS Code—it will reinstall the server.

Solution 6: Handle Firewall and Network Issues

Error message: "Connection timed out" or "Network is unreachable"

Step 1: Verify the SSH port is open:

bash
# Test port connectivity
nc -zv hostname 22
# or
telnet hostname 22

Step 2: If the SSH server uses a non-standard port, specify it in your config:

bash
Host myserver
    HostName hostname.com
    User myuser
    Port 2222

Step 3: For corporate networks with SSH blocked, you may need to use a VPN or contact your network administrator.

Step 4: Check if the remote SSH extension needs proxy settings. In VS Code settings:

json
"remote.SSH.remoteServerListenOnSocket": true,
"remote.SSH.useLocalServer": true

Solution 7: Fix Windows-Specific Issues

Issue: SSH connection fails on Windows but works in WSL.

Step 1: Ensure you're using the correct SSH client. VS Code can use the Windows OpenSSH or Git's SSH:

json
// In settings.json
"remote.SSH.path": "C:\\Windows\\System32\\OpenSSH\\ssh.exe"
// Or for Git's SSH:
"remote.SSH.path": "C:\\Program Files\\Git\\usr\\bin\\ssh.exe"

Step 2: If using Git Bash SSH, ensure your home directory is correct:

json
"remote.SSH.configFile": "C:\\Users\\YourUsername\\.ssh\\config"

Step 3: For key permissions issues on Windows, fix the key file permissions:

powershell
icacls "$env:USERPROFILE\.ssh\id_rsa" /inheritance:r
icacls "$env:USERPROFILE\.ssh\id_rsa" /grant:r "$env:USERNAME:F"

Solution 8: Enable Detailed Logging

If none of the above fixes work, enable detailed logging to diagnose the issue.

Step 1: In VS Code settings, enable SSH logging:

json
"remote.SSH.enableDynamicForwarding": true,
"remote.SSH.showLoginTerminal": true,
"remote.SSH.debug": true

Step 2: Check the Output panel: - View > Output - Select "Remote - SSH" from the dropdown

Step 3: Look for specific error messages in the log and search for those specific errors.

After applying these solutions, your VS Code Remote SSH connection should work. If problems persist, check the VS Code issue tracker for known bugs with the Remote - SSH extension version you're using.