外网ssh连接wsl

重装 OpenSSH

sudo apt remove openssh-server
sudo apt install openssh-server
sudo vim /etc/ssh/sshd_config

sshd_config 关键配置示例

# ssh服务监听端口
Port 2222
# ssh服务监听地址
ListenAddress 0.0.0.0

# ssh服务允许远程root用户登入
PermitRootLogin yes

# ssh服务允许使用用户名密码方式登入
PasswordAuthentication yes

UsePAM yes
X11Forwarding yes
PrintMotd no

Subsystem sftp /usr/lib/openssh/sftp-server
sudo service ssh restart

获取 WSL2 IP

ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1

Windows 端口转发设置(PowerShell)

  1. 添加端口转发规则:
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=2222 `
connectaddress=172.17.45.15 connectport=2222
  1. 删除错误的端口转发规则:
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=2222
  1. 用正确的 WSL IP 重建转发:
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=2222 `
connectaddress=172.17.45.15 connectport=2222
  1. 验证端口转发配置:
netsh interface portproxy show v4tov4

你应该看到类似:

0.0.0.0  2222   172.17.45.15  2222

Windows 防火墙放行端口 2222

查看规则:

netsh advfirewall firewall show rule name=WSL2

如果没看到或不正确,再添加规则:

netsh advfirewall firewall add rule `
name="WSL2 SSH" dir=in action=allow protocol=TCP localport=2222

测试连接

  1. 本机(Windows)上测试:
ssh root@127.0.0.1 -p 2222
  1. 局域网内其他机器(用 Windows 局域网 IP):
ssh root@192.168.x.x -p 2222

WSL IP 不固定,自动化更新端口转发的 PowerShell 脚本

# --------------------------------------------
# C:\Scripts\Update-WSL2-SSHPortProxy.ps1
# 自动获取 WSL2 IPv4 并更新 0.0.0.0:2222 转发
# --------------------------------------------

# 1. 调用 hostname -I 拿原始输出(含任何警告)
$raw = wsl.exe hostname -I 2>&1

if (-not $raw) {
Write-Error "Failed to invoke 'wsl hostname -I'."
exit 1
}

# 2. 从所有词中挑出第一个合法 IPv4
$wslIp = ($raw -split '\s+') `
| Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' } `
| Select-Object -First 1

if (-not $wslIp) {
Write-Error "Failed to parse a valid IPv4 address. Raw output:`n$raw"
exit 1
}

# **一定会打印出来**
Write-Host "WSL2 IP detected: $wslIp"

# 3. 删除旧的 portproxy(忽略错误)
netsh interface portproxy delete v4tov4 `
listenaddress=0.0.0.0 listenport=2222 2>$null

# 4. 添加新的映射
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=2222 `
connectaddress=$wslIp connectport=2222

Write-Host "Updated portproxy: 0.0.0.0:2222 -> $wslIp:2222"
# --------------------------------------------

运行脚本命令

powershell.exe -NoProfile -ExecutionPolicy Bypass `
-File "C:\Scripts\Update-WSL2-SSHPortProxy.ps1"

外网连接

  1. 访问 https://ipinfo.io/ 获取公网 IP(请关闭梯子)
  2. 路由器上设置端口转发:
  • 外部端口(WAN端口):2222
  • 内部 IP:你 Windows 主机的局域网 IP(例如 192.168.123.114)
  • 内部端口:2222
  • 协议:TCP
  1. 保存并重启路由器端口转发服务。
  2. 通过公网 IP 和端口连接:
ssh root@公网IP -p 2222

可选:Windows 开机自动执行脚本

  • 打开任务计划程序,创建新任务
  • 触发器:系统启动时
  • 操作:启动程序
  • 程序或脚本:powershell.exe
  • 添加参数:-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Update-WSL2-SSHPortProxy.ps1"
  • 保存并启用任务

评论

  1. 匿名
    10 月前
    2025-6-11 17:14:54

    很好的文章^_^

    • 博主
      匿名
      10 月前
      2025-6-11 17:19:50

      为什么匿名 耗子哥

  2. 8 月前
    2025-8-20 3:33:47

    Baklava tasting tour Tour guides are passionate about their city. https://kulturzentrum-hermannstadt.ro/?p=6248

  3. 4 月前
    2025-12-10 7:20:51

    Interesting read! Regulatory compliance in online gaming is HUGE – seeing Nustar Game Login prioritize that (KYC, AML) is reassuring. Check out their details: nustar game login link. Solid platform access procedures too!

  4. 4 月前
    2025-12-10 10:36:58

    Straight to the point, just login and play. Spintime777login is pretty straightforward. Simple and easy. Check it out if you want something quick at spintime777login.

  5. 4 月前
    2025-12-19 0:56:24

    Yo, what’s up? I stumbled upon 99okclub. Seems alright. Games are pretty standard, but the interface is clean. Might be your thing! Have a look-see: 99okclub

  6. 4 月前
    2025-12-27 21:41:12

    Yo, ACB88bet! Gave it a shot last night. Pretty solid experience, gotta say. The slots were hitting, and I liked the variety of games. Definitely worth checking out if you’re looking for something new. Check them out: acb88bet!

  7. 4 月前
    2025-12-30 16:14:03

    That’s a great point about balancing risk & reward! Seeing platforms like BossJL prioritize easy access – like the bossjl app casino</a> – really opens things up for players. Fun to explore those slot games too! 👍

  8. 3 月前
    2026-1-11 2:55:28

    Bong88comlogin… Straight to the point. Easier login, supposedly. Always a pain when the main site’s down. Anyone using this to get in? Let me know! Here’s the link: bong88comlogin

  9. 3 月前
    2026-1-11 2:55:45

    Yo, need a quick agbong88login. I heard this gives direct access. Is it reliable? Tired of the endless redirects. Feedback, anyone? Have a glance: agbong88login

  10. 3 月前
    2026-1-11 2:56:02

    Is bong88org a legitimate Bong88 alternative? I need a backup plan if my usual link goes down. If anyone has tested it please give confirmation. Visit: bong88org

  11. 3 月前
    2026-1-18 13:10:19

    Shbet.80, I like it! Payouts are quick, the games are fun, Customer support seems decent too. Nice work. shbet.80

  12. 3 月前
    2026-1-18 13:10:36

    Yo, check out 123win92! Been playing there lately and the vibes are solid. Games are smooth, and payouts are quick. Definitely worth a look if you’re looking for a new spot. Check it out here 123win92.

  13. 3 月前
    2026-1-18 13:10:52

    Okay, if you’re looking for something a bit more specific, give vn88 rezence việt nam a try. I’ve been browsing through what they’ve got cooking and it might be right for you. Dive in vn88 rezence việt nam.

  14. 2 月前
    2026-3-03 16:52:58

    Alright galera, WJ Cassino… é confiável? I’ve played a bit (testing the waters, sabe?). So far so good, pagamento ok. Let’s see se paga certo. Confira wj cassino é confiável e me fala o que achas.

  15. 2 月前
    2026-3-03 16:53:15

    Wstar77 slot, not bad, not bad at all. The slots they have are varied. I think their payout rate is good. See for yourself here: wstar77 slot

  16. 2 月前
    2026-3-03 16:53:31

    For all things ‘gà choic1.com’, look no further. A great resouce and community! It gets two thumbs up and a cackle from me. gà choic1.com

  17. 2 月前
    2026-3-07 18:14:19

    Interesting read! The blend of traditional gaming with modern tech, like seen on betmgm casino link, is really changing the landscape. Rigorous security & user experience are key, and it’s good to see platforms prioritizing that! 🤔

  18. 1 月前
    2026-3-19 3:34:34

    That’s a fascinating take on longshot strategies! Seeing platforms like downloading apps vip prioritize secure registration gives me confidence in online gaming’s future. Solid analysis! 🐎

  19. 1 月前
    2026-3-19 21:01:36

    Solid risk controls on this PAGCOR platform; I appreciate the strict KYC and secure channels like GCash. For safer online gaming in PH, check out Arion Pay today. Always verify RTP stats before playing slots or live casino games to manage your bankroll wisely and responsibly.

  20. 2 周前
    2026-4-06 9:12:15

    Thinking of trying cryptocasinoplinko. I like the idea of using crypto. Seems secure and quick. Plinko is my jam! Visit the site here: cryptocasinoplinko

  21. 2 周前
    2026-4-06 9:12:31

    Downloaded the ninecasinoapp. Seems pretty smooth. Great way to kill time on my commute. Hopefully, I can earn some extra money doing so! Download at ninecasinoapp

  22. 2 周前
    2026-4-06 9:12:47

    Just chilling and playing gpinasslot. Simple fun and hoping for that lucky spin. Crossing my fingers for a big win! Try the slots at gpinasslot

  23. 5 天前
    2026-4-16 12:36:17

    Hey there, uuu88love is a decent option if you are looking for a few new places to try your luck at! You might hit a jackpot, who knows. Check it out! uuu88love

  24. 5 天前
    2026-4-16 12:36:33

    Guys, mx58casino has been in my rotation for a bit now. It has a good selection and is easy to use. Worth a try at least! Check it! mx58casino

  25. 5 天前
    2026-4-16 12:36:50

    Just a heads up, pgbetlogin is a thing. I’ve checked it out, and it’s pretty standard. If you’re looking for something new, maybe give it a look. Here’s the link: pgbetlogin

发送评论 编辑评论


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