Contents

wsl

WSL2

windows上到linux(windows sub system for linux )

一、安装 wsl 2

如果系统中带有 wsl,则需要升级到 wsl 2。 以管理员身份打开 PowerShell(“开始”菜单 >“PowerShell”> 单击右键 >“以管理员身份运行”),然后输入以下命令:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

启动虚拟化功能,然后重启windows

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

下载 linux 内核更新包

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

修改 wsl 2 为默认的版本

wsl --set-default-version 2

下载 linux 发行版

将获得 <distro>.appx

导入系统

app-name 是 Linux 发行版 .appx 文件的名称。

Add-AppxPackage .\app_name.appx

二、wsl 基本命令

wsl 常用命令

# 安装特定的linux发行版
wsl -d <Distribution Name>

# 列出可用的 Linux 发行版
wsl -l -o

# 列出已安装的 Linux 发行版
wsl --list --verbose

# 将 WSL 版本设置为 1 或 2
wsl --set-version 2

# 设置默认 WSL 版本
wsl --set-default-version <Version>
wsl --set-default-version 2

# 设置默认 Linux 发行版
wsl --set-default <Distribution Name>


# 将目录更改为主页
wsl ~

# 通过 PowerShell 或 CMD 运行特定的 Linux 发行版
wsl --distribution <Distribution Name> --user <User Name>

# 启动一个发行版
wsl -d Ubuntu-20.04 -u root


更新 WSL
wsl --update

回滚wsl版本
wsl --update rollback

检查 WSL 状态
wsl --status

以特定用户的身份运行
wsl -u <Username>


更改发行版的默认用户
<DistributionName> config --default-user <Username>
# ubuntu config --default-user johndoe 会将 Ubuntu 发行版的默认用户更改为“johndoe”用户。

关闭
wsl --shutdown

内存限制
.wslconfig 文件

关闭一个发行版
wsl --terminate <Distribution Name>

将发行版导出到 TAR 文件
wsl --export <Distribution Name> <FileName>

导入新发行版
wsl --import <Distribution Name> <InstallLocation> <FileName>


注销或卸载 Linux 发行版,注销后就被删除,注意!!!
wsl --unregister <DistributionName>


装载磁盘或设备
wsl --mount <DiskPath>

安装特定的linux发行版

wsl -d <Distribution Name>

wsl 配置文件

https://docs.microsoft.com/zh-cn/windows/wsl/wsl-config

三、发行版维护

1、迁移工作目录

wsl 默认安装c盘里,c盘因为硬盘分区的问题,可能分配空间比较小,因此可能存在需要迁移的操作。

1)关闭运行的系统

PS C:\Users\serialt>                    
PS C:\Users\serialt> wsl -l -v 
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
  
PS C:\Users\serialt> wsl --shutdown
PS C:\Users\serialt> wsl -l -v     
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
PS C:\Users\serialt> 

2)导出与导入发行版

wsl --export docker-desktop-data "E:\wsl\docker-data\docker-desktop-data.tar"
wsl --unregister docker-desktop-data
wsl --import docker-desktop-data D:\docker\desktop "E:\wsl\docker-data\docker-desktop-data.tar" --version 2


wsl --export docker-desktop "E:\wsl\docker-data\docker-desktop.tar"
wsl --unregister docker-desktop
wsl --import docker-desktop D:\docker\desktop "E:\wsl\docker-data\docker-desktop.tar" --version 2

四、自定义发行版

参考链接:https://docs.microsoft.com/zh-cn/windows/wsl/use-custom-distro

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

本文演示了如何通过使用 Docker 容器获取 Linux 发行版 CentOS 的 tar 文件来将它导入,以便与 WSL 一起使用。 此过程可应用于导入任何 Linux 发行版。

1、获取发行版的tar文件

首先,需要获取一个 tar 文件,其中包含发行版的所有 Linux 二进制文件。

可通过多种方式获取 tar 文件,其中两种方式包括:

  • 下载提供的 tar 文件。 可在 Alpine Linux 下载站点的“微型根文件系统”部分找到 Alpine 的示例。
  • 查找 Linux 发行版容器,将实例导出为 tar 文件。 以下示例将使用 CentOS 容器演示此过程。
获取CentOS的tar文件

从容器中导出tar文件

[root@tc tabby]# docker pull rockylinux
Using default tag: latest
latest: Pulling from library/rockylinux
72a2451028f1: Pull complete
Digest: sha256:5fed5497b568bcf7a90a00965987fc099edbcf44b1179a5ef6d4b47758281ca5
Status: Downloaded newer image for rockylinux:latest
docker.io/library/rockylinux:latest

[root@tc tabby]# docker run -tid --name=rocky rockylinux
38b201fca776cc1aadf029cbf5a1d6a1fb57ff62f5d71d4ed0416d870b407550
[root@tc tabby]# docker ps
CONTAINER ID   IMAGE        COMMAND       CREATED         STATUS         PORTS     NAMES
38b201fca776   rockylinux   "/bin/bash"   4 seconds ago   Up 2 seconds             rocky
[root@tc tabby]# 
[root@tc tabby]#  dockerContainerID=$(docker container ls -a | grep -i rocky | awk '{print $1}')

[root@tc tabby]# docker export $dockerContainerID > /mnt/e/rocky.tar
[root@tc tabby]# du -sh /mnt/e/rocky.tar
202M    /mnt/e/rocky.tar

2、导入到wsl中

准备好 tar 文件后,可使用以下命令导入它:wsl --import <Distro> <InstallLocation> <FileName>

# 创建好存储发行版的目录
cd E:\wsl2
mkdir E:\wsl2\Rocky

# 导入tar文件
PS E:\wsl2> wsl --import Rocky E:\wsl2\Rocky ..\rocky.tar              
PS E:\wsl2> wsl -l -v
  NAME                   STATE           VERSION
* docker-desktop-data    Stopped         2
  Rocky                  Stopped         2
  docker-desktop         Stopped         2
  Ubuntu-20.04           Running         2
PS E:\wsl2>

3、启动

wsl -d Rocky -u root

自定义的发行版可能存在因使用system管理服务而无法使用service去管理服务

五、wsl 配置文件

1、wsl.conf

​```toml
[root@tc tcs]# cat /etc/wsl.conf 
# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
#root = /mnt/d/serialt/

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = tc
generateHosts = true
generateResolvConf = true

# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = true

# Set the user when launching a distribution with WSL.
[user]
default = root

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start
[root@tc tcs]# 
​```