7

WSL2 安装及配置

 3 years ago
source link: https://segmentfault.com/a/1190000040397592
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

version: v1

image.png

安装 WSL2

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

WSL2 基本操作

# 运行默认分发版
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

安装 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 的代码高亮及自动补全等。

image.png


Recommend

  • 14
    • zhuanlan.zhihu.com 4 years ago
    • Cache

    面向开发者的 WSL2 安装指南

    面向开发者的 WSL2 安装指南simpleapples专栏/公众号:Python私房菜为什么要使用 W...

  • 13

    安装WSL1、打开WINDOWS功能,勾选子系统选项2、打开商店搜索WSL,安装ubuntu我这里的系统版本是:18.04如何查看ubuntu系统版本sudo lsb_release -a3、打开ubuntu,设置账号密码安装.net core SDK

  • 12
    • www.cnblogs.com 4 years ago
    • Cache

    WSL2+Terminal+VScode配置调试

    最近几天一直想找个方法把VMware虚拟机和远程连接工具MobaXterm这一组配合替换掉,因为每次开启虚拟机操作Ubuntu都需要占用很大的内存,而且要等好久好久才能开启!!!后面还要使用MobaXterm或者Xshell在远程连接进行操作总觉得好麻烦,这两天发...

  • 16
    • www.80shihua.com 4 years ago
    • Cache

    wsl2 配置zsh

    wsl2配置zsh步骤 sudo apt update sudo apt upgrade 检查zsh是否安装 cat /etc/shells 如果没有安装的话 sudo apt install zsh 下载oh-my-zsh 国内...

  • 7
    • www.v2ex.com 3 years ago
    • Cache

    WSL2 作为 FRP 客户端如何配置

    V2EX  ›  Windows WSL2 作为 FRP 客户端如何配置   urhosts · 23 小时 16 分钟前 · 780 次点击...

  • 6
    • mrleidesen.github.io 3 years ago
    • Cache

    WSL2 配置代理

    MrLeiDeSen's Blog WSL2 配置代理2022-02-07 17:33:30 · MrLeiDeSen准备#C...

  • 9
    • novnan.github.io 3 years ago
    • Cache

    WSL2 + Docker + Laradock 配置

    系统环境介绍Win 10 2004WSL2(Ubuntu 20.04)Docker 19.03.13 启用 Linux 子系统打开 控制面板 -> 程序 -> 启用或关闭Windows功能。找到适用于Linux的Windows子系统和...

  • 5

    WSL2 安装、配置 Cuda、pytorch 记录本文地址:blog.lucien.ink/archives/532最近整了张矿卡,为了这碟醋,包了盘饺子。虽然我已经预料到买前深度学习,买...

  • 8
    • www.80shihua.com 2 years ago
    • Cache

    wsl2 配置服务自启动

    wsl2 配置服务自启动 – 梦回故里 梦回故里 梦回故里是一个php程 梦回故里 无产阶级在这个革命中失去的...

  • 5
    • www.80shihua.com 2 years ago
    • Cache

    wsl2配置中文输入法

    wsl2配置中文输入法 – 梦回故里 梦回故里 梦回故里

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK