本文详解SSH隧道的三种转发模式,掌握这个技能能解决很多网络访问问题。
前言
SSH不只是远程登录工具,它还是强大的网络隧道工具。
通过SSH端口转发,你可以:
- 访问内网服务
- 加密不安全的连接
- 绕过防火墙限制
- 实现简易的代理
今天来详解SSH的三种端口转发模式。
一、SSH端口转发概述
1.1 三种转发模式
| 模式 | 命令参数 | 方向 | 用途 |
|---|---|---|---|
| 本地转发 | -L |
本地→远程 | 访问远程内网服务 |
| 远程转发 | -R |
远程→本地 | 暴露本地服务到远程 |
| 动态转发 | -D |
SOCKS代理 | 通用代理 |
1.2 准备工作
# 确保SSH服务端允许转发
# /etc/ssh/sshd_config
AllowTcpForwarding yes
GatewayPorts yes # 远程转发需要
二、本地端口转发(-L)
2.1 原理
┌─────────────────────────────────────────────────────────┐
│ 本地端口转发 (-L) │
│ │
│ 本地电脑 SSH服务器 目标服务 │
│ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │:8080│ ──SSH──→ │ │ ────────→ │:3306│ │
│ └─────┘ └─────┘ └─────┘ │
│ │
│ 访问 localhost:8080 = 访问 目标服务:3306 │
└─────────────────────────────────────────────────────────┘
2.2 命令格式
ssh -L [本地地址:]本地端口:目标地址:目标端口 用户@SSH服务器# 简化
ssh -L 本地端口:目标地址:目标端口 用户@SSH服务器
2.3 实战场景
场景1:访问远程MySQL
# 远程服务器有MySQL,但只允许本地访问
# 通过SSH隧道访问ssh -L 3307:localhost:3306 user@remote-server# 本地连接
mysql -h 127.0.0.1 -P 3307 -u root -p
场景2:访问内网Web服务
# 内网有个Web服务 192.168.1.100:8080
# 通过跳板机访问ssh -L 8080:192.168.1.100:8080 user@jump-server# 浏览器访问
http://localhost:8080
场景3:访问Redis
ssh -L 6380:localhost:6379 user@redis-server# 本地连接
redis-cli -h 127.0.0.1 -p 6380
2.4 后台运行
# -f 后台运行
# -N 不执行远程命令
# -T 不分配终端
ssh -fNT -L 3307:localhost:3306 user@remote-server# 查看隧道进程
ps aux | grep ssh# 关闭隧道
kill <pid>
三、远程端口转发(-R)
3.1 原理
┌─────────────────────────────────────────────────────────┐
│ 远程端口转发 (-R) │
│ │
│ 本地服务 SSH服务器 远程访问 │
│ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │:3000│ ←──SSH── │:8080│ ←───────── │用户 │ │
│ └─────┘ └─────┘ └─────┘ │
│ │
│ 访问 SSH服务器:8080 = 访问 本地:3000 │
└─────────────────────────────────────────────────────────┘
3.2 命令格式
ssh -R [远程地址:]远程端口:本地地址:本地端口 用户@SSH服务器
3.3 实战场景
场景1:暴露本地开发环境
# 本地跑了个Web应用在3000端口
# 想让外网能访问(用于演示/测试)ssh -R 8080:localhost:3000 user@public-server# 外网访问
http://public-server:8080
场景2:让外网访问本地服务
# 本地有个API服务
ssh -R 0.0.0.0:9000:localhost:8000 user@vps# 注意:需要服务器配置 GatewayPorts yes
3.4 实现简易内网穿透
# 本地电脑没有公网IP
# 但有一台公网VPS# 本地执行
ssh -R 2222:localhost:22 user@vps# 外网通过VPS访问本地SSH
ssh -p 2222 localuser@vps
四、动态端口转发(-D)
4.1 原理
┌─────────────────────────────────────────────────────────┐
│ 动态端口转发 (-D) = SOCKS5代理 │
│ │
│ 本地应用 SSH服务器 任意目标 │
│ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │SOCKS│ ──SSH──→ │ │ ─────────→ │任意 │ │
│ │:1080│ └─────┘ └─────┘ │
│ └─────┘ │
│ │
│ 配置代理 127.0.0.1:1080 后,所有流量走SSH │
└─────────────────────────────────────────────────────────┘
4.2 命令格式
ssh -D [本地地址:]端口 用户@SSH服务器# 示例
ssh -D 1080 user@remote-server
4.3 使用方法
# 启动SOCKS代理
ssh -D 1080 user@remote-server# 配置浏览器/系统代理
# SOCKS5 代理:127.0.0.1:1080# 或命令行使用
curl --socks5 127.0.0.1:1080 http://example.com# 配合proxychains
proxychains curl http://example.com
五、SSH配置文件
5.1 简化命令
# ~/.ssh/config# 跳板机
Host jumpHostName jump.example.comUser adminIdentityFile ~/.ssh/jump_key# 内网服务器(通过跳板)
Host internalHostName 192.168.1.100User rootProxyJump jump# 带端口转发的连接
Host mysql-tunnelHostName db-server.example.comUser adminLocalForward 3307 localhost:3306Host dev-proxyHostName vps.example.comUser rootDynamicForward 1080
5.2 使用简化后的命令
# 直接连接内网服务器
ssh internal# 建立MySQL隧道
ssh -fNT mysql-tunnel# 启动SOCKS代理
ssh -fNT dev-proxy
六、autossh保持连接
6.1 安装
# Ubuntu/Debian
sudo apt install autossh# macOS
brew install autossh
6.2 使用
# 自动重连的隧道
autossh -M 0 -fNT -L 3307:localhost:3306 user@remote-server# 参数说明
# -M 0:禁用监控端口,使用SSH自身的保活
# -f:后台运行
# -N:不执行远程命令
# -T:不分配终端
6.3 开机自启(systemd)
# /etc/systemd/system/ssh-tunnel.service
[Unit]
Description=SSH Tunnel to MySQL
After=network.target[Service]
User=youruser
ExecStart=/usr/bin/autossh -M 0 -NL 3307:localhost:3306 user@remote-server
Restart=always
RestartSec=10[Install]
WantedBy=multi-user.target
sudo systemctl enable ssh-tunnel
sudo systemctl start ssh-tunnel
七、实际应用案例
7.1 案例:安全访问生产数据库
# 需求:本地IDE连接生产MySQL
# 生产MySQL不对外开放# 方案:通过跳板机建立隧道
ssh -L 3307:prod-mysql.internal:3306 user@jump-server# IDE连接配置
# Host: 127.0.0.1
# Port: 3307
7.2 案例:远程调试内网服务
# 需求:访问内网的测试环境Web服务
# 测试环境:192.168.1.50:8080ssh -L 8080:192.168.1.50:8080 user@office-server# 浏览器访问
http://localhost:8080
7.3 案例:临时暴露本地服务
# 需求:给客户演示本地开发的应用ssh -R 80:localhost:3000 user@demo-server# 客户访问
http://demo-server
八、SSH隧道的局限性
8.1 问题
1. 需要SSH服务器- 你得有一台可SSH的机器2. 单点故障- SSH断了,隧道就断了3. 配置较繁琐- 每个服务都要配置4. 不适合大流量- SSH开销较大
8.2 更优方案
对于长期、多服务的远程访问需求,可以考虑:
- 内网穿透工具:frp、ngrok等
- 组网软件:如星空组网,配置更简单,所有端口直接互通
SSH隧道适合临时、单一服务的场景。
九、安全注意事项
9.1 限制转发
# /etc/ssh/sshd_config# 禁止所有转发
AllowTcpForwarding no# 只允许本地转发
AllowTcpForwarding local# 限制可转发的地址
PermitOpen host1:port1 host2:port2
9.2 使用密钥认证
# 生成密钥
ssh-keygen -t ed25519 -C "tunnel-key"# 复制到服务器
ssh-copy-id -i ~/.ssh/tunnel-key.pub user@server# 使用密钥连接
ssh -i ~/.ssh/tunnel-key user@server
十、总结
| 转发类型 | 参数 | 场景 |
|---|---|---|
| 本地转发 | -L |
访问远程内网服务 |
| 远程转发 | -R |
暴露本地服务到公网 |
| 动态转发 | -D |
SOCKS代理 |
常用命令速查:
# 本地转发
ssh -L 本地端口:目标:端口 user@server# 远程转发
ssh -R 远程端口:localhost:本地端口 user@server# SOCKS代理
ssh -D 1080 user@server# 后台运行
ssh -fNT -L ...# 保持连接
autossh -M 0 -fNT -L ...
参考资料
- SSH手册:https://man.openbsd.org/ssh
- OpenSSH官网:https://www.openssh.com/
💡 SSH隧道是运维必备技能,但对于复杂的远程访问需求,专业的组网工具会更方便。