0%

WSL2相关配置

1. WSL配置

参考微软官方配置教程
以下的bash命令都需要用管理员身份打开PowerShell运行

1.1. Windows版本

确保windows版本大于1903.18362

1.2. 启用WSL子系统功能

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

1.3. 启用虚拟机功能

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

1.4. 下载Linux内核包

适用于 x64 计算机的 WSL2 Linux 内核更新包

1.5. WSL2设为默认版本

1
wsl --set-default-version 2

1.6. 安装Linux分发

打开Microsoft Store并选择Linux分发版,根据默认版本会直接下载运行

1.7. 查看版本

1
wsl -s -v

1.8. 启动

启动后自动打开cmd窗口,设置用户名和密码

2. 配置

2.1. Windows Terminal

在PowerShell中输入如下命令,获取一个guid

1
new-guid

直接在Windows Terminal的setting.profile.list中加入如下配置,就可以把wsl加入配置中

1
2
3
4
5
6
{
"guid": "{新获取的guid}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl"
}

2.2. 默认启动路径

在Windows Terminal的Ubuntu配置中加一句

1
"startingDirectory": "//wsl$/Ubuntu/home/<yourname>/"

2.3. Ubuntu替换清华源

  1. 首先需要使用root用户

    1
    sudo -i 
  2. 进行原来源的备份

    1
    cp /etc/apt/sources.list /etc/apt/sources.list.bak 
  3. 清空source.list

    1
    echo '' > /etc/apt/sources.list
  4. 进行源列表的修改
    复制并添加源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    vim /etc/apt/sources.list 
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
  5. 更新列表

    1
    apt-get update

2.4. oh-my-zsh配置

  1. 安装ZSH

    1
    2
    3
    $ sudo apt-get install zsh
    $ zsh --version
    zsh 5.8 (x86_64-ubuntu-linux-gnu)
  2. 安装oh-my-zsh

    1
    sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
  3. 配置主题
    需要编辑.zshrc文件,然后使用source刷新

    1
    2
    vim ~/.zshrc
    source ~/.zshrc

    我喜欢dst主题,修改ZSH_THEME="dst"即可

2.5. git status错误

git在wsl下读取windows中的repo会出现所有文件都modified的错误,是因为linux与windows的文件结尾不同导致的。使用如下命令解决

1
2
git config --global core.autocrlf true
git config --global core.filemode false