系统要求
- 对于 x64 系统:版本 1903 或更高版本,采用 内部版本 18362 或更高版本。
- 对于 ARM64 系统:版本 2004 或更高版本,采用 内部版本 19041 或更高版本。
- 低于 18362 的版本不支持 WSL 2。 使用 Windows Update 助手更新 Windows 版本。
安装
1. 下载所需文件
国内通道
- 7z2500-x64.exe
- wsl.2.5.10.0.x64.zip.001
- wsl.2.5.10.0.x64.zip.002
- wsl.2.5.10.0.x64.zip.003
- centos-7-x86_64-docker.tar.xz
- systemctl
国外通道
注意:国内通道由于文件大小限制,因此WSL内核安装包被拆成压缩分卷,需用zip软件解压(带了7z解压)
2. 新建setup.bat,并运行
@echo off
REM WSL2_CentOS_Installer.bat
:: 权限验证
fltmc >nul 2>&1 || (
echo 请求管理员权限...
powershell -Command "Start-Process '%~dpnx0' -Verb RunAs"
exit /b
)
:: 启用Windows功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
2. 安装WSL2内核
如果是国内通道,需先安装7z2500-x64.exe再解压wsl.2.5.10.0.x64.zip.001。
运行wsl.2.5.10.0.x64.msi,安装完成后重启。
使用WSL镜像
1. 导入CentOS7镜像
在cmd或powershell中执行
# %INSTALL_PATH%是你要将CentOS7安装到哪个目录下
# \path\to\centos-7-x86_64-docker.tar.xz是你的WSL镜像安装包目录
wsl --import CentOS7 "%INSTALL_PATH%" \path\to\centos-7-x86_64-docker.tar.xz
# 安装完成!使用以下命令启动:
wsl -d CentOS7
2. 启用systemd
适用于CentOS7
cd ~
# 可以在国内通道或国外通道选一个
curl -L "https://gitee.com/dph5199278/WSL2-CentOS7/releases/download/amd64/systemctl" -o systemctl
chmod +x systemctl
mv /bin/systemctl /bin/systemctl.bak
mv ~/systemctl /bin/systemctl
这样就兼容systemd服务了
这个不适用于CentOS7
以下不适用CentOS7,但以CentOS7来举例
# 进入WSL2
wsl -d CentOS7
# 新建/etc/wsl.conf
touch /etc/wsl.conf
# 编辑wsl.conf来启用systemd
# 在wsl.conf中加上以下内容保存
[boot]
systemd=true
# 退出WSL
exit
# 关闭WSL
wsl --shutdown
# 再进入,systemd就生效了
wsl -d CentOS7
这样就兼容systemd服务了
启用SSH
安装方式就不详细说明了
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
systemctl enable sshd
systemctl start sshd
# 确认sshd有无异常
sshd -t
正常后就可以通过xshell来做远程了
参照
- Win10家庭版安装docker(WSL与WSL2两种安装方式)
- WSL2-CentOS7
- CentWSL
- docker-systemctl-replacement
- in10及win11的wsl更新后,centos7无法启动systemctl
0