WSL2 安装ArchLinux

WSL2 安装ArchLinux

安装ubuntu测试

从win10商店下载完直接运行一般会出错

The WSL optional component is not enabled. Please enable it and try again.
See https://aka.ms/wslinstall for details.
Error: 0x8007007e
Press any key to continue...

需要运行命令打开WSL的功能,以管理员身份打开powershell运行以下命令

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

安装完成后会提示重启,重启会更新一些组件延后正常打开就会安装了,安装提示输入用户名和密码就可以正常进入系统了。

PS:修改root用户的密码

sudo -s #切换到root用户
passwd  #设置密码

配置国内源/etc/apt/sources.list

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

执行更新

apt-get update
apt-get upgrade

可能出现broken dependencies,或者出现unmet dependencies,执行以下命令

sudo apt-get update
sudo apt-get clean
sudo apt-get autoremove

安装Windows Terminal和美化

使用WSL配合windows termianl使用更佳

从win10商店安装直接使用,通过win+r打开运行wt.exe就可以快捷打开

简单美化一下

打开设置,选择开启Json文件,其中是Terminal的配置文件

{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,

    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,


    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
			   "useAcrylic" : true,
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
			   "useAcrylic" : true,
                "hidden": false
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
                "hidden": false,
                "name": "Ubuntu-20.04",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [],

    // Add custom actions and keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
    "actions":
    [

        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },

        { "command": "find", "keys": "ctrl+shift+f" },

        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
}

其中profiles对应着每一个终端的配置,添加

"useAcrylic" : true

开启亚克力特效,还可以设置字体等(fontFace),方法类似

开启WSL2

最新版的Arch已经剔除了强制WSL2的限制也可以直接进行下一步的安装

以管理员身份打开Powershell,运行一下命令开启虚拟机平台可选功能

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

安装Linux内核更新包

设置WSL2为默认版本

wsl --set-default-version 2

尝试切换原来的为wsl的ubuntu为wsl2

wsl --set-version Ubuntu-20.04 2  #--set-version后跟开发版的名字再跟wsl版本

出现以下提示

正在进行转换,这可能需要几分钟时间...
有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
转换完成。

查看,发现版本已经改变了

wsl -l -v

安装Arch

下载 yuk7/ArchWSL,下载最新的zip包,解压运行Arch.exe,会在目录下生成系统运行文件,Arch就可以使用了

蓝奏云密码:ca81,因为文件大小限制在100M,所以是分卷压缩,注意看备注

百度网盘提取码:9kyw

Arch初始化配置

首先打开/etc/pacman.conf配置文件,配置一下源,打开

[multilib]
Include = /etc/pacman.d/mirrorlist

结尾添加archlinuxcn的源

[archlinuxcn]
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch

更新一下源

pacman -Syy  

初始化一下

pacman-key --init
pacman-key --populate
pacman -S archlinuxcn-keyring

更新

pacman -Syyu  #强烈建议先更新再进行下面的操作

给当前账号设置密码

passwd

安装一些基本的软件

pacman -S base base-devel git curl wget zsh yay neofetch

新建用户

useradd -m -G wheel -s /bin/bash <用户名>
passwd <用户名>

将文件/etc/sudoers中的wheel ALL=(ALL) ALL那一行前面的注释去掉

vim /etc/sudoers  #文件为只读文件,不要尝试修改文件权限,强制保存命令 :w !sudo tee %

回到 Windows 中 Arch.exe 所在目录,打开命令提示符并运行 Arch.exe config --default-user $用户名,设置启动默认用户

PS:如果修改sudoers出现问题而又没有备份可以重新安装sudo解决

安装ohmyzsh

安装

  1. 安装
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  1. 可以设置主题,打开配置文件~/.zshrc
修改 ZSH_THEME="agnoster"  #是当前比较热门的主题

主题已经运用上了但是一会发现是乱码,见乱码问题

  1. 安装插件
# 自动语法高亮
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 在输入命令时会给出建议的命令
git clone git://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  1. 修改配置信息
plugins=(git zsh-syntax-highlighting zsh-autosuggestions)
  1. 使配置生效
source ~/.zshrc

有时候需要设置代理

WSL1 和 WSL2 网络的区别

在 WSL1 时代,由于 Linux 子系统和 Windows 共享了网络端口,所以访问 Windows 的代理非常简单。例如 Windows 的代理客户端监听了 8000 端口,那么只需要在 Linux 子系统中执行如下命令,就可以让当前 session 中的请求通过代理访问互联网。

export ALL_PROXY="http://127.0.0.1:8000"

但是 WSL2 基于 Hyper-V 运行,导致 Linux 子系统和 Windows 在网络上是两台各自独立的机器,从 Linux 子系统访问 Windows 首先需要找到 Windows 的 IP。

配置 WSL2 访问 Windows 上的代理

有两个关键步骤: 1. WSL2 中配置的代理要指向 Windows 的 IP; 2. Windows 上的代理客户端需要允许来自本地局域网的请求;

由于 Linux 子系统也是通过 Windows 访问网络,所以 Linux 子系统中的网关指向的是 Windows,DNS 服务器指向的也是 Windows,基于这两个特性,我们可以将 Windows 的 IP 读取出来。

例如,在 Ubuntu 子系统中,通过 cat /etc/resolv.conf 查看 DNS 服务器 IP。

# This file was automatically generated by WSL. To stop 
automatic generation of this file, add the following entry to /etc/wsl.conf:# [network]# generateResolvConf = false
nameserver 172.19.80.1

可以看到 DNS 服务器是 172.19.80.1,通过环境变量 ALL_PROXY 配置代理:

export ALL_PROXY="http://172.19.80.1:7890"

7890 是 Windows 上运行的代理客户端的端口,记得要在 Windows 代理客户端上配置允许本地局域网请求

通过curl cip.cc可以查看自己的IP地址

PS:测试还是不能走代理,防火墙新建相关规则也不行

可能的防火墙配置

首先由于windows防火墙的存在,此时可能出现ping 172.19.80.1失败

  • 新建防火墙入站规则
    • 打开控制面板\系统和安全\Windows Defender 防火墙
    • 点击入站规则->新建规则
    • 规则类型:自定义
    • 程序:所有程序
    • 协议和端口:默认即可
    • 作用域:
      • 本地ip处选择“任何IP地址”
      • 远程ip处选择“下列IP地址”,并将wsl2的IP添加进去。(通过ip addr获取wsl2的ip,带上掩码)
    • 操作:允许连接
    • 配置文件:三个全选
    • 名称描述:请自定义
    • 注意:这一步完成后,从wsl2 ping主机的ip应该可以ping通了。
  • 防火墙配置
    • 打开控制面板\系统和安全\Windows Defender 防火墙\允许的应用
    • 将与代理相关的应用程序均设置为:允许其进行专用、公用网络通信。

推荐阅读:

配置代理

posted @ 2021-07-10 11:50  云子墨  阅读(1697)  评论(0编辑  收藏  举报