vscode ssh config

VS Code SSH Config Location:

  • Windows: C:\Users\YourUsername\.ssh\config

  • macOS/Linux: ~/.ssh/config

SSH Config Structure:

复制代码
Host my-server
    HostName 192.168.1.100
    User myusername
    Port 22
    IdentityFile ~/.ssh/id_rsa

SSH Config Options:

复制代码
# Simple connection
Host myserver
    HostName 192.168.1.100
    User admin

# Custom port
Host production
    HostName prod.example.com
    User deploy
    Port 2222

# With identity file
Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_rsa

# Jump host / Proxy
Host internal-server
    HostName 10.0.0.5
    User myuser
    ProxyJump bastion-host

Host bastion-host
    HostName bastion.example.com
    User jumpuser
    IdentityFile ~/.ssh/bastion_key

VS Code Specific Settings:

Add these to your VS Code settings.json:

json

复制代码
{
    "remote.SSH.showLoginTerminal": true,
    "remote.SSH.configFile": "~/.ssh/config",
    "remote.SSH.useLocalServer": false,
    "remote.SSH.allowLocalServerDownload": true,
    "remote.SSH.connectTimeout": 60,
    "remote.SSH.serverInstallTimeout": 120
}
复制代码
Using different keys for different platforms
Host aws-*
    User ubuntu
    IdentityFile ~/.ssh/aws-key.pem
    IdentitiesOnly yes

Host aws-web
    HostName ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com

Setting File Permissions

复制代码
# Linux/macOS
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

VS Code Remote-SSH Commands

  • Connect: Ctrl+Shift+P → "Remote-SSH: Connect to Host..."

  • Disconnect: Ctrl+Shift+P → "Remote-SSH: Close Remote Connection"

  • Reopen: Ctrl+Shift+P → "Remote-SSH: Reopen in Remote Window"

  • Create SSH config:

复制代码
touch ~/.ssh/config
chmod 600 ~/.ssh/config
  • Add your server:

  • ssh-config

    复制代码
    Host dev
        HostName dev.company.com
        User johndoe
        IdentityFile ~/.ssh/id_rsa

Generate SSH Key Pair

复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
复制代码
ssh-keygen -t rsa -b 4096

Verify Generated Keys

复制代码
# Check if keys were created
ls -la ~/.ssh/id_rsa*
  • ~/.ssh/id_rsa - PRIVATE KEY (keep secret!)

  • ~/.ssh/id_rsa.pub - PUBLIC KEY (share with servers)

Set Correct Permissions

复制代码
chmod 600 ~/.ssh/id_rsa          # Private key - read/write for owner only
chmod 644 ~/.ssh/id_rsa.pub      # Public key - readable by everyone
chmod 700 ~/.ssh                 # .ssh directory

Alternative Key Types

复制代码
# ED25519 (faster, more secure, recommended for modern systems)
ssh-keygen -t ed25519 -C "your_email@example.com"

# ECDSA (smaller key size)
ssh-keygen -t ecdsa -b 521 -C "your_email@example.com"

# RSA 2048 (older, still widely supported)
ssh-keygen -t rsa -b 2048 -C "your_email@example.com"

Copy Public Key to Server

Method 1: Using ssh-copy-id (Recommended)

bash

复制代码
ssh-copy-id user@your-server.com

Method 2: Manual copy

bash

复制代码
# View and copy the public key
cat ~/.ssh/id_rsa.pub

# Then on the server, add to authorized_keys
echo "ssh-rsa AAAAB3NzaC1yc2E..." >> ~/.ssh/authorized_keys

Quick One-Liner Setup

复制代码
# Generate key and copy to server in one go
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa -N "" && ssh-copy-id user@server.com

Test Your SSH Connection

复制代码
# Test with verbose output
ssh -v user@your-server.com

# Test with the key explicitly
ssh -i ~/.ssh/id_rsa user@your-server.com

Issue: "Permission denied (publickey)"

复制代码
# Check key permissions
chmod 600 ~/.ssh/id_rsa

# Ensure public key is on server
cat ~/.ssh/id_rsa.pub
# Copy output to server's ~/.ssh/authorized_keys

Issue: "No such file or directory"

复制代码
# Create .ssh directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh
  • Check SSH service is running on server:

复制代码
# On the server
sudo systemctl status sshd
# or
sudo service ssh status
  • Enable password authentication on server:

复制代码
# On the server
sudo nano /etc/ssh/sshd_config
# Make sure these lines are set:
PasswordAuthentication yes
# Restart SSH
sudo systemctl restart sshd
  • Test manually first:

复制代码
# From your terminal
ssh admin@192.168.1.50
# Enter password
复制代码
# Connect with password first
ssh username@192.168.1.100

# Once connected, check SSH config
sudo nano /etc/ssh/sshd_config

# Make sure these lines are set:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes  # Keep this until key works

# Restart SSH
sudo systemctl restart sshd
# or
sudo service ssh restart
复制代码
# 1. View your public key
cat ~/.ssh/id_rsa.pub

# 2. Copy the output (starts with ssh-rsa AAAAB3...)

# 3. Connect to server with password
ssh username@192.168.1.100

# 4. On the server, create/update authorized_keys
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-rsa AAAAB3..." >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
exit

# 5. Test passwordless login
ssh username@192.168.1.100
复制代码
nano ~/.ssh/config
  • Check your IP address:
复制代码
# On Mac
ifconfig | grep inet

# On Windows
ipconfig

# On Linux
ip addr show
复制代码
# Test if port 22 is open
nc -zv 192.168.50.122 22
# or
telnet 192.168.50.122 22
  • On the server, check firewall:

  • bash

    复制代码
    # Linux (UFW)
    sudo ufw status
    sudo ufw allow 22
    
    # Linux (firewalld)
    sudo firewall-cmd --list-all
    sudo firewall-cmd --add-service=ssh
    
    # iptables
    sudo iptables -L -n
复制代码
sudo systemctl status sshd
# or
sudo service ssh status
复制代码
# 1. Ping test
ping -c 4 192.168.50.122

# 2. Traceroute to see where it fails
traceroute 192.168.50.122
# or
tracert 192.168.50.122  # Windows
复制代码
# Check if SSH port is open
nmap -p 22 192.168.50.122
# or
nc -zv 192.168.50.122 22
复制代码
# Show routing table
netstat -rn
# or
route -n
相关推荐
郝学胜-神的一滴1 天前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
云起无垠4 天前
关掉笔记本,让 AI agent 在远程服务器上继续跑完一夜:DevAgent 的 SSH 远程工作区实战
服务器·人工智能·ssh
BD_Marathon5 天前
安装Jupyter notebook
ide·python·jupyter
布莱克6055 天前
Swift 下标(Subscript)详解:从语法到实战
服务器·开发语言·ios·ssh·swift·下标
fengkai45455 天前
Python运维实战
运维·开发语言·python·ssh·sftp·paramiko
讓丄帝愛伱6 天前
Zed Markdown 预览不能铺满全屏的原因及解决
ide
BD_Marathon6 天前
Jupyter魔法命令
ide·python·jupyter
郝学胜-神的一滴7 天前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
aaaameliaaa7 天前
VSCode的安装与配置
ide·笔记·vscode·编辑器