1. 首页 > Linux教程 > 正文

Linux教程FG212-SSH端口转发与隧道

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

风哥提示:

本文档详细介绍SSH端口转发和隧道技术,包括本地转发、远程转发、动态转发等内容。

Part01-SSH端口转发概述

1.1 端口转发类型

# SSH端口转发类型
# 1. 本地端口转发(Local Port Forwarding)
# – 将本地端口转发到远程服务器
# – 使用-L参数
# – 语法:ssh -L 本地端口:目标主机:目标端口 SSH服务器

# 2. 远程端口转发(Remote Port Forwarding)
# – 将远程端口转发到本地
# – 使用-R参数
# – 语法:ssh -R 远程端口:目标主机:目标端口 SSH服务器

# 3. 动态端口转发(Dynamic Port Forwarding)
# – 创建SOCKS代理
# – 使用-D参数
# – 语法:ssh -D 本地端口 SSH服务器

# 4.更多学习教程公众号风哥教程itpux_com X11转发(X11 Forwarding)
# – 转发图形界面
# – 使用-X或-Y参数
# – 语法:ssh -X SSH服务器

Part02-本地端口转发

2.1 本地端口转发配置

# 场景:访问远程服务器的Web服务
# 本地主机 -> SSH服务器 -> Web服务器(192.168.2.100:80)

# 创建本地端口转发
$ ssh -L 8080:192.168.2.100:80 user@sshserver.fgedu.net.cn
user@sshserver.fgedu.net.cn’s password:
Last login: Thu Apr 3 20:20:00 2026 from 192.168.1.10

# 在另一个终端访问
$ curl http://localhost:8080

Welcome to Web Server

# 后台运行
$ ssh -fNL 8080:192.168.2.100:80 user@sshserver.fgedu.net.cn

# 验证端口监听
$ ss -tlnp | grep 8080
LISTEN 0 128 127.0.0.1:8080 0.0.0.0:* users:((“ssh”,pid=12350,fd=4))

# 允许外部访问
$ ssh -L 0.0.0.0:8080:192.168.2.100:80 user@sshserver.fgedu.net.cn

# 验证端口监听
$ ss -tlnp | grep 8080
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* users:((“ssh”,pid=12351,fd=4))

# 访问远程数据库
$ ssh -L 3306:192.168.2.200:3306 user@sshserver.fgedu.net.cn

# 使用MySQL客户端连接
$ mysql -h 127.0.0.1 -P 3306 -u dbuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123
Server version: 8.0.32 MySQL Community Server

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
+——————–+
4 rows in set (0.00 sec)

Part03-远程端口转发

3.1 远程端口转发配置

# 场景:让远程服务器访问本地服务
# 远程服务器 -> SSH服务器 -> 本地服务(localhost:8080)

# 创建远程端口转发
$ ssh -R 8080:localhost:80 user@sshserver.fgedu.net.cn
user@sshserver.fgedu.net.cn’s password:
Last login: Thu Apr 3 20:25:00 2026 from 192.168.1.10

# 在SSH服务器上验证
$ ss -tlnp | grep 8080
LISTEN 0 128 127.0.0.1:8080 0.学习交流加群风哥微信: itpux-com0.0.0:* users:((“sshd”,pid=12352,fd=8))

# 允许外部访问远程端口
# 需要在SSH服务器配置GatewayPorts yes
$ sudo grep GatewayPorts /etc/ssh/sshd_config
GatewayPorts yes

$ sudo systemctl restart sshd

# 重新创建远程转发
$ ssh -R 0.0.0.0:8080:localhost:80 user@sshserver.fgedu.net.cn

# 内网穿透示例
# 本地运行Web服务,通过远程服务器暴露给外网
$ ssh -R 80:localhost:8080 user@public-server.fgedu.net.cn

# 反向代理示例
$ ssh -R 2222:localhost:22 user@jump-server.fgedu.net.cn
# 然后可以通过jump-server连接到本地
$ ssh -p 2222 user@jump-server.fgedu.net.cn

Part04-动态端口转发

4.1 创建SOCKS代理

# 创建SOCKS代理
$ ssh -D 1080 user@sshserver.fgedu.net.cn
user@sshserver.fgedu.net.cn’s password:
Last login: Thu Apr 3 20:30:00 2026 from 192.168.1.10

# 验证SOCKS代理
$ ss -tlnp | grep 1080
LISTEN 0 128 127.0.0.1:1080 0.0.0.0:* users:((“ssh”,pid=12353,fd=4))

# 使用curl通过SOCKS代理访问
$ curl –socks5 localhost:1080 http://www.google.com

Google

# 配置浏览器使用SOCKS代理
# Firefox: 设置 -> 网络设置 -> 手动代理配置 -> SOCKS主机: localhost, 端口: 1080

# 后台运行SOCKS代理
$ ssh -fND 1080 user@sshserver.fgedu.net.cn

# 允许外部访问
$ ssh -D 0.0.0.0:1080 user@sshserver.fgedu.net.更多视频教程www.fgedu.net.cncn

# 使用ProxyCommand
$ ssh -o ProxyCommand=”nc -X 5 -x localhost:1080 %h %p” user@internal-server.fgedu.net.cn

Part05-SSH隧道高级应用

5.1 多跳SSH隧道

# 场景:通过跳板机访问内网服务器
# 本地 -> 跳板机 -> 内网服务器

# 方法1:嵌套SSH隧道
$ ssh -L 2222:internal-server:22 user@jump-server
$ ssh -p 2222 user@localhost

# 方法2:使用ProxyJump
$ ssh -J user@jump-server user@internal-server
user@jump-server’s password:
user@internal-server’s password:
Last login: Thu Apr 3 20:35:00 2026 from jump-server

# 方法3:配置SSH客户端
$ tee ~/.ssh/config << EOF Host jump-server HostName jump-server.fgedu.net.cn User jumpuser Host internal-server HostName internal-server.fgedu.net.cn User internaluser ProxyJump jump-server EOF $ ssh internal-server Last login: Thu Apr 3 20:35:30 2026 from jump-server # 创建持久化隧道 $ sudo tee /etc/systemd/system/ssh-tunnel.service << EOF [Unit] Description=SSH Tunnel Service After=network.target [Service] Type=simple User=user学习交流加群风哥QQ113257174 ExecStart=/usr/bin/ssh -N -L 8080:192.168.2.100:80 user@sshserver.fgedu.net.cn Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target EOF $ sudo systemctl daemon-reload $ sudo systemctl enable --now ssh-tunnel # 查看隧道状态 $ sudo systemctl status ssh-tunnel ● ssh-tunnel.service - SSH Tunnel Service Loaded: loaded (/etc/systemd/system/ssh-tunnel.service; enabled; preset: disabled) Active: active (running) since Thu 2026-04-03 20:36:00 CST; 10s ago Main PID: 12354 (ssh) Tasks: 1 (limit: 49152) Memory: 1.2M CPU: 15ms CGroup: /system.slice/ssh-tunnel.service └─12354 /usr/bin/ssh -N -L 8080:192.168.2.100:80 user@sshserver.fgedu.net.cn Apr 03 20:36:00 rhel10 systemd[1]: Started SSH Tunnel Service.

风哥针对使用建议:
1. 使用SSH隧道加密不安全的协议
2. 配置持久化隧道服务
3. 使用ProxyJump简化多跳连接
4. 注意SSH服务器配置GatewayPorts
5. 监控隧道连接状态

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息