远程登录WSL2

笔记本电脑的内存用起来捉襟见肘,为了方便远程登录 Windows PC 上的 WSL 2 环境进行开发,需要开启 Windows 的 OpenSSH 服务端功能。整个链路结构如下:

---
config:
  theme: 'base'
  themeVariables:
    darkMode: true
    fontSize: 16px
    primaryColor: '#000'
    primaryTextColor: '#fff'
    primaryBorderColor: '#02d7f2'
    lineColor: '#fcee09'
    tertiaryColor: '#0d0d0d'
    tertiaryBorderColor: '#cdcdcd'
---
flowchart LR
  subgraph Tailscale
    direction LR
    A
    B
    subgraph PC
      direction LR
      B
      C
    end
  end
  A["Laptop"] e1@--SSH--> B["Windows"]
  B e2@-- 本地进程通信 --> C["WSL2"]
  e1@{ animate: true }
  e2@{ animate: true }

启用OpenSSH Server

Win+R 输入 powershell 后按 Ctrl+Shift+Enter,以管理员身份运行 PowerShell:

# 查询当前系统中与OpenSSH相关的可选功能的状态
> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Name : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

# OpenSSH客户端已经安装,服务端还未安装,需要安装下
> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Path :
Online : True
RestartNeeded : False

# 启动sshd服务
> Start-Service sshd
# 设置sshd服务自启动
> Set-Service -Name sshd -StartupType 'Automatic'

PS C:\Users\Chikai> Get-Service sshd

Status Name DisplayName
------ ---- -----------
Running sshd OpenSSH SSH Server

启动 sshd 服务后,会在 C:\ProgramData\ssh 目录下生成配置文件 sshd_config 以及多种密钥对:

C:\ProgramData\ssh\ >folded
> ls
目录: C:\ProgramData\ssh
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2025/9/16 15:03 logs
-a---- 2025/9/16 15:03 7 sshd.pid
-a---- 2022/5/6 14:15 2297 sshd_config
-a---- 2025/9/16 15:03 513 ssh_host_ecdsa_key
-a---- 2025/9/16 15:03 185 ssh_host_ecdsa_key.pub
-a---- 2025/9/16 15:03 419 ssh_host_ed25519_key
-a---- 2025/9/16 15:03 105 ssh_host_ed25519_key.pub
-a---- 2025/9/16 15:03 2610 ssh_host_rsa_key
-a---- 2025/9/16 15:03 577 ssh_host_rsa_key.pub

正常情况下安装程序会自动创建防火墙规则,确认下该规则是否存在:

> if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}
# 正常情况下会显示防火墙规则已存在
Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.

配置免密登录

由于开启 OpenSSH 服务的 PC 是我的个人电脑,没有为管理员用户 thinklong 设置密码,但 SSH 登录时仍被要求输入密码,如果直接回车会被拒绝:

# 尝试SSH登录
> ssh thinklong@localhost
The authenticity of host 'localhost (::1)' can't be established.
ED25519 key fingerprint is SHA256:c+JdPZlkDWQ1gfd+0PM1eN7Pf123GydtvYe45qCV6j7.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ED25519) to the list of known hosts.
thinklong@localhost's password:
Permission denied, please try again.

熟悉 OpenSSH 服务端配置的话会知道 sshd_config 中有一个配置项 PermitEmptyPasswords 控制着是否允许无密码登录,但 Windows 上的情况要更复杂一点,需要无密码登录的可以展开查看——

Windows查看如何配置SSH无密码登录

OpenSSH 服务端配置文件中有两个选项与密码登录有关:

C:\ProgramData\ssh\sshd_config
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
PermitEmptyPasswords yes

我们可以把这两个配置项都设为 yes,重启 sshd 服务后再次尝试登录,发现仍然要求输入密码,这就是 Windows 上特殊的地方。

在 Linux 上 sshd 会自己查 /etc/shadow,如果密码是空的就能成功。但在 Windows 上 OpenSSH 不自己做验证,而是调用 Windows 的登录 API,并且在 Windows 本地安全策略中有一条“账户:使用空密码的本地账户只允许进行控制台登录”,默认为启用,需要将这条策略改为禁用才能允许空密码登录。

Windows-空密码策略

注意:家庭版系统没有组策略编辑器。


无密码登录并非重点,我们需要的是免密登录,略去为 thinklong 用户创建密钥对的过程,修改配置开启免密登录——允许公钥验证并关闭密码验证:

C:\ProgramData\ssh\sshd_config
- #PubkeyAuthentication yes
+ PubkeyAuthentication yes

# To disable tunneled clear text passwords, change to no here!
- #PasswordAuthentication yes
+ PasswordAuthentication no
- #PermitEmptyPasswords no
+ PermitEmptyPasswords no

此外,还注意到 sshd_config 文件存在如下配置:

C:\ProgramData\ssh\sshd_config
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys

Match Group administrators
AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

这段配置指定了免密登录用户的公钥存储位置,普通用户管理员用户使用两个不同文件进行验证。因此把 thinklong 用户的公钥写进 ~/.ssh/authorized_keys 是无效的,需要写进 C:\ProgramData\ssh\administrators_authorized_keys

做完以上操作,重启 sshd 服务后就能成功免密登录:

> Restart-Service sshd
> ssh thinklong@localhost
Microsoft Windows [版本 10.0.26100.4652]
(c) Microsoft Corporation。保留所有权利。

thinklong@SUPERPOW C:\Users\thinklong>

以 SSH 方式登录 Windows 后进入的是 CMD 命令行,输入 wsl 命令即可进入 WSL2。


参考资料

适用于 Windows 的 OpenSSH 入门

OpenSSH for Windows 中基于密钥的身份验证

帐户:限制本地帐户使用空白密码,仅限控制台登录

作者

ThinkLong

发布于

2025-09-16

更新于

2025-12-11

许可协议

评论

+