WSL2 安装及配置


title: WSL2 安装及配置
toc_level: 3
date: 2021-07-23 17:43:01
tags: [WSL, Linux, Config]
categories: [Uncategorized]

version: v1

WSL2 安装及配置_第1张图片

安装 WSL2

见 Microsoft 文档,右上角切换中英文: 安装 WSL2

WSL2 基本操作

# 运行默认分发版
wsl 

# 以指定用户运行执行分发版 
wsl -u  -d 
# example
wsl -u xuwhao -d my-distribution   

# 导出分发版(可用于备份,更改分发版安装位置等)
wsl --export  
# example 
wsl --export my-distribution D:\bak\my-distribution.tar

# 导入分发版(可导入上一步导出的 tar 包,实现备份恢复,更改安装位置等功能)
wsl --import    --version 
# example , version 不指定默认为你设置的默认 wsl 版本
wsl --import new-ubuntu D:\wsl\ubuntu D:\bak\my-distribution.tar --version 2

# 卸载分发版
wsl --unregister 

# 停止某个分发版
wsl -t 

# 立即终止所有正在运行的分发和 WSL 2 轻型工具虚拟机
wsl --shutdown

# 显示有关所有分发的详细信息
wsl -l -v

安装 Ubuntu

应用商店搜索 ubuntu, 安装 Ubuntu-20.04 LTS。根据上一步的 WSL2 基本操作,进入该分发版,然后依次执行以下命令。

更换软件源

nano /etc/apt/sources.list

# 追加以下内容

## Tsinghua
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

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

# 保存退出后执行

sudo apt-get update
sudo apt-get upgrade

安装 zsh

zsh 及主题包

#安装zsh软件包
$ sudo apt-get install -y zsh

# 安装ohmyzsh (如果 443 error, 先去下面设置下代理或者配置hosts)
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 设置为默认shell
$ chsh -s /usr/bin/zsh

# 安装字体库
$ sudo apt-get install fonts-powerline

# 安装 powerlevel10k 主题包
$ sudo git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# 修改配置文件,更改主题
$ sudo vi ~/.zshrc

# 修改对应行
ZSH_THEME="powerlevel10k/powerlevel10k"

$ source ~/.zshrc

插件

# 进入插件路径
$ cd ~/.oh-my-zsh/plugins

# 下载自动补齐、高亮、建议插件
$ wget http://mimosa-pudica.net/src/incr-0.2.zsh
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
$ git clone https://github.com/zsh-users/zsh-autosuggestions.git
$ source incr*.zsh

# 修改配置文件
$ vim ~/.zshrc
plugins=(git
zsh-syntax-highlighting
zsh-autosuggestions)

$ source zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

设置代理

代理软件一定要设置允许虚拟机访问 LAN,如 Clash 要打开 Allow LAN 按钮。

# 编辑代理脚本
vim ~/proxy.sh

# 粘贴下列代码 (port 改成自己代理软件的port, 我用的 clash for windows)
#!/bin/sh
hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
wslip=$(hostname -I | awk '{print $1}')
port=7890

PROXY_HTTP="http://${hostip}:${port}"

set_proxy(){
    export http_proxy="${PROXY_HTTP}"
    export HTTP_PROXY="${PROXY_HTTP}"

    export https_proxy="${PROXY_HTTP}"
    export HTTPS_proxy="${PROXY_HTTP}"

    git config --global http.proxy "${PROXY_HTTP}"
    git config --global https.proxy "${PROXY_HTTP}"
    
    echo "success"
}

unset_proxy(){
    unset http_proxy
    unset HTTP_PROXY

    unset https_proxy
    unset HTTPS_PROXY

    git config --global --unset http.proxy
    git config --global --unset https.proxy

    echo "success"   
}

test_setting(){
    echo "Host ip:" ${hostip}
    echo "WSL ip:" ${wslip}
    echo "Current proxy:" $https_proxy
}

if [ "$1" = "set" ]
then
    set_proxy

elif [ "$1" = "unset" ]
then
    unset_proxy

elif [ "$1" = "test" ]
then
    test_setting
else
    echo "Unsupported arguments."
fi

Example

# 设置别名
vim ~/.zshrc
alias proxy="source ~/proxy.sh"

# 启动代理
proxy set

# 关闭代理
proxy unset

# 测试代理情况
proxy test

效果

可以看到 zsh 的代码高亮及自动补全等。

WSL2 安装及配置_第2张图片

你可能感兴趣的:(WSL2 安装及配置)