概述 總體來說,可以有如下3種辦法:
1、通過github的文件直接導(dǎo)入
2、通過docker 鏡像導(dǎo)入
3、通過LxRunOffline安裝
docker鏡像導(dǎo)入 Linux 發(fā)行版 通過使用 tar 文件導(dǎo)入任何 Linux 發(fā)行版,可在適用于 Linux 的 Windows 子系統(tǒng) (WSL) 中使用該發(fā)行版(即使它不在 Microsoft Store 中提供)。
-- 從容器中導(dǎo)出
docker pull centos:7.9.2009
docker run -d --name lhrcentos79 -h lhrcentos79 --privileged=true centos:7.9.2009 /usr/sbin/init
docker export lhrcentos79 > /mnt/f/lhrcentos79.tar
wsl --import centos79 F:\wsl2centos79 f:\lhrcentos79.tar
wsl --set-default centos79
wsl -d centos79
-- 解決Failed to get D-Bus connection: Operation not permitted
mkdir -p /opt/distrod
cd /opt/distrod
wget https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz
tar xvf opt_distrod.tar.gz
rm -rf opt_distrod.tar.gz
/opt/distrod/bin/distrod enable --start-on-windows-boot
wsl --shutdown
wsl
systemctl status | cat
參考 https://learn.microsoft.com/zh-cn/windows/wsl/use-custom-distro
通過github開源包直接導(dǎo)入 參考地址: https://www./how-to/install-centos-on-windows-10-subsystem-for-linux.html
下載 CentOS 鏡像包
以下是 Github 上的一個(gè)開源項(xiàng)目,提供適用于 Linux 的 Windows 子系統(tǒng)的 CentOS 的下載。
https://github.com/mishamosher/CentOS-WSL
解壓鏡像包后,得到以下兩個(gè)文件
分別是:rootfs.tar.gz
, CentOS.exe
安裝運(yùn)行
右鍵 CentOS.exe
文件, 以管理員身份運(yùn)行
安裝完成
安裝完成后,您將在 CentOS 命令屏幕上收到一條消息。現(xiàn)在按 Enter 鍵退出設(shè)置。 運(yùn)行 CentOS 7
一種是:在安裝目錄中找到 CentOS7.exe
雙擊運(yùn)行,進(jìn)入到 linux 系統(tǒng)
另一種:在cmd 中 輸入 wsl
,進(jìn)入到 linux 系統(tǒng) 更新 yum 要卸載它,請(qǐng)將 PowerShell 作為管理員切換到 CentOS 目錄, ./CentOS.exe clean
通過LxRunOffline安裝 在 WSL2 中安裝完成 CentOS 系統(tǒng)后,基于 systemctl 或 service 命令的服務(wù)無法運(yùn)行,比如 nginx php-fpm mysql docker等。
一、安裝 chocolatey 和 LxRunOffline Chocolatey 是一個(gè) Windows 包管理工具,LxRunOffline 是一個(gè) WSL 的管理軟件。用 choco 安裝 LxRunOffine ,然后用 LxRunOffline 安裝各種版本的 Linux 鏡像。
NuGet(讀作New Get)是用于微軟.NET開發(fā)平臺(tái)的軟件包管理器,是一個(gè)Visual Studio的擴(kuò)展。Chocolatey 是基于 NuGet 的一個(gè)軟件包管理器,就像 Linux 中的 yum 或 apt 一樣,在 Windows10 中也可以用命令行安裝程序了。
右鍵單擊開始菜單,選擇 Windows PowerShell(管理員),打開一個(gè)具有管理員權(quán)限的 PowerShell 窗口,輸入命令并回車:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https:///install.ps1'))
-- 或
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://community./install.ps1'))
完成后,輸入命令:choco
,如果能正確顯示版本號(hào),說明安裝成功。
LxRunOffline 是非常實(shí)用的 WSL 管理軟件,可以備份、創(chuàng)建、恢復(fù)、導(dǎo)出WSL子系統(tǒng),也可以安裝適配 WSL 的任何 Linux 發(fā)行版,可以將 WSL 子系統(tǒng)安裝到任意目錄中。
在 PowerShell 窗口中輸入命令安裝LxRunOffline,安裝完成后重啟 PowerShell。
choco install lxrunoffline -y
-- 升級(jí)
choco upgrade lxrunoffline
若不能安裝,報(bào)如下的錯(cuò)誤,則可以下載離線安裝包:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip
PS C:\Users\lhr> choco install lxrunoffline -y
Chocolatey v1.1.0
Installing the following packages:
lxrunoffline
By installing, you accept licenses for the packages.
Error retrieving packages from source 'https://community./api/v2/':
基礎(chǔ)連接已經(jīng)關(guān)閉: 發(fā)送時(shí)發(fā)生錯(cuò)誤。
lxrunoffline not installed. The package was not found with the source(s) listed.
Source(s): 'https://community./api/v2/'
NOTE: When you specify explicit sources, it overrides default sources.
If the package version is a prerelease and you didn't specify `--pre`,
the package may not be found.
Please see https://docs./en-us/troubleshooting for more
assistance.
Chocolatey installed 0/1 packages. 1 packages failed.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Failures
- lxrunoffline - lxrunoffline not installed. The package was not found with the source(s) listed.
Source(s): 'https://community./api/v2/'
NOTE: When you specify explicit sources, it overrides default sources.
If the package version is a prerelease and you didn't specify `--pre`,
the package may not be found.
Please see https://docs./en-us/troubleshooting for more
assistance.
并且配置環(huán)境變量:
setx PATH "%path%;F:\LxRunOffline-v3.5.0-msvc"
檢查:
C:\Users\lhr>LxRunoffline version
LxRunOffline v3.5.0
在命令行窗口中,使用"LxRunoffline version"命令;若成功顯示版本號(hào),則代表安裝成功
choco 其它命令如下:
choco -v 查看 choco 版本
choco list -li 查看本地安裝的軟件
choco search nodejs 查找安裝包
choco install sublimetext3 下載
choco uninstall sublimetext3 卸載
choco upgrade sublimetext3 更新升級(jí)(update)
choco upgrade chocolatey 更新升級(jí) chocolatey 自己
二、下載 適用于 WSL 的 Linux 發(fā)行版鏡像 Linux 發(fā)行版鏡像可以到 http://hub. 去找,參考:https://github.com/RoliSoft/WSL-Distribution-Switcher
以 CentOS 為例,打開https://hub./_/centos/
下載 CentOS8 鏡像:
https://raw./CentOS/sig-cloud-instance-images/CentOS-8-x86_64/docker/centos-8-x86_64.tar.xz
下載 CentOS7 鏡像:
https://raw./CentOS/sig-cloud-instance-images/CentOS-7-x86_64/docker/centos-7-x86_64-docker.tar.xz
其它鏡像地址:https://github.com/CentOS/sig-cloud-instance-images
三、安裝 CentOS 鏡像 安裝WSL子系統(tǒng)的命令行格式為:
LxRunOffline install -n 自定義系統(tǒng)名稱 -d 安裝目錄路徑 -f 安裝包路徑.tar.xz
重新啟動(dòng) PowerShell,輸入命令安裝 CentOS 鏡像:
LxRunOffline install -n CentOS8 -d f:/CentOS8 -f f:\centos-8-x86_64.tar.xz
LxRunOffline install -n CentOS7 -d f:/CentOS7 -f f:\centos-7-x86_64-docker.tar.xz
這行命令表示將 CentOS 系統(tǒng)安裝到 f盤的 centos 文件夾下,并且命名為CentOS8。注意:Windows 系統(tǒng)命令行中的文件路徑和 Linux 系統(tǒng)不同,這里安裝目錄 f:/
要用 Linux 方式的反斜線表示,是為了 WSL 使用;而安裝包路徑 f:\
是為了 LxRunOffline 使用,是 Windows 下的路徑。
查看所有已安裝鏡像,能夠顯示已安裝的 centos ,表示安裝成功。
如果安裝失敗也會(huì)出現(xiàn) centos 目錄,安裝失敗重新安裝時(shí)需要卸載鏡像,另外安裝目錄在 C 盤在卸載時(shí)可能存在權(quán)限問題,卸載后如果目錄沒有刪除可以手動(dòng)刪除目錄。
卸載 WSL 系統(tǒng)鏡像命令:
LxRunOffline.exe uninstall -n centos
LxRunOffline的其它命令:
//已經(jīng)安裝的WSL
LxRunOffline list
//還原WSL
LxRunOffline install -n <wsl_name> -d <res_path> -f <back_path>
//卸載WSL
LxRunOffline uninstall -n <wsl_name>
//備份WSL
LxRunOffline export -n <wsl_name> -f <back_path>
eg:LxRunOffline export -n CentOS -f D:\LinuxSubSystem\WSL\centos7-diy-20210430.tar.xz
//啟動(dòng)一個(gè)WSL
LxRunOffline run -n <wslname>
`l` , `list` - 列出所有已安裝的發(fā)行版。
`gd` , `get-default` - 獲取 bash.exe 使用的默認(rèn)發(fā)行版。
`sd` , `set-default` - 設(shè)置 bash.exe 使用的默認(rèn)發(fā)行版。
`i` , `install` - 安裝新的發(fā)行版。
`sd` , `set-default` - 設(shè)置 bash.exe 使用的默認(rèn)發(fā)行版。
`ui` , `uninstall` - 卸載發(fā)行版。
`rg` , `register` - 注冊(cè)現(xiàn)有的安裝目錄。
`ur` , `unregister` - 取消注冊(cè)發(fā)行版但不刪除安裝目錄。
`m` , `move` - 將發(fā)行版移動(dòng)到新目錄。
`d` , `duplicate` - 在新目錄中復(fù)制現(xiàn)有發(fā)行版。
`e` , `export` - 將發(fā)行版的文件系統(tǒng)導(dǎo)出到`.tar.gz` 文件,該文件可以通過 `install` 命令安裝。
`r` , `run` - 在發(fā)行版中運(yùn)行命令。
`di` , `get-dir` - 獲取發(fā)行版的安裝目錄。
`gv` , `get-version` - 獲取發(fā)行版的文件系統(tǒng)版本。
`ge` , `get-env` - 獲取發(fā)行版的默認(rèn)環(huán)境變量。
`se` , `set-env` - 設(shè)置發(fā)行版的默認(rèn)環(huán)境變量。
`ae` , `add-env` - 添加到發(fā)行版的默認(rèn)環(huán)境變量。
`re` , `remove-env` - 從發(fā)行版的默認(rèn)環(huán)境變量中刪除。
`gu` , `get-uid` - 獲取發(fā)行版的默認(rèn)用戶的 UID。
`su` , `set-uid` - 設(shè)置發(fā)行版的默認(rèn)用戶的 UID。
`gk` , `get-kernelcmd` - 獲取發(fā)行版的默認(rèn)內(nèi)核命令行。
`sk` , `set-kernelcmd` - 設(shè)置發(fā)行版的默認(rèn)內(nèi)核命令行。
`gf` , `get-flags` - 獲取發(fā)行版的一些標(biāo)志。
`sf` , `set-flags` - 設(shè)置發(fā)行版的一些標(biāo)志。
`s` , `shortcut` - 創(chuàng)建啟動(dòng)發(fā)行版的快捷方式。
`ec` , `export-config` - 將發(fā)行版配置導(dǎo)出到 XML 文件。
`ic` , `import-config` - 從 XML 文件導(dǎo)入發(fā)行版的配置。
`sm` , `summary` - 獲取發(fā)行版的一般信息。
四、轉(zhuǎn)換為 WSL2 輸入命令并回車,列出已經(jīng)安裝的 WSL 的信息
將對(duì)應(yīng)的 WSL 發(fā)行版設(shè)為 WSL2,命令格式:
wsl --set-version <Distro> 2
其中要和上面查詢到的信息一致,本例中為 centos,輸入以下命令并回車:
wsl --set-version centos 2
輸入以下命令并回車,設(shè)置默認(rèn)使用的發(fā)行版:
此時(shí),再次使用命令 wsl -l -v 查看,可以看到 centos 的 VERSION 已經(jīng)變?yōu)?2,并且名稱前加了*號(hào)(即默認(rèn)的WSL系統(tǒng))。
五、安裝 CentOS 中的常用工具 1、安裝 wget vim 等工具 -- 報(bào)錯(cuò):Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.|baseurl=http://vault.|g' /etc/yum.repos.d/CentOS-*
cd ~ #轉(zhuǎn)到用戶目錄
dnf -y install vim wget
dnf -y install net-tools #啟用 ifconfig,netstat 等網(wǎng)絡(luò) 命令
dnf -y install git
dnf -y install make
dnf -y install initscripts #啟用 service 命令
2、更換國內(nèi)源 (1) 備份原始的yum源
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
mv CentOS-Extras.repo CentOS-Extras.repo.bak
mv CentOS-centosplus.repo CentOS-centosplus.repo.bak
mv CentOS-PowerTools.repo CentOS-PowerTools.repo.bak
mv CentOS-AppStream.repo CentOS-AppStream.repo.bak
(2) 下載對(duì)應(yīng)版本的repo文件
# wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
vim CentOS-Base.repo
可以看到 aliyun 的源包含的以下各項(xiàng):[base] [extras] [centosplus] [PowerTools] [AppStream] ,因此前面對(duì)這些源都做了備份。
(3) 清除舊的緩存并生成新的緩存
# dnf clean all
# dnf makecache
六、WSL2 與 systemd 進(jìn)程 WSL2 本身是由 Windows 負(fù)責(zé)運(yùn)行的,因此使用 tree 或 ps 命令時(shí)會(huì)看到根進(jìn)程不是 systemd,這將導(dǎo)致無法啟動(dòng) Linux 系統(tǒng)服務(wù)的守護(hù)進(jìn)程(deamon)。當(dāng)我們執(zhí)行 systemctl 命令的時(shí)候,會(huì)顯示出我們的 init system (PID 1) 并非 systemd,而是微軟提供的 /init。
$ systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
$ ps u -q 1
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 908 592 ? Sl 10:31 0:00 /init
或許在以后 WSL 的原生 systemd 都會(huì)實(shí)現(xiàn),但是現(xiàn)在我們?nèi)绻胍獡碛幸粋€(gè) systemd 環(huán)境的話,該怎麼辦呢?
由于 systemd 必須以 PID 1 的方式執(zhí)行,所以不能直接執(zhí)行 systemd ,但是利用 Linux Namespace 讓我們可以在 WSL2 中建立一個(gè)新的 Namespace 并把 systemd 作為 PID 1 來執(zhí)行。也就是在 WSL2 中再多加一層 PID Namespace,使得我們可以在這個(gè)新的 Namespace 中運(yùn)行 systemd 環(huán)境。
七、配置 genie ,運(yùn)行 systemctl 命令 1、安裝 .NET Core SDK 3.1 dnf install dotnet-sdk-3.1
2、安裝依賴 daemonize cd /home
wget http://repo./centos/8/x86_64/release/daemonize-1.7.8-1.el8.x86_64.rpm
rpm -ivh daemonize-1.7.8-1.el8.x86_64.rpm
如果不安裝,在運(yùn)行 genie 時(shí)會(huì)報(bào)錯(cuò),提示:找不到 daemonize
3、安裝genie git clone https://github.com/arkane-systems/genie.git
cd genie/genie
make
make install
ln -s /usr/libexec/genie/main/genie /usr/local/bin/genie
vim Makefile 查看安裝腳本,默認(rèn)安裝目錄是 /usr/libexec/genie,沒有配置軟鏈接,因此在最后一步使用 ln 命令增加了軟鏈接。
現(xiàn)在就可以借助 genie 運(yùn)行 systemctl 命令了。
也可以直接下載rpm包進(jìn)行安裝:
https://github.com/arkane-systems/genie/tags
wget https://github.com/arkane-systems/genie/releases/download/v1.44/genie-1.44-1.fc34.x86_64.rpm
dnf install -y genie-1.44-1.fc34.x86_64.rpm
cat >> /root/.bashrc <<"EOF"
if [ "`ps -eo pid,lstart,cmd | grep systemd | grep -v -e grep -e systemd- | sort -n -k2 | awk 'NR==1 { print $1 }'`" != "1" ]; then
genie -s
fi
EOF
最新的2.4版本需要python 3.7和 libc.so.6(GLIBC_2.34),否則會(huì)報(bào)錯(cuò):
-- 查看glibc的版本
strings /lib64/libc.so.6 | egrep ^GLIBC_
[root@lhrxxt soft]# dnf install genie-2.4-1.fc36.x86_64.rpm
Last metadata expiration check: 0:04:55 ago on Thu 22 Sep 2022 10:31:26 AM CST.
Error:
Problem: conflicting requests
- nothing provides libc.so.6(GLIBC_2.34)(64bit) needed by genie-2.4-1.fc36.x86_64
- nothing provides python3 >= 3.7 needed by genie-2.4-1.fc36.x86_64
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
[root@lhrxxt soft]#
[root@lhrxxt soft]# strings /lib64/libc.so.6 | egrep ^GLIBC_
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_2.13
GLIBC_2.14
GLIBC_2.15
GLIBC_2.16
GLIBC_2.17
GLIBC_2.18
GLIBC_2.22
GLIBC_2.23
GLIBC_2.24
GLIBC_2.25
GLIBC_2.26
GLIBC_2.27
GLIBC_2.28
GLIBC_PRIVATE
GLIBC_2.26
GLIBC_2.25
GLIBC_2.28
GLIBC_2.23
GLIBC_2.8
GLIBC_2.5
GLIBC_2.9
GLIBC_2.7
GLIBC_2.6
GLIBC_2.18
GLIBC_2.11
GLIBC_2.16
GLIBC_2.13
GLIBC_2.2.6
[root@lhrxxt soft]#
[root@lhrxxt soft]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
4、genie 的使用 genie 有三個(gè)指令:
genie -i 啟動(dòng) systemd 進(jìn)程
genie -s 啟動(dòng) systemd 進(jìn)程,并進(jìn)入該環(huán)境終端
genie -c <command> 啟動(dòng) systemd 進(jìn)程,并執(zhí)行相應(yīng)的指令
示例:安裝 openssh 并運(yùn)行
dnf install openssh-server
genie -c systemctl start sshd
5、使用 pstree 命令查看進(jìn)程樹 dnf -y install psmisc #安裝pstree
pstree -ap #查看進(jìn)程樹
可以看到由守護(hù)進(jìn)程 unshare 下的 systemd 空間,將會(huì)運(yùn)行所有的 systemd 進(jìn)程。
此時(shí),使用 ssh 客戶端,可以登錄這個(gè) CentOS 系統(tǒng)。
另外使用命令: genie systemctl enable sshd,不能實(shí)現(xiàn)開機(jī)啟動(dòng)。
WSL2中使用systemctl命令 相關(guān)報(bào)錯(cuò):
-- 不能運(yùn)行systemctl命令的報(bào)錯(cuò)
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Failed to get D-Bus connection: Operation not permitted
Authorization not available. Check if polkit service is running or see debug message for more information.
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables --wait -t nat -N DOCKER: iptables v1.8.4 (nf_tables): CHAIN_ADD failed (No such file or directory): chain PREROUTING
(exit status 4)
在 wsl2 中啟用 systemd 的方法至少有如下三種:
genie subsystemctl wsl-distrod 方法一:替換原有的systemctl命令 參考:https://github.com/gdraheim/docker-systemctl-replacement/blob/master/files/docker/systemctl.py
mv /usr/bin/systemctl /usr/bin/systemctl.old
curl https://raw./gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl
chmod +x /usr/bin/systemctl
若報(bào)錯(cuò),請(qǐng)手工將內(nèi)容復(fù)制進(jìn)去。
我在centOS8上運(yùn)行docker,報(bào)錯(cuò):
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables --wait -t nat -N DOCKER: iptables v1.8.4 (nf_tables): CHAIN_ADD failed (No such file or directory): chain PREROUTING
(exit status 4)
沒找到解決方法。
缺點(diǎn):
1、該方式返回的內(nèi)容比正常的命令少了很多,自己測(cè)試也有很多的問題。
參考:https://blog.csdn.net/m0_55155505/article/details/122995993
https://www.jianshu.com/p/e670ae82e97a
方法二:安裝Distrod(推薦) GitHub:https://github.com/nullpo-head/wsl-distrod
Distrod是一個(gè)基于systemd的WSL2元發(fā)行版,它允許您在一分鐘內(nèi)安裝Ubuntu,Arch Linux,Gentoo和許多其他帶有systemd的發(fā)行版,或者使您當(dāng)前的發(fā)行版以systemd運(yùn)行。
Distrod還提供內(nèi)置的自動(dòng)啟動(dòng)功能和端口轉(zhuǎn)發(fā)服務(wù)。這允許您在 Windows 啟動(dòng)時(shí)啟動(dòng) systemd 托管服務(wù),并使其可從 Windows 外部訪問。
適用的發(fā)行版 :以下發(fā)行版經(jīng)過持續(xù)測(cè)試Ubuntu, Debian, Arch Linux, Fedora, CentOS, AlmaLinux, Rocky Linux, Kali Linux, Linux Mint, openSUSE, Amazon Linux, Oracle Linux, Gentoo Linux (參見Integration test on Linux (distro_name))
情況1,安裝新系統(tǒng):Install a New Distro 請(qǐng)確保您的默認(rèn) WSL 版本為 2。 wsl --set-default-version 2
下載并解壓縮https://github.com/nullpo-head/wsl-distrod/releases/latest/download/distrod_wsl_launcher-x86_64.zip,然后雙擊exe文件。 按照向?qū)О惭b新的發(fā)行版。 [可選]若要使發(fā)行版在 Windows 啟動(dòng)時(shí)啟動(dòng),請(qǐng)運(yùn)行以下命令。 sudo /opt/distrod/bin/distrod enable --start-on-windows-boot
情況2,在現(xiàn)有系統(tǒng)上安裝:Make your Current Distro Run Systemd 通過此安裝,systemd 已在 WSL 2 發(fā)行版中啟用。
下載并運(yùn)行最新的安裝程序腳本。 curl -L -O "https://raw./nullpo-head/wsl-distrod/main/install.sh"
chmod +x install.sh
sudo ./install.sh install
此腳本安裝發(fā)行版,但尚未啟用它。
若不能正常安裝,則可以采用如下的方法:
mkdir -p /opt/distrod
cd /opt/distrod
wget https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz
tar xvf opt_distrod.tar.gz
rm -rf opt_distrod.tar.gz
在發(fā)行版中啟用發(fā)行版 您有兩種選擇。如果要在 Windows 啟動(dòng)時(shí)自動(dòng)啟動(dòng)發(fā)行版,請(qǐng)通過以下命令啟用 distrod
/opt/distrod/bin/distrod enable --start-on-windows-boot
否則
/opt/distrod/bin/distrod enable
如果要在以后啟用自動(dòng)啟動(dòng),則可以再次運(yùn)行 with 。enable--start-on-windows-boot
重新啟動(dòng)發(fā)行版 關(guān)閉 WSL 的終端。打開一個(gè)新的命令提示符窗口,然后運(yùn)行以下命令。
重新打開新的 WSL 窗口后,shell 將在 systemd 會(huì)話中運(yùn)行。
安裝完畢后,可以運(yùn)行以下命令進(jìn)行查詢
與其它方法的對(duì)比 在官方文檔中,Distrod 描述了其工作原理:
簡(jiǎn)而言之,Distrod 是一個(gè)二進(jìn)制文件,它會(huì)創(chuàng)建一個(gè)簡(jiǎn)單的容器 ,將 systemd 作為 init 進(jìn)程,并在該容器中啟動(dòng)您的 wsl 會(huì)話。為了做到這一點(diǎn), Distrod 做了以下事情:
修改具體發(fā)行版的 rootfs,以使其與 wsl 和 systemd 兼容。 修改 systemd 服務(wù),使其與 wsl 兼容。 把/opt/distrod/bin/distrod
和其它資源放進(jìn) rootfs。 把 Distrod 二進(jìn)制文件注冊(cè)為登錄 shell。 當(dāng) Distrod 作為登錄 shell 被 wsl 的 init 進(jìn)程啟動(dòng)時(shí),Distrod: 在簡(jiǎn)單的容器中啟動(dòng) systemd 在這個(gè)容器中啟動(dòng)你實(shí)際的 shell 在 systemd 會(huì)話和 wsl 交互環(huán)境間建立橋梁。 事實(shí)上 Distrod 和 genie、subsystemctl 的工作原理是類似的:創(chuàng)建一個(gè)容器,在容器內(nèi)以 pid 1 啟動(dòng) systemd 并使用 shell。在其官方文檔中也有提過:
把 Distrod 作為一個(gè)獨(dú)立的一次性命令:在這種情況下, distrod 的工作方式就像 genie 和 subsystemctl。
但是,Distrod 在自啟動(dòng)方面做了更多的工作。通過上述操作,Distrod 做到了:
安裝并啟用后,啟動(dòng) wsl 會(huì)自動(dòng)啟動(dòng) systmed。 添加--start-on-windows-boot
參數(shù)后,Distrod 會(huì)注冊(cè)一個(gè) windows 任務(wù),使得 wsl 在 windows 開機(jī)時(shí)就會(huì)運(yùn)行。 對(duì)比其它方法需要手動(dòng)編寫 shell 腳本實(shí)現(xiàn)自動(dòng)啟動(dòng),安裝更簡(jiǎn)單、使用更方便的 Distrod 無疑更被用戶青睞。
方法三:使用 genie 總結(jié) 1、推薦Distrod的離線安裝,已在centOS7、CentOS8、CentOS Stream release 9上測(cè)試通過,大部分是正常的,但是不能安裝太多的包,否則會(huì)導(dǎo)致systemctl啟動(dòng)卡住,到底是哪個(gè)包,我也不清楚 :
mkdir -p /opt/distrod
cd /opt/distrod
wget https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz
tar xvf opt_distrod.tar.gz
rm -rf opt_distrod.tar.gz
/opt/distrod/bin/distrod enable
2、推薦最低使用CentOS 7.9.2009
版本,其它版本可能會(huì)報(bào)錯(cuò)
3、使用過程感覺也有問題,有時(shí)候systemctl不能正常啟動(dòng)sshd會(huì)卡住,有時(shí)候df -h
會(huì)卡住
4、使用CentOS Stream release 9
的過程,感覺沒啥問題
5、關(guān)于WSL2中使用systemctl命令,建議還是等微軟支持吧,其實(shí)Ubuntu 就挺好的
CentOS 8的yum問題 -- 報(bào)錯(cuò):Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.|baseurl=http://vault.|g' /etc/yum.repos.d/CentOS-*
參考 https://github.com/mishamosher/CentOS-WSL
https://github.com/CentOS/sig-cloud-instance-images
https://github.com/wsldl-pg/CentWSL/releases
https://github.com/nullpo-head/wsl-distrod/
https://github.com/arkane-systems/genie/releases
https://github.com/systemd/systemd/issues/8036