To connect VS Code to a remote server via SSH, you need an SSH key pair (private and public) configured on your local machine and the public key added to the server’s authorized_keys file. This article explains how to generate, configure, and troubleshoot SSH keys for VS Code’s Remote – SSH extension, covering common issues and best practices.
Why VS Code Uses SSH Keys
SSH keys provide a secure, passwordless way to authenticate to remote servers. Unlike passwords, which can be guessed or intercepted, SSH keys use a pair of cryptographic keys: a private key that stays on your local machine and a public key that you place on the server. When you connect, the server verifies that you possess the matching private key. VS Code’s Remote – SSH extension leverages this standard SSH authentication to let you edit files and run commands on a remote machine as if they were local.
Generating an SSH Key Pair
Before connecting, you need an SSH key pair. If you don’t have one, generate it using the ssh-keygen command. Open a terminal (PowerShell, CMD, or bash) and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
This creates a new Ed25519 key (recommended for its security and speed). You’ll be prompted for a file location (default is ~/.ssh/id_ed25519) and an optional passphrase. A passphrase adds an extra layer of security—if someone steals your private key, they still need the passphrase to use it. If you prefer a traditional RSA key (e.g., for older servers), use ssh-keygen -t rsa -b 4096.
Copying the Public Key to the Server
Once the key pair is generated, you need to place the public key on the server. The easiest way is using ssh-copy-id (Linux/macOS) or manually appending the key. For Windows, you can use the following command in PowerShell:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Replace user@hostname with your actual server credentials. This command creates the .ssh directory if it doesn’t exist and appends your public key to authorized_keys. Ensure the server has proper permissions: chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
Connecting VS Code to the Remote Server
With the key in place, open VS Code and install the Remote – SSH extension (by Microsoft). Then press F1 to open the Command Palette, type “Remote-SSH: Connect to Host…”, and enter your server address in the form user@hostname. VS Code will use your default SSH key (usually ~/.ssh/id_ed25519 or id_rsa). If your key is in a non-default location or has a custom name, you must configure it in your SSH config file.
Configuring SSH Keys in VS Code
VS Code reads your SSH configuration from ~/.ssh/config (on Windows: C:\Users\YourName\.ssh\config). You can specify which key to use for a particular host:
Host my-server
HostName 192.168.1.100
User ubuntu
IdentityFile ~/.ssh/my_custom_key
This is especially useful if you have multiple keys for different servers. After editing the config, reload the VS Code window (Command Palette → “Reload Window”) and reconnect.
Common SSH Key Issues and Fixes
Even with correct setup, you may encounter errors. Here are frequent problems and their solutions:
- Permission denied (publickey): The server rejected your key. Check that the public key is correctly appended to
authorized_keysand that file permissions are strict (600 on the file, 700 on the.sshdirectory). Also verify you are using the correct username and host. - Key is not recognized: If you’re using a custom key path, ensure the
IdentityFilein your SSH config points to the correct file. Also, the private key must have permissions 600 (on Linux/macOS) or be properly secured on Windows. - Passphrase prompt every time: If you set a passphrase, VS Code will ask for it each connection. To avoid this, you can add the key to your SSH agent (
ssh-add ~/.ssh/id_ed25519) or use a key without a passphrase (less secure). - VS Code can’t find the SSH executable: On Windows, ensure OpenSSH client is installed (Settings → Apps → Optional Features). In VS Code, you can set the
remote.SSH.pathsetting to the full path ofssh.exe. - Connection timed out: Check network connectivity, firewall rules, and that the server is listening on port 22 (or the custom port you specified).
VS Code SSH Key vs. Password Authentication
SSH keys are generally more secure than passwords because they are not transmitted over the network and are resistant to brute-force attacks. Passwords can be phished or guessed, especially if weak. Keys also enable automation and scripting without manual password entry. However, keys require initial setup and must be protected (never share your private key). For most users, using SSH keys with VS Code is the recommended approach.
Security Best Practices for SSH Keys
To keep your SSH keys safe:
- Use a strong passphrase for your private key and store it in a password manager.
- Never share your private key. Only the public key should be placed on servers.
- Use ed25519 keys if possible; they are faster and more secure than RSA for new setups.
- Regularly rotate keys if you suspect compromise.
- Consider using an SSH agent with a hardware token (like YubiKey) for extra security.
Alternatives to SSH Keys in VS Code
While SSH keys are the standard, there are alternatives:
- Password authentication: Simpler but less secure; not recommended for production.
- VS Code Remote Tunnels: Microsoft’s newer feature that uses a tunnel service, useful when you can’t open inbound ports.
- Third-party extensions: Some extensions support other authentication methods, but they may not integrate as seamlessly.
For most users, SSH keys remain the best balance of security and convenience.
Frequently Asked Questions
How do I generate an SSH key on Windows for VS Code?
Open PowerShell and run ssh-keygen -t ed25519. Follow the prompts. The key will be saved to C:\Users\YourName\.ssh\. Then copy the public key to your server using the command shown earlier.
Why does VS Code ask for my password even after setting up SSH keys?
This usually means the public key isn’t in the server’s authorized_keys file, or the private key isn’t being used. Check your SSH config for the correct IdentityFile. Also, ensure the server’s SSH daemon allows public key authentication (check /etc/ssh/sshd_config for PubkeyAuthentication yes).
Can I use the same SSH key for multiple servers?
Yes, you can use the same public key on multiple servers. Just append it to each server’s authorized_keys. However, if one server is compromised, the key is exposed, so consider using separate keys for different trust levels.
What is the difference between id_rsa and id_ed25519?
Both are private key files. id_rsa is for RSA keys (commonly 2048 or 4096 bits), while id_ed25519 is for Ed25519 keys, which are newer, faster, and offer better security with shorter key lengths. Most modern servers support Ed25519.
How do I change the SSH key VS Code uses?
Edit your ~/.ssh/config file and add an IdentityFile line under the host entry. Alternatively, you can set the remote.SSH.configFile setting in VS Code to point to a different config file.
Does VS Code support SSH keys with passphrases?
Yes, but you’ll be prompted for the passphrase each time you connect unless you add the key to your SSH agent. On Windows, you can enable the OpenSSH Authentication Agent service and use ssh-add to cache your passphrase.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Rebrand a Tech Company Without Confusing Customers
- Starbound Spawn Creature Command Not Working? Fixes
- How to Use AI Coding Tools Without Losing Skills
- Evolve Wraith Guide: How the Stealth Assassin Works
- How Foldable Phone Displays and Hinges Work
- AI Workflow Completion Criteria: How to Check Results