适用于Linux的windows子系统的安装及使用(WSL2 GUI)

什么是WSL2

WSL全称为Windows Subsystem for Linux,官网译为:适用于 Linux 的 Windows 子系统 (WSL)。

为什么要WSL2

官方解释:可让开发人员直接在 Windows 上按原样运行 GNU/Linux 环境(包括大多数命令行工具、实用工具和应用程序),且不会产生传统虚拟机或双启动设置开销。

Windows与Linux子系统将共用同一文件系统!我们可以在WSL中使用三剑客命令查询分析windows文档、日志、使用shell命令或者bash脚本运行存储在windows中的linux程序、甚至在WSL中创建docker容器,在windows下使用docker desktop进行可视化管理。

WSL2让我们既拥有Windows的操作界面又拥有Linux的命令行工具。

启用虚拟机平台

WSL 2 需要启用 Windows 10 的 “虚拟机平台” 特性。它独立于 Hyper-V,并提供了一些在 Linux 的 Windows 子系统新版本中可用的更有趣的平台集成。

我们打开【控制面板-启用或关闭window功能】,勾选“适用于Linux的Windows子系统”和“虚拟机平台”

为了确保所有相关部件都整齐到位,您应该在此时重启系统,否则可能会发现事情没按预期进行。

安装分发版本Ubuntu

1、以管理员身份打开 PowerShell 或 cmd 并运行,查看支持的Linux版本

wsl --list --online

2、下载Ubuntu-20.04

wsl --install -d Ubuntu-20.04

我们也可以使用其他工具先将Ubuntu-20.04下载下来,下载参考文档:https://docs.microsoft.com/zh-cn/windows/wsl/install

单击打开 Ubuntu 20.04 LTS 的 Microsoft Store 页面:

在上一步打开的页面若是直接点击 获取 进行安装,会默认安装在系统盘(C盘),而且下载有时候速度也很慢。

快速下载安装包:复制上一步的红框的网址链接,单击打开:

Microsoft Store - Generation Project (v1.2.3) [by @rgadguard & mkuba50]

粘贴链接并点击 √ ,搜索结果如下图:

单击后缀是 .appxbundle 的文件链接进行下载。

3、将下载好的 .appxbundle文件后缀名修改为 .zip 并进行解压,解压后的文件目录:

4、根据自己计算机的类型,选择红框中的安装包,将选好的安装包的后缀名 .appx 修改为 .zip 并进行解压,解压后的文件目录:

5、将解压好后的文件夹,移动到自定义安装的位置,并双击执行目录下的 .exe 文件,稍等片刻,安装完成。并提示新建 Unix用户 和 设置密码。

6、如果需要 注销并卸载 WSL 发行版,在PowerShell执行如下命令:

wsl --unregister Ubuntu-20.04

7、基本的WSL命令

8、更新和升级包

大多数分发版随附了一个空的的包目录或最简单的包目录。 我们强烈建议定期更新包目录并使用分发版的首选包管理器升级已安装的包。 对于Debian/Ubuntu,请使用 apt:

sudo apt update && sudo apt upgrade

9、重置或忘记密码

(1)请打开 PowerShell,并使用以下命令进入默认 WSL 分发版的根目录:

wsl -u root

如果需要在非默认分发版中更新忘记的密码,请使用命令:wsl -d Debian -u root,并将 Debian 替换为目标分发版的名称。

(2)在 PowerShell 内的根级别打开 WSL 分发版后,可以使用此命令更新密码:

passwd

(3)系统将提示你输入新的 UNIX 密码,然后确认该密码。 在被告知密码已成功更新后,请使用以下命令在 PowerShell 内关闭 WSL:

exit

10、更新镜像源

cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list

 官方的源都注释掉,换成下面两个之一即可(我的是Ubuntu20.04,别的版本或者源可以自行网上搜)

  • 阿里源
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  • 清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse 

11、Ubuntu系统下用apt命令安装/删除/卸载软件包

虽然 apt 与 apt-get 有一些类似的命令选项,但它并不能完全向下兼容 apt-get 命令。也就是说,可以用 apt 替换部分 apt-get 系列命令,但不是全部

apt命令 取代的命令 命令的功能
apt install apt-get install 安装软件包
apt remove apt-get remove 移除软件包
apt purge apt purge 移除软件包及配置文件
apt update apt-get update 刷新存储库索引
apt upgrade apt-get upgrade 升级所有可升级的软件包
apt autoremove apt-get autoremove 自动删除不需要的包
apt full-upgrade apt-get dist-upgrade 在升级软件包时自动处理依赖关系
apt search apt search 搜索应用程序
apt show apt-cache show 显示装细节
apt list - 列出包含条件的包(已安装,可升级等)
apt edit-sources - 编辑源列表

安装Windows Terminal

虽然安装完成了,但是原生的linux的终端确实不够美观,毕竟爱使用bash的小伙伴哪个不希望自己的teminal更好看呢?这一点windows已经替你想好了,我们可以使用windows的另一个开源项目Windows Terminal来让我们的命令终端更好看。

 在微软商店中搜索:https://apps.microsoft.com/store/apps

 

 安装完后界面

 

posted @ 2022-08-29 14:52  残城碎梦  阅读(1381)  评论(0编辑  收藏  举报