WSL2构建开发环境


使用原因

因为不希望在Windows下配置工具链,所以选择了Linux系统进行开发

而在大一的上学期,分别使用了Ubuntu18.04LTS(Desktop)和Deepin15作为开发环境,体验相当不爽

由于Ubuntu和Deepin的耗电量较高(那时候还不知道tlp这个东东…),同时Deepin15的CPU调度有问题,经常出现一核有难,多核围观的蛋疼场景

因此决定弃用Linux的图形界面

在后来了解到了WSL这个新技术,决定使用WSL作为开发环境

由于WSL中无需使用GUI,使得Ubuntu的资源消耗减少且变得相对稳定(至少在目前的使用下没有翻车)

作为Windows系统的一部分,耗电受Windows管理,体验比直接使用Linux要好很多

配置WSL2

WSL与WSL2

WSL简介

WSL2相比WSL最大的优势在于速度的提高和一个完整的Linux内核

但是启用了WSL2后(使用了类似于Hyper-V的技术),VMware正式版将无法使用(测试版可以调用Hyper-V,类似于虚拟机套娃,但性能就emmm)

而WSL可以与VMware共存,这是两者的差别,也是WSL的优势

同时WSL2的网络部分与WSL不同,在一些设置上需要特别注意

启用WSL2

在 Windows 10 上安装适用于 Linux 的 Windows 子系统 (WSL)

以管理员身份运行powershell,启用WSL

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

同样以管理员身份运行powershell,将WSL升级到WSL2

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

然后重启电脑

重启完成后,下载WSL2的Linux内核更新包并安装更新

更新 WSL 2 Linux 内核

至此,WSL2已经启用

  • WSL的常用操作
关闭所有wsl发行版
wsl --shutdown
关闭某个wsl发行版
wsl -t <DistroName>
注销(即卸载)某个发行版
wsl --unregister <DistroName>
设置默认wsl版本
wsl --set-default-version <版本 1或者2>

在WSL2上安装系统

首先,前往Windows store下载Windows terminal

购买 Windows Terminal - Microsoft Store zh-CN

打开Windows terminal,在里面输入 wsl --set-default-version 2 ,将WSL2设置为默认发行版

1.通过Windows store下载Ubuntu18.04LTS/Debian10(默认安装在系统盘)

购买 Ubuntu 18.04 LTS - Microsoft Store zh-CN

购买 Debian - Microsoft Store zh-CN

下载完成,点击启动后,等待一到两分钟来完成初始化,然后输入用户名和密码(如果在等待一到两分钟后仍然处在初始化的状态,点击空格进入到输入用户名和密码的步骤,记得删除用户名前的空格)

进入到Ubuntu后,可以关闭启动产生的命令行窗口,使用Windows terminal,输入 wsl 即可进入Ubuntu/Debian

2.通过微软官方下载,可安装在其他位置

Ubuntu18.04

Debian10

将下载得到的文件的后缀修改为 .zip ,解压到安装位置,双击exe文件即可运行安装程序

等待一到两分钟来完成初始化,然后输入用户名和密码(如果在等待一到两分钟后仍然处在初始化的状态,点击空格进入到输入用户名和密码的步骤,记得删除用户名前的空格)

进入到Ubuntu后,可以关闭启动产生的命令行窗口,使用Windows terminal,输入 wsl 即可进入Ubuntu/Debian

Terminal美化(可选)

Fira Code Retina 字体下载链接

Windows Terminal Color Schemes

这是我的配置文件(需要将{xxxx}修改为你电脑生成的值)

// This file was initially generated by Windows Terminal 1.0.1401.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "alwaysShowTabs": true,
    "initialCols": 90,
    "initialRows": 30,
    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings
    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,
    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,
    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    //   and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles":
    {
        "defaults":
        {
            "useAcrylic": true,
            "acrylicOpacity": 0.5,
            "fontFace": "Fira Code Retina",
            "fontSize": 14,
            "cursorColor": "#FFFFFF"
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false,
                "colorScheme": "One Half Dark"
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
                "hidden": true
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            //{//看个人需要是否使用Debian
            //    "guid": "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
            //    "hidden": false,
            //    "name": "Debian",
            //    "source": "Windows.Terminal.Wsl",
            //    "colorScheme": "One Half Dark"
            //},
            {
                "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
                "hidden": false,
                "name": "Ubuntu-18.04",
                "source": "Windows.Terminal.Wsl",
                "colorScheme": "One Half Dark"
            },
            {
                "guid": "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
                "hidden": false,
                "name": "Debian",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    },
    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
    ],
    // Add custom keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
    "keybindings":
    [
        // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
        // These two lines additionally bind them to Ctrl+C and Ctrl+V.
        // To learn more about selection, visit https://aka.ms/terminal-selection
        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },
        // Press Ctrl+Shift+F to open the search box
        { "command": "find", "keys": "ctrl+shift+f" },
        // Press Alt+Shift+D to open a new pane.
        // - "split": "auto" makes this pane open in the direction that provides the most surface area.
        // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
        // To learn more about panes, visit https://aka.ms/terminal-panes
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
}

Linux配置

换源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

sudo rm /etc/apt/sources.list

sudo nano /etc/apt/sources.list

sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y

  • Ubuntu18.04阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  • Debian9阿里源
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib

deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main

deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib

deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
  • Debian10阿里源
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib

deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main

deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib

deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
  • 将Debian9升级为Debian10
Debian9下切换到阿里源
sudo apt-get update
sudo apt-get upgrade
将源切换为Debian10的阿里源
sudo apt-get update
sudo apt-get upgrade

sudo apt-get dist-upgrade

sudo apt-get autoremove

cat /etc/debian_version
输出10.4,升级完成

系统代理(clash)

  • wsl1
在 .bashrc 或者 .zshrc 中添加如下内容

export https_proxy="http://127.0.0.1:7890";
export http_proxy="http://127.0.0.1:7890";
export all_proxy="socks5://127.0.0.1:7890";
  • wsl2
在 .bashrc 或者 .zshrc 中添加如下内容

export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*');
export https_proxy="http://${hostip}:7890";
export http_proxy="http://${hostip}:7890";
export all_proxy="socks5://${hostip}:7890";

完成后,输入 source .bashrc 或者 source .zshrc

  • apt代理

sudo apt -o Acquire::http::proxy="http://172.27.176.1(此处ip为wsl的ip):7890/" install *********

ZSH&开发环境

  • 安装zsh和基本开发软件

sudo apt install zsh gcc g++ gdb python python3 python-pip python3-pip

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

编辑~/.zshrc
将 ZSH_THEME 的值改为 agnoster 或保持默认(个人喜好

隐藏用户名
DEFAULT_USER=$USER

安装 syntax-highlighting & zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting && git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && chmod -R 555 ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting && chmod -R 555 ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions

编辑~/.zshrc

plugins=(git z colored-man-pages zsh-autosuggestions zsh-syntax-highlighting)

在切换shell后, .bashrc 中的代理设置要复制到 .zshrc

使zsh配置生效
source ~/.zshrc

  • 配置pip清华源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

或者在 ~/.pip/pip.conf 中添加(建议使用这种方式)

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

clion&vsc链接wsl

clion官方文档

wget https://raw.githubusercontent.com/JetBrains/clion-wsl/master/ubuntu_setup_env.sh && bash ubuntu_setup_env.sh

在clion中配置wsl的链接

Developing in the Windows Subsystem for Linux with Visual Studio Code

在vscode中安装Remote-WSL插件

Remote - WSL - Visual Studio Marketplace

在你要进行开发的目录下,如 ~/python_code ,输入 code . ,即可进行开发

桌面环境

  • 安装桌面环境(Ubuntu18.04)
  1. 使用windows的xrdp远程连接(较稳定)

sudo apt install xorg xrdp xfce4 xfce4-session xfce4-terminal

/etc/xrdp/xrdp.ini中将端口修改为3390 (windows远程桌面地址 localhost:3390)
~/.xsession 写入xfce4-session

启动 sudo service xrdp start

  1. 使用xming(不稳定)

Xming X Server for Windows

sudo apt install xfce4
  • wsl配置

修改 ~/.bashrc 或 ~/.zshrc
在最后一行添加
export DISPLAY=:0
使配置生效
source ~/.bashrc 或者 source ~/.zshrc

测试端口号
echo $DISPLAY
输出应该为:0

  • wsl2配置

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0
使配置生效
source ~/.bashrc 或者 source ~/.zshrc

启动XLaunch

选择one windows

XLaunch的Display number设置为0
选择 start no clinet

wsl2额外勾选 Disable access control

额外参数(用于设置xming窗口大小) additional parameters for xming

-screen 0 1280x960

启动xfce4

startxfce4

在调用GUI时可能会出现报错,如:
failed to commit changes to dconf: Failed to execute child process “dbus-launch” (No such file or directory)
解决方法
sudo apt-get install x11-xserver-utils dconf-editor dbus-x11 -y


文章作者: Misaka
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Misaka !
 上一篇
Hyper-V构建CTF环境 Hyper-V构建CTF环境
使用原因在开启WSL2后,VMware的正式版不能正常使用,所以我一开始尝试了VMware的测试版(现在的15.5版本已经可以支持了) VMware的博客和对测试版的介绍 VMware的博客和对15.5版的介绍 总的来说,在使用测试版时没有
2020-07-26 Misaka
下一篇 
test test
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hex
2020-07-25 Misaka
  目录