头图

WSL2 installation and configuration

满眼星辰
中文

title: WSL2 installation and configuration
toc_level: 3
date: 2021-07-23 17:43:01
tags: [WSL, Linux, Config]
categories: [Uncategorized]

version: v1

image.png

Install WSL2

See Microsoft documentation, switch between Chinese and English in the upper right corner: install WSL2

WSL2 basic operation

# 运行默认分发版
wsl 

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

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

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

# 卸载分发版
wsl --unregister <distribution>

# 停止某个分发版
wsl -t <distribution>

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

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

Install ubuntu

Search ubuntu in the application store and install Ubuntu-20.04 LTS. According to the basic operation of WSL2 in the previous step, enter the distribution, and then execute the following commands in sequence.

Change software source

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

Install zsh

zsh and theme packages

#安装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

Plug-in

# 进入插件路径
$ 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

Set up proxy

The proxy software must be set to allow the virtual machine to access the LAN. For example, Clash must turn on the Allow LAN button.

# 编辑代理脚本
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

Effect

You can see the code highlighting and automatic completion of zsh.

image.png

阅读 2.8k

计算机硕士研究生。

67 声望
7 粉丝
0 条评论

计算机硕士研究生。

67 声望
7 粉丝
文章目录
宣传栏