WSL2尝鲜

222 阅读3分钟

最近有个需求:给Seata提了个PR,将Server端改造成springboot工程,开发完成了,也已经在Window上验证通过了,但是不知道能不能在Linux上正常运行。

好早之前就在在网上看到过有关于WSL的文章,也一直想试试,这个周末刚好就尝尝鲜了。这里把过程记录下来,以防下次重装系统又要浪费时间去找各种资料

安装

1、启动或关闭windows功能,开启以下两项

2、开启虚拟化技术 我的电脑是 AMD+微星主板: F11进入BIOS -> 将SVM mod设置为开启

3、在应用商店装一个Linux系统

我开始装的Ubantu 20.04 LTS,但在配置软件包源时各种问题,搞不定,我就退回到Ubantu 18.04 LTS

安装好之后,第一次打开Ubantu会让我们设置用户名和对应的密码,之后就可以正常使用了

4、启用 WSL2

wsl --set-default-version 2

5、总结

  1. windows进入子系统: 任意终端输入wsl即可进入
  2. windows的C盘,在子系统中对应的目录为/mnt/c,其他的以此类推
  3. 有关于卸载,我感觉没网上说的那么麻烦啊:开始菜单中,找到对应的Ubantu,然后右键单击选择卸载就可以了。此时再通过wsl -l -v查看,发现已经没有Ubantu
  4. 重启系统: 我开始在Ubantu中输入reboot,好像不行,哈哈。然后网上找一下:net stop LxssManagernet start LxssManager 即可 5、特别方便:比如我在IDEA终端中直接输入wsl,就切换到Ubantu中了,并且还是在当前目录

配置

  1. Ubantu包管理器配置
  2. zsh配置
  3. Windows Terminal美化
  4. JDK版本管理器

有这些对我来说基本够用了,要用的时候再配置补充把

包管理器

1、更新镜像源:这个我试了好几个,网上找的,开始是用阿里的,有点问题,后面换成中科大的,对我很好使

1. sudo cp /etc/apt/source.list /etc/apt/source.list.bak
2. sudo vi /etc/apt/source.list
3. 把该文件内的内容全情空,然后添加以下内容

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

2、检查可用更新

sudo apt update

3、更新以安装包版本

sudo apt upgrade

zsh

1、安装zsh

sudo apt install zsh

2、设置zsh为默认shell

sudo chsh -s /bin/zsh

3、查看当前使用的shell

echo $SHELL

4、安装oh-my-zsh

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

5、修改oh-my-zsh主题

vim ~/.zshrc
ZSH_THEME="fletcherm"

6、安装oh-my-zsh插件

// 自动补全插件
1. git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

// 代码高亮插件
2. git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

// 然后编辑 ~/.zshrc 文件,在插件列表加上以下内容
3. vim ~/.zshrc

plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)

7、配置别名

alias update='sudo apt update'
alias upgrade='sudo apt upgrade'
alias install='sudo apt install'
alias aremove='sudo apt autoremove'
alias remove='sudo apt remove'
alias aclean='sudo apt autoclean'
alias clean='sudo apt clean'

Windows Terminal

{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    "initialRows": 35,
    "initialCols": 150,
    // "alwaysShowTabs": false,
    // "showTerminalTitleInTitlebar": false,
    // "showTabsInTitlebar": false,
    // "requestedTheme": "system",

    // 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
    "theme": "light",
    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "PowerShell",
                "commandline": "powershell.exe",
                "colorScheme": "Frost",
                "acrylicOpacity": 0.6,
                "cursorColor" : "#000000",
                "fontFace": "Consolas",
                "useAcrylic": true,
                "hidden": false
            },
            {
                "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl",
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "Command Prompt",
                "commandline": "cmd.exe",
                "hidden": true
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure",
                "hidden": true
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
        {
            "name": "3024 Day",
            "black": "#090300",
            "red": "#db2d20",
            "green": "#01a252",
            "yellow": "#fded02",
            "blue": "#01a0e4",
            "purple": "#a16a94",
            "cyan": "#b5e4f4",
            "white": "#a5a2a2",
            "brightBlack": "#5c5855",
            "brightRed": "#e8bbd0",
            "brightGreen": "#3a3432",
            "brightYellow": "#4a4543",
            "brightBlue": "#807d7c",
            "brightPurple": "#d6d5d4",
            "brightCyan": "#cdab53",
            "brightWhite": "#f7f7f7",
            "background": "#f7f7f7",
            "foreground": "#4a4543"
        },
        {
            "name": "3024 Night",
            "black": "#090300",
            "red": "#db2d20",
            "green": "#01a252",
            "yellow": "#fded02",
            "blue": "#01a0e4",
            "purple": "#a16a94",
            "cyan": "#b5e4f4",
            "white": "#a5a2a2",
            "brightBlack": "#5c5855",
            "brightRed": "#e8bbd0",
            "brightGreen": "#3a3432",
            "brightYellow": "#4a4543",
            "brightBlue": "#807d7c",
            "brightPurple": "#d6d5d4",
            "brightCyan": "#cdab53",
            "brightWhite": "#f7f7f7",
            "background": "#090300",
            "foreground": "#a5a2a2"
        },
        {
            "name": "Frost",
            "background":"#FFFFFF",
            "black":"#3C5712",
            "blue":"#17b2ff",
            "brightBlack": "#749B36",
            "brightBlue": "#27B2F6",
            "brightCyan": "#13A8C0",
            "brightGreen": "#89AF50",
            "brightPurple": "#F2A20A",
            "brightRed": "#F49B36",
            "brightWhite": "#741274",
            "brightYellow": "#991070",
            "cyan": "#3C96A6",
            "foreground": "#000000",
            "green": "#6AAE08",
            "purple": "#991070",
            "red": "#8D0C0C",
            "white": "#6E386E",
            "yellow": "#991070"
        }
    ],

    "actions":
    [
        // 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" }
    ]
}

JDK版本管理器

1、安装

1. curl -s "https://get.sdkman.io" | zsh
2. source "$HOME/.sdkman/bin/sdkman-init.sh"

2、常用命令

sdk version
sdk list java : 查看可下载的jdk版本
sdk install java ${Identifier} : sdk install java 11.0.9-amzn
sdk use java ${Identifier} : sdk use java 11.0.9-amzn

常用软件

1. sudo apt install zip
2. sudo apt install unzip