前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >在WSL2中安装CentOS发行版

在WSL2中安装CentOS发行版

作者头像
AiDBA宝典
发布2023-04-26 11:59:31
发布2023-04-26 11:59:31
11.6K00
代码可运行
举报
运行总次数:0
代码可运行

概述

总体来说,可以有如下3种办法:

1、通过github的文件直接导入

2、通过docker镜像导入

3、通过LxRunOffline安装

docker镜像导入 Linux 发行版

通过使用 tar 文件导入任何 Linux 发行版,可在适用于 Linux 的 Windows 子系统 (WSL) 中使用该发行版(即使它不在 Microsoft Store 中提供)。

代码语言:javascript
代码运行次数:0
运行
复制
-- 从容器中导出
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开源包直接导入

参考地址: https://www.how2shout.com/how-to/install-centos-on-windows-10-subsystem-for-linux.html

下载 CentOS 镜像包 以下是 Github 上的一个开源项目,提供适用于 Linux 的 Windows 子系统的 CentOS 的下载。 https://github.com/mishamosher/CentOS-WSL

  1. 解压镜像包后,得到以下两个文件 分别是:rootfs.tar.gz , CentOS.exe
  1. 安装运行 右键 CentOS.exe文件, 以管理员身份运行
  1. 安装完成 安装完成后,您将在 CentOS 命令屏幕上收到一条消息。现在按 Enter 键退出设置。
  2. 运行 CentOS 7 一种是:在安装目录中找到 CentOS7.exe 双击运行,进入到 linux 系统 另一种:在cmd 中 输入 wsl,进入到 linux 系统
  3. 更新 yum
代码语言:javascript
代码运行次数:0
运行
复制
yum -y update

要卸载它,请将 PowerShell 作为管理员切换到 CentOS 目录, ./CentOS.exe clean

通过LxRunOffline安装

在 WSL2 中安装完成 CentOS 系统后,基于 systemctl 或 service 命令的服务无法运行,比如 nginx php-fpm mysql docker等。

一、安装 chocolatey 和 LxRunOffline

Chocolatey 是一个 Windows 包管理工具,LxRunOffline 是一个 WSL 的管理软件。用 choco 安装 LxRunOffine ,然后用 LxRunOffline 安装各种版本的 Linux 镜像。

NuGet(读作New Get)是用于微软.NET开发平台的软件包管理器,是一个Visual Studio的扩展。Chocolatey 是基于 NuGet 的一个软件包管理器,就像 Linux 中的 yum 或 apt 一样,在 Windows10 中也可以用命令行安装程序了。

右键单击开始菜单,选择 Windows PowerShell(管理员),打开一个具有管理员权限的 PowerShell 窗口,输入命令并回车:

代码语言:javascript
代码运行次数:0
运行
复制
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

-- 或
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

完成后,输入命令:choco ,如果能正确显示版本号,说明安装成功。

LxRunOffline 是非常实用的 WSL 管理软件,可以备份、创建、恢复、导出WSL子系统,也可以安装适配 WSL 的任何 Linux 发行版,可以将 WSL 子系统安装到任意目录中。

在 PowerShell 窗口中输入命令安装LxRunOffline,安装完成后重启 PowerShell。

代码语言:javascript
代码运行次数:0
运行
复制
choco install lxrunoffline -y

-- 升级
choco upgrade lxrunoffline

若不能安装,报如下的错误,则可以下载离线安装包:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip

代码语言:javascript
代码运行次数:0
运行
复制
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.chocolatey.org/api/v2/':
 基础连接已经关闭: 发送时发生错误。
lxrunoffline not installed. The package was not found with the source(s) listed.
 Source(s): 'https://community.chocolatey.org/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.chocolatey.org/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.chocolatey.org/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.chocolatey.org/en-us/troubleshooting for more
 assistance.

并且配置环境变量:

代码语言:javascript
代码运行次数:0
运行
复制
setx PATH  "%path%;F:\LxRunOffline-v3.5.0-msvc"

检查:

代码语言:javascript
代码运行次数:0
运行
复制
C:\Users\lhr>LxRunoffline version
LxRunOffline v3.5.0

在命令行窗口中,使用"LxRunoffline version"命令;若成功显示版本号,则代表安装成功

choco 其它命令如下:

choco -v 查看 choco 版本 choco list -li 查看本地安装的软件 choco search nodejs 查找安装包 choco install sublimetext3 下载 choco uninstall sublimetext3 卸载 choco upgrade sublimetext3 更新升级(update) choco upgrade chocolatey 更新升级 chocolatey 自己

二、下载 适用于 WSL 的 Linux 发行版镜像

Linux 发行版镜像可以到 http://hub.docker.com 去找,参考:https://github.com/RoliSoft/WSL-Distribution-Switcher

以 CentOS 为例,打开https://hub.docker.com/_/centos/

下载 CentOS8 镜像:

https://raw.githubusercontent.com/CentOS/sig-cloud-instance-images/CentOS-8-x86_64/docker/centos-8-x86_64.tar.xz

下载 CentOS7 镜像:

https://raw.githubusercontent.com/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子系统的命令行格式为:

代码语言:javascript
代码运行次数:0
运行
复制
LxRunOffline install -n 自定义系统名称 -d 安装目录路径 -f 安装包路径.tar.xz

重新启动 PowerShell,输入命令安装 CentOS 镜像:

代码语言:javascript
代码运行次数:0
运行
复制
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 系统安装到 f盘的 centos 文件夹下,并且命名为CentOS8。注意:Windows 系统命令行中的文件路径和 Linux 系统不同,这里安装目录 f:/ 要用 Linux 方式的反斜线表示,是为了 WSL 使用;而安装包路径 f:\ 是为了 LxRunOffline 使用,是 Windows 下的路径。

查看所有已安装镜像,能够显示已安装的 centos ,表示安装成功。

代码语言:javascript
代码运行次数:0
运行
复制
LxRunOffline.exe list

如果安装失败也会出现 centos 目录,安装失败重新安装时需要卸载镜像,另外安装目录在 C 盘在卸载时可能存在权限问题,卸载后如果目录没有删除可以手动删除目录。

卸载 WSL 系统镜像命令:

代码语言:javascript
代码运行次数:0
运行
复制
LxRunOffline.exe uninstall -n centos

LxRunOffline的其它命令:

代码语言:javascript
代码运行次数:0
运行
复制
//已经安装的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
//启动一个WSL
LxRunOffline run -n <wslname>


`l` , `list` - 列出所有已安装的发行版。
`gd` , `get-default` - 获取 bash.exe 使用的默认发行版。
`sd` , `set-default` - 设置 bash.exe 使用的默认发行版。
`i` , `install` - 安装新的发行版。
`sd` , `set-default` - 设置 bash.exe 使用的默认发行版。
`ui` , `uninstall` - 卸载发行版。
`rg` , `register` - 注册现有的安装目录。
`ur` , `unregister` - 取消注册发行版但不删除安装目录。
`m` , `move` - 将发行版移动到新目录。
`d` , `duplicate` - 在新目录中复制现有发行版。
`e` , `export` - 将发行版的文件系统导出到`.tar.gz` 文件,该文件可以通过 `install` 命令安装。
`r` , `run` - 在发行版中运行命令。
`di` , `get-dir` - 获取发行版的安装目录。
`gv` , `get-version` - 获取发行版的文件系统版本。
`ge` , `get-env` - 获取发行版的默认环境变量。
`se` , `set-env` - 设置发行版的默认环境变量。
`ae` , `add-env` - 添加到发行版的默认环境变量。
`re` , `remove-env` - 从发行版的默认环境变量中删除。
`gu` , `get-uid` - 获取发行版的默认用户的 UID。
`su` , `set-uid` - 设置发行版的默认用户的 UID。
`gk` , `get-kernelcmd` - 获取发行版的默认内核命令行。
`sk` , `set-kernelcmd` - 设置发行版的默认内核命令行。
`gf` , `get-flags` - 获取发行版的一些标志。
`sf` , `set-flags` - 设置发行版的一些标志。
`s` , `shortcut` - 创建启动发行版的快捷方式。
`ec` , `export-config` - 将发行版配置导出到 XML 文件。
`ic` , `import-config` - 从 XML 文件导入发行版的配置。
`sm` , `summary` - 获取发行版的一般信息。

四、转换为 WSL2

输入命令并回车,列出已经安装的 WSL 的信息

代码语言:javascript
代码运行次数:0
运行
复制
wsl -l -v

将对应的 WSL 发行版设为 WSL2,命令格式:

代码语言:javascript
代码运行次数:0
运行
复制
wsl --set-version <Distro> 2

其中要和上面查询到的信息一致,本例中为 centos,输入以下命令并回车:

代码语言:javascript
代码运行次数:0
运行
复制
wsl --set-version centos 2

输入以下命令并回车,设置默认使用的发行版:

代码语言:javascript
代码运行次数:0
运行
复制
wsl -d centos

此时,再次使用命令 wsl -l -v 查看,可以看到 centos 的 VERSION 已经变为 2,并且名称前加了*号(即默认的WSL系统)。

五、安装 CentOS 中的常用工具

1、安装 wget vim 等工具
代码语言:javascript
代码运行次数:0
运行
复制
-- 报错: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.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

cd ~ #转到用户目录
dnf -y install vim wget
dnf -y install net-tools #启用 ifconfig,netstat 等网络 命令
dnf -y install git
dnf -y install make
dnf -y install initscripts #启用 service 命令
2、更换国内源

(1) 备份原始的yum源

代码语言:javascript
代码运行次数:0
运行
复制
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) 下载对应版本的repo文件

代码语言:javascript
代码运行次数:0
运行
复制
# wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
vim CentOS-Base.repo

可以看到 aliyun 的源包含的以下各项:[base] [extras] [centosplus] [PowerTools] [AppStream] ,因此前面对这些源都做了备份。

(3) 清除旧的缓存并生成新的缓存

代码语言:javascript
代码运行次数:0
运行
复制
# dnf clean all 
# dnf makecache
六、WSL2 与 systemd 进程

WSL2 本身是由 Windows 负责运行的,因此使用 tree 或 ps 命令时会看到根进程不是 systemd,这将导致无法启动 Linux 系统服务的守护进程(deamon)。当我们执行 systemctl 命令的时候,会显示出我们的 init system (PID 1) 并非 systemd,而是微软提供的 /init。

代码语言:javascript
代码运行次数:0
运行
复制
$ 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 都会实现,但是现在我们如果想要拥有一个 systemd 环境的话,该怎麽办呢?

由于 systemd 必须以 PID 1 的方式执行,所以不能直接执行 systemd ,但是利用 Linux Namespace 让我们可以在 WSL2 中建立一个新的 Namespace 并把 systemd 作为 PID 1 来执行。也就是在 WSL2 中再多加一层 PID Namespace,使得我们可以在这个新的 Namespace 中运行 systemd 环境。

七、配置 genie ,运行 systemctl 命令

1、安装 .NET Core SDK 3.1
代码语言:javascript
代码运行次数:0
运行
复制
dnf install dotnet-sdk-3.1
2、安装依赖 daemonize
代码语言:javascript
代码运行次数:0
运行
复制
cd /home
wget http://repo.okay.com.mx/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

如果不安装,在运行 genie 时会报错,提示:找不到 daemonize

3、安装genie
代码语言:javascript
代码运行次数:0
运行
复制
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 查看安装脚本,默认安装目录是 /usr/libexec/genie,没有配置软链接,因此在最后一步使用 ln 命令增加了软链接。

现在就可以借助 genie 运行 systemctl 命令了。

也可以直接下载rpm包进行安装:

https://github.com/arkane-systems/genie/tags

代码语言:javascript
代码运行次数:0
运行
复制
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),否则会报错:

代码语言:javascript
代码运行次数:0
运行
复制
-- 查看glibc的版本
strings /lib64/libc.so.6 | egrep ^GLIBC_
代码语言:javascript
代码运行次数:0
运行
复制
[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 有三个指令:

代码语言:javascript
代码运行次数:0
运行
复制
genie -i 启动 systemd 进程
genie -s 启动 systemd 进程,并进入该环境终端
genie -c <command> 启动 systemd 进程,并执行相应的指令

示例:安装 openssh 并运行

代码语言:javascript
代码运行次数:0
运行
复制
dnf install openssh-server
genie -c systemctl start sshd
5、使用 pstree 命令查看进程树
代码语言:javascript
代码运行次数:0
运行
复制
dnf -y install psmisc #安装pstree 
pstree -ap #查看进程树

可以看到由守护进程 unshare 下的 systemd 空间,将会运行所有的 systemd 进程。

此时,使用 ssh 客户端,可以登录这个 CentOS 系统。

另外使用命令: genie systemctl enable sshd,不能实现开机启动。

WSL2中使用systemctl命令

相关报错:

代码语言:javascript
代码运行次数:0
运行
复制
-- 不能运行systemctl命令的报错
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 的方法至少有如下三种:

  1. genie
  2. subsystemctl
  3. wsl-distrod

方法一:替换原有的systemctl命令

参考:https://github.com/gdraheim/docker-systemctl-replacement/blob/master/files/docker/systemctl.py

代码语言:javascript
代码运行次数:0
运行
复制
mv /usr/bin/systemctl /usr/bin/systemctl.old
curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl
chmod +x /usr/bin/systemctl

若报错,请手工将内容复制进去。

我在centOS8上运行docker,报错:

代码语言:javascript
代码运行次数:0
运行
复制
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)

没找到解决方法。

缺点: 1、该方式返回的内容比正常的命令少了很多,自己测试也有很多的问题。

参考: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是一个基于systemd的WSL2元发行版,它允许您在一分钟内安装Ubuntu,Arch Linux,Gentoo和许多其他带有systemd的发行版,或者使您当前的发行版以systemd运行。

Distrod还提供内置的自动启动功能和端口转发服务。这允许您在 Windows 启动时启动 systemd 托管服务,并使其可从 Windows 外部访问。

  • 适用的发行版:以下发行版经过持续测试
  • 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,安装新系统:Install a New Distro
  1. 请确保您的默认 WSL 版本为 2。
代码语言:javascript
代码运行次数:0
运行
复制
wsl --set-default-version 2
  1. 下载并解压缩https://github.com/nullpo-head/wsl-distrod/releases/latest/download/distrod_wsl_launcher-x86_64.zip,然后双击exe文件。
  2. 按照向导安装新的发行版。
  3. [可选]若要使发行版在 Windows 启动时启动,请运行以下命令。
代码语言:javascript
代码运行次数:0
运行
复制
sudo /opt/distrod/bin/distrod enable --start-on-windows-boot
情况2,在现有系统上安装:Make your Current Distro Run Systemd

通过此安装,systemd 已在 WSL 2 发行版中启用。

  1. 下载并运行最新的安装程序脚本。
代码语言:javascript
代码运行次数:0
运行
复制
curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh"
chmod +x install.sh
sudo ./install.sh install

此脚本安装发行版,但尚未启用它。

若不能正常安装,则可以采用如下的方法:

代码语言:javascript
代码运行次数:0
运行
复制
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
  1. 在发行版中启用发行版

您有两种选择。如果要在 Windows 启动时自动启动发行版,请通过以下命令启用 distrod

代码语言:javascript
代码运行次数:0
运行
复制
/opt/distrod/bin/distrod enable --start-on-windows-boot

否则

代码语言:javascript
代码运行次数:0
运行
复制
/opt/distrod/bin/distrod enable

如果要在以后启用自动启动,则可以再次运行 with 。enable--start-on-windows-boot

  1. 重新启动发行版

关闭 WSL 的终端。打开一个新的命令提示符窗口,然后运行以下命令。

代码语言:javascript
代码运行次数:0
运行
复制
wsl --terminate Distrod

重新打开新的 WSL 窗口后,shell 将在 systemd 会话中运行。

安装完毕后,可以运行以下命令进行查询

代码语言:javascript
代码运行次数:0
运行
复制
systemctl status | cat
与其它方法的对比

在官方文档中,Distrod 描述了其工作原理:

简而言之,Distrod 是一个二进制文件,它会创建一个简单的容器,将 systemd 作为 init 进程,并在该容器中启动您的 wsl 会话。为了做到这一点, Distrod 做了以下事情:

  1. 修改具体发行版的 rootfs,以使其与 wsl 和 systemd 兼容。
  2. 修改 systemd 服务,使其与 wsl 兼容。
  3. /opt/distrod/bin/distrod和其它资源放进 rootfs。
  4. 把 Distrod 二进制文件注册为登录 shell。
  5. 当 Distrod 作为登录 shell 被 wsl 的 init 进程启动时,Distrod:
  6. 在简单的容器中启动 systemd
  7. 在这个容器中启动你实际的 shell
  8. 在 systemd 会话和 wsl 交互环境间建立桥梁。

事实上 Distrod 和 genie、subsystemctl 的工作原理是类似的:创建一个容器,在容器内以 pid 1 启动 systemd 并使用 shell。在其官方文档中也有提过:

把 Distrod 作为一个独立的一次性命令:在这种情况下, distrod 的工作方式就像 genie 和 subsystemctl。

但是,Distrod 在自启动方面做了更多的工作。通过上述操作,Distrod 做到了:

  1. 安装并启用后,启动 wsl 会自动启动 systmed。
  2. 添加--start-on-windows-boot参数后,Distrod 会注册一个 windows 任务,使得 wsl 在 windows 开机时就会运行。

对比其它方法需要手动编写 shell 脚本实现自动启动,安装更简单、使用更方便的 Distrod 无疑更被用户青睐。

方法三:使用 genie

总结

1、推荐Distrod的离线安装,已在centOS7、CentOS8、CentOS Stream release 9上测试通过,大部分是正常的,但是不能安装太多的包,否则会导致systemctl启动卡住,到底是哪个包,我也不清楚

代码语言:javascript
代码运行次数:0
运行
复制
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版本,其它版本可能会报错

3、使用过程感觉也有问题,有时候systemctl不能正常启动sshd会卡住,有时候df -h会卡住

4、使用CentOS Stream release 9的过程,感觉没啥问题

5、关于WSL2中使用systemctl命令,建议还是等微软支持吧,其实Ubuntu 就挺好的

CentOS 8的yum问题

代码语言:javascript
代码运行次数:0
运行
复制
-- 报错: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.centos.org|baseurl=http://vault.centos.org|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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DB宝 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • docker镜像导入 Linux 发行版
    • 参考
  • 通过github开源包直接导入
  • 通过LxRunOffline安装
    • 一、安装 chocolatey 和 LxRunOffline
    • 二、下载 适用于 WSL 的 Linux 发行版镜像
    • 三、安装 CentOS 镜像
    • 四、转换为 WSL2
    • 五、安装 CentOS 中的常用工具
      • 1、安装 wget vim 等工具
      • 2、更换国内源
      • 六、WSL2 与 systemd 进程
    • 七、配置 genie ,运行 systemctl 命令
      • 1、安装 .NET Core SDK 3.1
      • 2、安装依赖 daemonize
      • 3、安装genie
      • 4、genie 的使用
      • 5、使用 pstree 命令查看进程树
  • WSL2中使用systemctl命令
    • 方法一:替换原有的systemctl命令
    • 方法二:安装Distrod(推荐)
      • 情况1,安装新系统:Install a New Distro
      • 情况2,在现有系统上安装:Make your Current Distro Run Systemd
      • 与其它方法的对比
    • 方法三:使用 genie
  • 总结
  • CentOS 8的yum问题
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档