SSH 反向隧道远程连接云桌面配置指南
背景
公司使用云桌面进行开发,代码只能在云桌面内网访问,手机/外部设备无法通过 VPN 连入内网。解决方案:让云桌面主动出站建立反向 SSH 隧道到公网 VPS,外部设备通过 VPS 跳转访问云桌面。
架构
端口规划:
| 端口 | 用途 | 监听位置 |
|---|---|---|
| 22 | VPS SSH 服务(外部连接 + 云桌面建隧道) | VPS |
| 2222 | 反向隧道映射端口(指向云桌面 22) | VPS,由云桌面主动建立 |
一、VPS 配置
1.1 创建专用用户
sudo adduser jump
sudo usermod -aG sudo jump 1.2 配置 SSH Key 登录
本地生成密钥并上传:
ssh-keygen -t ed25519 -f ~/.ssh/jump_key -C "jump-vps"
ssh-copy-id -i ~/.ssh/jump_key.pub jump@your-vps-ip 1.3 配置 sshd
编辑 /etc/ssh/sshd_config:
Port 22
PermitRootLogin no
PasswordAuthentication no
AllowTcpForwarding yes
GatewayPorts clientspecified
ClientAliveInterval 60
ClientAliveCountMax 3 验证并重启:
sudo sshd -t && sudo systemctl restart sshd
sudo ss -tlnp | grep sshd 1.4 安装 fail2ban
sudo apt update && sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban 如果 fail2ban 误封 IP,解封:
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 你的IP 1.5 防火墙放行
sudo ufw allow 22/tcp
sudo ufw enable 云厂商控制台的安全组也要放行 22 端口。
二、云桌面配置
2.1 安装必要软件
# Node.js 22 LTS
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
# Claude Code
npm install -g @anthropic-ai/claude-code
# autossh(自动重连)
sudo apt install -y autossh
# tmux
sudo apt install -y tmux 2.2 配置 SSH Key(云桌面到 VPS)
# 生成无密码密钥(用于自动化)
ssh-keygen -t ed25519 -f ~/.ssh/reverse_tunnel -N "" -C "cloud-desktop-tunnel"
# 上传公钥到 VPS
ssh-copy-id -i ~/.ssh/reverse_tunnel.pub jump@your-vps-ip
# 修复私钥权限(必须 600,否则 SSH 拒绝使用)
chmod 600 ~/.ssh/reverse_tunnel 踩坑: 私钥权限必须为 600,0664 等更开放的权限会导致 SSH 报 UNPROTECTED PRIVATE KEY FILE 错误并拒绝使用该密钥。
2.3 配置 SSH config(如果云桌面需要代理访问外网)
编辑 ~/.ssh/config:
Host jump-vps
HostName your-vps-ip
User jump
Port 22
IdentityFile ~/.ssh/reverse_tunnel
# 通过 HTTP 代理连接(如果需要)
ProxyCommand /usr/bin/ncat --proxy your-proxy:port --proxy-type http %h %p 不需要代理则去掉 ProxyCommand 那行。
测试连接:
ssh jump-vps echo "连接成功" 2.4 手动建立反向隧道
ssh -R 2222:localhost:22 -N jump-vps 参数说明:
-R 2222:localhost:22:在 VPS 上开 2222 端口,转发到云桌面的 22 端口-N:不执行远程命令,只建隧道- 光标停住无输出 = 成功
踩坑: 反向隧道端口不能和 VPS 已有服务端口冲突,否则报 remote port forwarding failed。
在 VPS 上验证隧道:
ss -tlnp | grep 2222 2.5 配置 systemd 自动保活
创建 /etc/systemd/system/reverse-tunnel.service:
[Unit]
Description=Reverse SSH Tunnel to VPS
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=你的云桌面用户名
ExecStart=/usr/bin/autossh -M 0 -N \
-o "ServerAliveInterval=30" \
-o "ServerAliveCountMax=3" \
-o "ExitOnForwardFailure=yes" \
-o "StrictHostKeyChecking=no" \
-i /home/你的云桌面用户名/.ssh/reverse_tunnel \
-R 2222:localhost:22 \
jump-vps
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target 如果 SSH 需要代理,ExecStart 改为使用 -F 指定 config 文件:
ExecStart=/usr/bin/autossh -M 0 -N \
-o "ServerAliveInterval=30" \
-o "ServerAliveCountMax=3" \
-o "ExitOnForwardFailure=yes" \
-o "StrictHostKeyChecking=no" \
-F /home/你的云桌面用户名/.ssh/config \
-R 2222:localhost:22 \
jump-vps 启动服务:
sudo systemctl daemon-reload
sudo systemctl enable --now reverse-tunnel
sudo systemctl status reverse-tunnel 踩坑: User= 必须是实际存在的用户名,不能留占位符,否则 systemd 报 exit code 217。
2.6 配置 Claude Code
# API Key 认证
echo 'export ANTHROPIC_API_KEY="sk-ant-xxxxx"' >> ~/.bashrc
# 如果需要代理访问 Anthropic API
echo 'export HTTPS_PROXY="http://your-proxy:port"' >> ~/.bashrc
source ~/.bashrc 2.7 创建 tmux 工作会话
tmux new -s claude
cd ~/your-project
claude
# 退出 tmux:Ctrl+B 然后按 D
# 重新进入:tmux attach -t claude tmux 鼠标模式(写入 ~/.tmux.conf 永久生效):
set -g mouse on 三、手机端配置(Termius)
3.1 导入 SSH Key
Keychain → + → Import Key → 导入 jump_key 私钥文件
3.2 配置 VPS 连接
| 字段 | 值 |
|---|---|
| Hostname | VPS IP |
| Port | 22 |
| Username | jump |
| Authentication | 选择导入的 Key |
3.3 配置云桌面直达(ProxyJump)
新建 Host:
| 字段 | 值 |
|---|---|
| Hostname | localhost |
| Port | 2222 |
| Username | 云桌面用户名 |
| Proxy Jump | 选择上面配好的 VPS 连接 |
需要先把云桌面的 SSH 公钥添加到 VPS 的跳板配置:
# 在 VPS 上执行
ssh-keygen -t ed25519 -f ~/.ssh/cloud-desktop-key -N "" -C "jump-to-desktop"
ssh-copy-id -i ~/.ssh/cloud-desktop-key.pub -p 2222 云桌面用户名@localhost 然后把 cloud-desktop-key 私钥也导入 Termius,云桌面 Host 的 Authentication 选这个 Key。
四、日常使用流程
1. 手机打开 Termius
2. 点击云桌面 Host(ProxyJump 自动跳转)
3. tmux attach -t claude
4. 开始 vibe coding
5. 退出 tmux:Ctrl+B 然后 D
6. Claude Code 在云桌面持续运行,随时回来继续 五、安全加固
5.1 反向隧道端口仅限本机访问
修改 systemd 服务的隧道参数,绑定到 127.0.0.1:
-R 127.0.0.1:2222:localhost:22 这样只有先 SSH 到 VPS 的人才能跳转,外部无法直连 2222。
5.2 云桌面 SSH 加固
编辑 /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
AllowUsers your-username 5.3 定期检查
# VPS 上查看隧道连接
ss -tlnp | grep 2222
# 查看登录记录
sudo last
sudo journalctl -u ssh --since "1 hour ago" 踩坑汇总
| 问题 | 原因 | 解决 |
|---|---|---|
| fail2ban 安装后 SSH 连接被拒 | IP 被误封 | fail2ban-client set sshd unbanip IP,或先停 fail2ban 排查 |
| 私钥报 UNPROTECTED PRIVATE KEY FILE | 私钥文件权限太开放 | chmod 600 ~/.ssh/reverse_tunnel |
| 反向隧道报 remote port forwarding failed | 端口被 VPS 已有服务占用 | 换一个未占用的端口 |
| systemd 报 exit code 217 | User 字段写的是占位符 | 替换为实际用户名 |
| tmux 滚轮变成方向键 | 鼠标模式未开启 | set -g mouse on 写入 ~/.tmux.conf |