反向 SSH 隧道技术实现内网穿透
反向 SSH 隧道技术实现内网穿透
场景描述
有一台内网的 Linux PC 机,想在其他地方(如家中)使用浏览器,在浏览器中能够使用内网 Linux PC 机的命令行。
实现思路
内网 Linux PC 机在内网可以使用 SSH 进行连接,但内网的 Linux PC 机 SSH 是作为服务端使用的,只能被动等待连接。我们需要一种技术,让内网 Linux PC 机以客户端的形式连接到一台公网服务器,其他 SSH 客户端也连接到公网服务器,通过公网服务器能够使用命令控制内网 Linux PC 机。
参考资料
- SSH Tunnel 强大的隧道功能
- 只需 2 条指令,轻松实现 SSH 反向隧道!
- ssh反向隧道实现内网穿透
- 使用SSH反向隧道进行内网穿透
实现步骤
场景模拟
- Ubuntu 电脑:IP 为 172.18.1.168/24,用户名为
systemuser2
。 - Windows 电脑:IP 为 172.16.30.87/24 和 192.168.92.1/24。
- Debian 虚拟机:IP 为 192.168.92.141/24,用户名为
tukuku01
。
根据上述 IP 信息:
- Windows 可以通过内部路由器访问 Ubuntu,Ubuntu 也可以通过内部路由器访问 Windows。
- Windows 可以通过虚拟机网络访问 Debian,Debian 也可以通过虚拟网络访问 Windows。
- Debian 可以通过虚拟机网络访问 Ubuntu,但 Ubuntu 不能访问 Debian 系统。
目标
在 Windows 上通过 Ubuntu 进行反向代理,直接访问 Debian 系统。使用一系列的 SSH 命令,但命令中不可出现 Debian 的 IP 地址。
前提
- Ubuntu 电脑需要有全套的 SSH 服务(
ssh
和sshd
)。 - Debian 也需要有全套的 SSH 服务。
- Windows 中需要有 SSH 客户端。
具体命令
在 Ubuntu 中
-
修改 SSH 配置文件:
sudo nano /etc/ssh/sshd_config
修改或添加以下内容:
GatewayPorts yes
-
重启 SSH 服务:
sudo systemctl restart sshd sudo systemctl restart ssh
-
开放端口 6666 并允许通过防火墙:
如何正确打开端口6666并允许通过防火墙,可自行搜索,本次测试使用:sudo ufw disable # 关闭防火墙 sudo nft add rule inet filter input tcp dport 6666 accept
如果没有
nft
,可以使用以下命令安装:sudo apt install nft
注意:如果重启或注销,6666 端口可能会被关闭。由于验证功能,不需要固化到系统中。
在 Debian 中
-
安装 SSH 服务:
sudo apt update sudo apt install ssh
-
重启 SSH 服务:
sudo systemctl restart sshd sudo systemctl restart ssh
-
建立反向隧道:
ssh -p 22 -qngfNTR 0.0.0.0:6666:0.0.0.0:22 systemuser2@172.18.1.168
提示:
The authenticity of host '172.18.1.168 (172.18.1.168)' can't be established. ED25519 key fingerprint is SHA256:/4KPZ2tdSMIKRIMfWRI7TPzWMli5b1OHtQdh0zRT9b0. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 输入yes systemuser2@172.18.1.168's password: #输入Ubuntu的密码
执行完毕后自动退出,因为有一个参数是后台执行的意思。
在 Windows 中
-
使用 SSH 命令连接到 Ubuntu:
ssh systemuser2@172.18.1.168
输入 Ubuntu 的密码。
-
通过 Ubuntu 的反向隧道连接到 Debian:
ssh -p 6666 tukuku01@127.0.0.1
输入 Debian 的密码。
完整输出:
C:\Users\shaoz>ssh systemuser2@172.18.1.168 systemuser2@172.18.1.168's password: Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.0-131-generic x86_64) ****** Last login: Mon Mar 24 10:18:49 2025 from 172.16.30.87 xhost: unable to open display "" systemuser2@kukutu:~$ ssh -p 6666 tukuku01@127.0.0.1 tukuku01@127.0.0.1's password: Linux tukuku-vm1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Mar 24 10:19:38 2025 from ::1 tukuku01@tukuku-vm1:~$
-
直接从 Windows 连接到 Debian:
ssh -p 6666 tukuku01@172.18.1.168
完整输出:
C:\Users\shaoz>ssh -p 6666 tukuku01@172.18.1.168 The authenticity of host '[172.18.1.168]:6666 ([172.18.1.168]:6666)' can't be established. ED25519 key fingerprint is SHA256:HoC4CmexS/2TWJxn3MZkKgSLsIUmaPQuyezPC5F2EmQ. This host key is known by the following other names/addresses: C:\Users\shaoz/.ssh/known_hosts:15: 192.168.92.141 Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[172.18.1.168]:6666' (ED25519) to the list of known hosts. tukuku01@172.18.1.168's password: Linux tukuku-vm1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Mar 24 14:47:53 2025 from 127.0.0.1 tukuku01@tukuku-vm1:~$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 tukuku01@tukuku-vm1:~$ ll -bash: ll: 未找到命令 tukuku01@tukuku-vm1:~$
待优化
- 命令中未出现 Debian 的地址:虽然实现了目标,但命令中并未直接使用 Debian 的地址,而是通过 Ubuntu 的反向隧道间接访问。
- 直接从 Windows 连接到 Debian:目前需要先连接到 Ubuntu,再通过 Ubuntu 连接到 Debian,这不太合理。可能是因为在 Ubuntu 中未正确监听
0.0.0.0:6666
地址,而是监听了127.0.0.1:6666
。可以通过以下命令检查端口绑定:sudo netstat -tulnap | grep 6666
- 修改配置文件:可能需要修改
/etc/ssh/ssh_config
文件,而不是/etc/ssh/sshd_config
文件。
高级操作
- 使用 WebSSH 在浏览器中访问
- 在服务器上(本场景下使用 Ubuntu),环境和操作同上。
- 参考:GitHub - huashengdun/webssh: Web based ssh client
- 使用命令安装 WebSSH:
更多参数参考官网。pip3 install webssh wssh
- 在 Windows 浏览器中输入 Ubuntu 的环境地址,本次验证为:
http://172.18.1.168:8888/ - 输入 SSH 反向隧道的信息:
- hostname: 172.18.1.168
- port: 6666
- Username: tukuku01
- Password: Debian 的密码
- 进入 Debian 系统中的命令行。
通过上述步骤,你可以在 Windows 上通过浏览器访问内网的 Debian 系统,而无需直接使用 Debian 的 IP 地址。