合 在WSL2中安装其它Linux发行版
- 概述
- 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问题
- 参考
概述
总体来说,可以有如下3种办法:
1、通过github的文件直接导入
2、通过docker镜像导入
3、通过LxRunOffline安装
docker镜像导入 Linux 发行版
通过使用 tar 文件导入任何 Linux 发行版,可在适用于 Linux 的 Windows 子系统 (WSL) 中使用该发行版(即使它不在 Microsoft Store 中提供)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | -- 从容器中导出 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
- 解压镜像包后,得到以下两个文件
分别是:rootfs.tar.gz
,CentOS.exe
- 安装运行
右键CentOS.exe
文件, 以管理员身份运行 - 安装完成
安装完成后,您将在 CentOS 命令屏幕上收到一条消息。现在按 Enter 键退出设置。 - 运行 CentOS 7
一种是:在安装目录中找到CentOS7.exe
双击运行,进入到 linux 系统
另一种:在cmd 中 输入wsl
,进入到 linux 系统 - 更新 yum
1 | 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 窗口,输入命令并回车:
1 2 3 4 5 6 | 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。
1 2 3 4 | choco install lxrunoffline -y -- 升级 choco upgrade lxrunoffline |
若不能安装,报如下的错误,则可以下载离线安装包:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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. |
并且配置环境变量:
1 | setx PATH "%path%;F:\LxRunOffline-v3.5.0-msvc" |
检查:
1 2 | 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 镜像:
微软已经为WSL2开发支持了原生的systemd,请参考:https://www.xmmup.com/weiruanweiwsl2tigongyuanshengsystemd-zhichi.html