重装 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)
- 添加端口转发规则:
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=2222 `
connectaddress=172.17.45.15 connectport=2222
- 删除错误的端口转发规则:
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=2222
- 用正确的 WSL IP 重建转发:
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=2222 `
connectaddress=172.17.45.15 connectport=2222
- 验证端口转发配置:
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
测试连接
- 本机(Windows)上测试:
ssh root@127.0.0.1 -p 2222
- 局域网内其他机器(用 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"
外网连接
- 访问 https://ipinfo.io/ 获取公网 IP(请关闭梯子)
- 路由器上设置端口转发:
- 外部端口(WAN端口):2222
- 内部 IP:你 Windows 主机的局域网 IP(例如 192.168.123.114)
- 内部端口:2222
- 协议:TCP
- 保存并重启路由器端口转发服务。
- 通过公网 IP 和端口连接:
ssh root@公网IP -p 2222
可选:Windows 开机自动执行脚本
- 打开任务计划程序,创建新任务
- 触发器:系统启动时
- 操作:启动程序
- 程序或脚本:powershell.exe
- 添加参数:
-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Update-WSL2-SSHPortProxy.ps1" - 保存并启用任务
很好的文章^_^
为什么匿名 耗子哥
Baklava tasting tour Tour guides are passionate about their city. https://kulturzentrum-hermannstadt.ro/?p=6248
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!
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.
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
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!
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! 👍
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
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
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
Shbet.80, I like it! Payouts are quick, the games are fun, Customer support seems decent too. Nice work. shbet.80
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.
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.