# Expose local dev server (port 3000) on remote port 8080
Host dev-server
RemoteForward 8080 localhost:3000
# On remote: curl http://localhost:8080 → hits your local :3000
# VS Code Remote: forward local Git credential helper
Host vscode-host
RemoteForward /run/user/1000/gnupg/S.gpg-agent ~/.gnupg/S.gpg-agent
# ~/.ssh/config
Include ~/.ssh/conf.d/work.conf
Include ~/.ssh/conf.d/personal.conf
# ~/.ssh/conf.d/work.conf
Host *.work.example.com
User alice
IdentityFile ~/.ssh/id_ed25519_work
# Apply only when local username is alice
Match User alice
IdentityFile ~/.ssh/id_ed25519_alice
# Apply when on VPN subnet (Exec runs locally)
Match Exec "ip addr show tun0 2>/dev/null | grep -q 10.8.0"
ProxyJump none
ForwardX11ForwardX11 yes|no
将远端服务器的 X11 显示转发到本地 X 服务器。让你在服务器上运行 GUI 应用,窗口出现在本地。需要本地有 X 服务器(macOS 用 XQuartz,Windows 用 Xming)。
# ~/.ssh/config — Host * MUST be last
Host dev
HostName dev.example.com
User alice
Port 2222
Host *.prod
User ubuntu
IdentityFile ~/.ssh/prod_key
# Global fallbacks — last in file
Host *
AddKeysToAgent yes
ServerAliveInterval 60
ServerAliveCountMax 3
ControlMaster auto
ControlPath ~/.ssh/cm/%C
ControlPersist 10m
# ~/.ssh/config
Host bastion-1
HostName bastion1.example.com
User ops
Host bastion-2
HostName bastion2.internal.example.com
User ops
ProxyJump bastion-1
Host deep-internal
HostName 10.10.10.5
User ubuntu
ProxyJump bastion-1,bastion-2
# Or inline at command line:
# ssh -J bastion1,bastion2 10.10.10.5
Pattern: VS Code Remote SSHControlMaster + RemoteForward for VS Code
VS Code Remote SSH 读取 ~/.ssh/config 并应用所有指令。加 ControlMaster/ControlPersist 加快 VS Code 重复连接速度。用 RemoteForward 把本地服务器暴露给远端工作区。用 SetEnv 设置 VS Code 专用环境变量。
Host vscode-devbox
HostName devbox.example.com
User alice
IdentityFile ~/.ssh/id_ed25519
ControlMaster auto
ControlPath ~/.ssh/cm/%C
ControlPersist 1h
# Forward local dev server to remote port 3001
RemoteForward 3001 localhost:3000
# Increase alive timeouts for long VS Code sessions
ServerAliveInterval 120
ServerAliveCountMax 10
Pattern: SOCKS5 office proxyDynamicForward 1080 via SSH server
# Show effective config for 'dev':
ssh -G dev
# Grep for specific directives:
ssh -G dev | grep -E 'hostname|user|identityfile|proxyjump'
# Useful outputs:
# hostname dev.example.com
# user alice
# port 2222
# identityfile ~/.ssh/id_ed25519