Windows WSL2 环境下 Ubuntu 24.04 exit后,wsl就自动退出了无法保持在后台运行。

1. 创建服务文件

sudo nano /etc/systemd/system/wsl-keepalive.service

配置内容

[Unit]
Description=WSL Keep Alive Service
After=network.target

[Service]
Type=simple
ExecStart=/bin/sleep infinity
Restart=always

[Install]
WantedBy=multi-user.target

2. 启动服务

sudo systemctl daemon-reload

sudo systemctl enable wsl-keepalive

sudo systemctl start wsl-keepalive

3. 检查服务

systemctl status wsl-keepalive

Active: active (running)

4. 退出wsl ,然后在windows powershell 运行

 wsl -l -v                                                                                                  08:36:58
  NAME              STATE           VERSION
* Ubuntu-24.04      Running         2