Windows Subsystem for Linux 配置记录

224 阅读2分钟

写在前面

Windows Subsystem for Linux(简称 WSL)顾名思义是在 Windows 上运行的 Linux 子系统。搭配 Windows Terminal,开发者可以获得直接在 Windows 上运行 GNU/Linux 环境的极佳体验。

安装 WSL

# 管理员模式打开 PowerShell
wsl --install
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2

安装Ubuntu 20.04.4

应用商店: Ubuntu 20.04.4 LTS

包管理换源

进入 WSL 后使用清华大学开源软件镜像站加速 APT,更新软件源配置文件 /etc/apt/sources.list

参考: 清华大学镜像源

# Back up source list
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# Edit source list
sudo vi /etc/apt/sources.list

修改镜像源

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
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

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

使更新生效

# update system
sudo apt update
sudo apt upgrade -y

安装 Zsh

# Install zsh
sudo apt install -y zsh

# Set zsh as default shell
chsh -s /bin/zsh

新开一个 Shell Session,此时会得到提示

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses ---

输入 0 或 2 均可,安装 Oh My Zsh 后配置文件 .zshrc 会被覆盖。

安装 Oh My Zsh

sudo apt install -y curl git vim

# Install oh my zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Install plugin
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

配置主题与插件

# 编辑配置文件 .zshrc
vi .zshrc
# Set up themes
ZSH_THEME="ys"

# Set up plugins
plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

配置代理和高亮

# 编辑配置文件 .zshrc
vi .zshrc
# Set proxy shell function
proxy() {
    if [[ $@ == 'enable' ]]; then
# Get host ip
        export HOST_IP=$(ip route | grep default | awk '{print $3}')
        export ALL_PROXY="socks5://$HOST_IP:1080";
        export all_proxy="socks5://$HOST_IP:1080";
        export http_proxy="http://$HOST_IP:1080";
        export https_proxy="http://$HOST_IP:1080";
    elif [[ $@ == 'disable' ]]; then
        unset ALL_PROXY;
        unset all_proxy;
        unset http_proxy;
        unset https_proxy;
    else
        echo 'ALL_PROXY   =' ${ALL_PROXY:-'none'};
        echo 'all_proxy   =' ${all_proxy:-'none'};
        echo 'http_proxy  =' ${http_proxy:-'none'};
        echo 'https_proxy =' ${https_proxy:-'none'};
    fi
}

# Enable proxy
proxy enable

# Change ls colors
export LS_COLORS='ow=34;4'

使配置生效

source ~/.zshrc