内网穿透

前提

基于前面wsl转发到了2222端口的基础

0. WSL:确保 sshd 在 2222 正常

# 在 WSL
sudo sed -i 's/^#\?Port .*/Port 2222/' /etc/ssh/sshd_config
sudo service ssh restart
ss -lntp | grep :2222
# 期望看到:LISTEN ... 127.0.0.1:2222 ... sshd
# 自检:
ssh -p 2222 shuo@127.0.0.1   # 这里必须能登录成功

1. VPS:确认允许对外监听与转发

# 在 VPS
sed -i 's/^#\?GatewayPorts.*/GatewayPorts clientspecified/' /etc/ssh/sshd_config
grep -q '^AllowTcpForwarding' /etc/ssh/sshd_config   && sed -i 's/^AllowTcpForwarding.*/AllowTcpForwarding yes/' /etc/ssh/sshd_config   || echo 'AllowTcpForwarding yes' >> /etc/ssh/sshd_config
systemctl reload ssh || systemctl reload sshd || service ssh reload
sshd -T | egrep 'gatewayports|allowtcpforwarding'
# 期望输出:
# gatewayports clientspecified
# allowtcpforwarding yes

放行端口(如已放行可跳过):

ufw allow 2333/tcp 2>/dev/null || true
firewall-cmd --add-port=2333/tcp --permanent && firewall-cmd --reload 2>/dev/null || true

2. VPS 本机自测(打通全链路)

开一个新的 VPS 终端:

# 在 VPS
ss -lntp | grep :2333
# 期望:LISTEN ... 0.0.0.0:2333 ... sshd

ssh -v -p 2333 shuo@127.0.0.1
# 期望:可以登入到 WSL(看到 WSL 的欢迎信息)
  • 若这里能登录,而你在外网仍失败 ⇒ 是安全组/防火墙/ISP阻断,继续检查云防火墙和本机出网。
  • 若这里也失败,继续第 4 步看日志。

3. 看两端日志精准定位

VPS:

# 尝试一次 ssh -p 2333 ... 之后立即看
journalctl -u ssh -n 50 --no-pager | tail -n 50
# 常见报错:
# "connect_to 127.0.0.1 port 2222: failed: Connection refused"
# => 说明 WSL 的 2222 没开好(回到步骤0)

WSL:

# 连接一次后查看
sudo journalctl -u ssh -n 50 --no-pager | tail -n 50
# 若无任何记录,说明请求没到 WSL(VPS 侧转发失败/未监听 0.0.0.0)

4. 外网再次测试

# 你的 Mac/其它外网机器
ssh -p 2333 shuo@107.174.102.179

5. 稳定化(通过后再做)

# 在 WSL
sudo apt update && sudo apt install -y autossh
autossh -M 20000 -N -f -R 0.0.0.0:2333:127.0.0.1:2222 root@107.174.102.179

持久化操作

1:WSL 开启 systemd

# /etc/wsl.conf

[boot]

systemd=true

在 Windows 里执行:

wsl --shutdown

2:安装 autossh

sudo apt update && sudo apt install -y autossh

3:建立免密(如已配置可跳过)

ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@107.174.102.179

4:创建用户级 systemd 服务

# ~/.config/systemd/user/wsl-revtunnel.service
[Unit]
Description=Reverse SSH tunnel to VPS
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/autossh -M 20000 -N   -o "ServerAliveInterval=60" -o "ServerAliveCountMax=3"   -o "ExitOnForwardFailure=yes" -o "StrictHostKeyChecking=accept-new"   -R 0.0.0.0:2333:127.0.0.1:2222 root@107.174.102.179
Restart=always
RestartSec=5

[Install]
WantedBy=default.target

5:允许用户服务在无人登录时运行(一次)

sudo loginctl enable-linger shuo

6:启用并立即启动

systemctl --user daemon-reload
systemctl --user enable --now wsl-revtunnel.service
systemctl --user status wsl-revtunnel.service

之后的行为

  • WSL 重启后:systemd 会自动拉起该服务,隧道自动恢复;你可直接从外网连:
  ssh -p 2333 shuo@107.174.102.179
  • Windows 重启后:WSL 默认不自启。若想开机即生效,在 Windows 任务计划里加一条“开机运行”任务唤醒 WSL,例如(以发行版名 Ubuntu-22.04 为例):
  schtasks /Create /TN "WSL-Autostart-Ubuntu-22.04" /SC ONSTART /RU SYSTEM /RL HIGHEST /TR "wsl -d ""Ubuntu-22.04"" -- systemctl --user start wsl-revtunnel.service" /F

快速自检

# VPS:应为 0.0.0.0 监听
ss -lntp | grep :2333

# WSL:服务应为 active (running)
systemctl --user status wsl-revtunnel.service
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇