WOA02-安装WSL2

本文提供了一套完整的Windows Subsystem for Linux 2 (WSL2)配置教程,包括安装WSL2、启用虚拟机平台、安装Linux发行版、配置国内镜像源、固定MAC地址、安装systemd环境以及搭建GNOME桌面系统。此外,还介绍了在Windows上安装VcXsrv以实现远程桌面连接。
摘要由CSDN通过智能技术生成


WOA02-安装WSL2

WSL 适用于 Linux 的 Windows 子系统可让开发人员按原样运行 GNU/Linux 环境 - 包括大多数命令行工具、实用工具和应用程序 - 且不会产生传统虚拟机或双启动设置开销。
WSL2 是适用于 Linux 的 Windows 子系统体系结构的一个新版本,它支持适用于 Linux 的 Windows 子系统在 Windows 上运行 ELF64 Linux 二进制文件。 它的主要目标是提高文件系统性能,以及添加完全的系统调用兼容性。

什么是适用于 Linux 的 Windows 子系统?

WSL2 需要启用 Windows 10 的虚拟机平台特性

安装脚本 install-wsl2-vmp.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xN" goto :skip_getadmin_privileges
>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
    ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
    ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
    "%TEMP%\Getadmin.vbs"
    DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
    Exit
)
:skip_getadmin_privileges

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
if not %errorlevel% == 0 echo :: Found error!!! && (DELAY 30 2>nul || ping -n 30 127.0.0.1>nul) && EXIT 1

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
if not %errorlevel% == 0 echo :: Found error!!! && (DELAY 30 2>nul || ping -n 30 127.0.0.1>nul) && EXIT 1

wsl --set-default-version 2
wsl -l -v

shutdown /r /t 10

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击install-wsl2-vmp.cmd运行, 显示无误后等待自动重启系统!

安装 WSL2/Linux 发行版

安装脚本 install-wsl2-linux.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xN" goto :skip_getadmin_privileges
>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
    ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
    ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
    "%TEMP%\Getadmin.vbs"
    DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
    Exit
)
:skip_getadmin_privileges

:: 查看 Hyper-V 支持信息
systeminfo | grep  Hyper

:: 查看支持的 Linux 信息
wsl --list --online

wsl --install -d Ubuntu-18.04

DELAY 10 2>nul || ping -n 10 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击install-wsl2-linux.cmd运行, 等待完成安装!

第一次运行 Linux 会创建用户, 为了脚本处理方便用户名和密码都设为 wsl !

配置 WSL/Linux 发行版国内镜像

Ubuntu-18.04 Linux 软件仓库国内镜像源文件 sources.list.1804:


# 
# based on https://www.cnblogs.com/zqifa/p/12910989.html
# 

# 阿里云源

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

# 清华源

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

# 中科大源

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

# 网易163源

deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

安装脚本 fixes-wsl2-mirror-cn.sh:

#!/bin/sh
#
#
#

sudo cp -f /etc/apt/sources.list /etc/apt/sources.list.0

sudo cp -f ./03.sources.list.1804 /etc/apt/sources.list

sudo apt-get update
sudo apt-get upgrade

安装脚本 fixes-wsl2-mirror-cn.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xy" wsl -u wsl -- cd $PRE_NAMESPACE_PWD; ./%~n0.sh

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击fixes-wsl2-mirror-cn.cmd运行, 等待脚本覆盖 WSL/Linux 仓库软件源文件, 并更新软件源!

配置 WSL/Linux 固定 MAC 地址

安装脚本 fixes-wsl2-dynmac.bashrc(可以按需求更改 wantmac 地址):

###--> CVP.BASHRC

wantmac=00:15:5d:7d:e1:2b
mac=$(ip link show bond0 | awk '/ether/ {print $2}')
if [[ $mac !=  $wantmac ]]; then
    sudo ip link set dev bond0 address $wantmac
fi

###<-- CVP.BASHRC

安装脚本 fixes-wsl2-dynmac.sh:

#!/bin/sh
#
#
#


cp -rf ./fixes-wsl2-dynmac.bashrc ~/.fixes-wsl2-dynmac.bashrc

grep -qxF '###--> CVP.BASHRC.fixes-wsl2-dynmac' ~/.bashrc || ( \
    echo -e '\n'; \
    echo '###--> CVP.BASHRC.fixes-wsl2-dynmac'; \
    echo 'if [ -f ~/.fixes-wsl2-dynmac.bashrc ]; then'; \
    echo '    . ~/.fixes-wsl2-dynmac.bashrc'; \
    echo 'fi'; \
    echo '###<-- CVP.BASHRC.fixes-wsl2-dynmac'; \
    echo -e '\n' \
) >> ~/.bashrc


安装脚本 fixes-wsl2-dynmac.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xy" wsl -u wsl -- cd $PRE_NAMESPACE_PWD; ./%~n0.sh

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击fixes-wsl2-dynmac.cmd运行, 等待完成安装!

安装 WSL/Linux systemd 运行环境

安装脚本 install-wsl2-systemd.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xy" wsl -u wsl -- cd $HOME; git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git; $HOME/ubuntu-wsl2-systemd-script/ubuntu-wsl2-systemd-script.sh

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击install-wsl2-systemd.cmd运行, 等待完成安装!

安装 WSL/Linux 发行版桌面系统

安装脚本 install-wsl-gnome.sh:

#!/bin/sh
#
#
#


sudo apt install -y ubuntu-desktop daemonize xrdp gnome-software gnome-tweaks gnome-shell-extensions

# sudo systemctl status xrdp

sudo adduser xrdp ssl-cert
sudo systemctl restart xrdp

sudo cp -rf /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.0
sudo cp -rf ./06-xrdp.ini /etc/xrdp/xrdp.ini

# auto start
# sudo gedit /etc/xrdp/xrdp.ini
# [Xorg]
# name=Xorg
# lib=libxup.so
# username=wsl
# password=wsl
# ip=127.0.0.1
# port=-1
# code=20

# sudo gedit /etc/xrdp/xrdp.ini
sudo /etc/init.d/xrdp start

sudo ufw allow 3390

echo :: Done.

安装脚本 install-wsl-gnome.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if "xy" == "xy" wsl -u wsl -- cd $PRE_NAMESPACE_PWD; ./%~n0.sh

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击install-wsl-gnome.cmd运行, 等待 WSL/Linux 完成安装桌面组件!

安装 VcXsrv Windows X Server 显示服务器

安装脚本 win-vcxsrv-startup.reg:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"vcxsrv"="\"E:\\scoop\\scoop\\apps\\vcxsrv\\current\\vcxsrv.exe\" -multiwindow -displayfd -1 -clipboard -wgl -ac"

安装脚本 install-win-vcxsrv.cmd:


@echo off

:: =====
cd /d "%~dp0"
set "TOPDIR=%cd:\=/%"
title "%~n0"
:: =====

if not exist "%SCOOP%/apps/vcxsrv/current/vcxsrv.exe" scoop install vcxsrv

if "xy" == "xy" set __COMPAT_LAYER=RunAsInvoker  
if "xy" == "xy" REGEDIT.EXE "win-vcxsrv-startup.reg"

DELAY 3 2>nul || ping -n 3 127.0.0.1>nul
EXIT 0

新建保存好上面安装脚本, 直接双击install-win-vcxsrv.cmd运行, 等待 Windows 完成安装vcxsrv!


现在,可以在 WSL/Linux 运行带图形界面的程序了!!!


…TBC…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值