【Linux 运维系列】Windows 系统下开启 Ubuntu 子系统

【Linux 运维系列】Windows 系统下开启 Ubuntu 子系统

【1】Windows 系统中安装 Ubuntu 子系统

【1.1】开启 Windows 开发者模式

设置 -> 安全和更新 -> 开发者选项

【1.2】启用 Windows 下 Linux 子系统功能

控制面板 -> 程序与功能 -> 启用或关闭 Windows 功能,选择“适用于 Linux 的 Windows 子系统”并重启;

 【1.3】WSL 配置 Ubuntu 操作系统

【1.3.1】安装 Ubuntu 操作系统

Microsoft Store 中搜索 Ubuntu 安装即可;

【1.3.2】卸载 WSL 中的 Ubuntu 操作系统

wsl --unregister Ubuntu-20.04

  【2】Windows 与 Ubuntu 子系统互传文件

运行命令 df -h

从图中可见,Windows 系统中的盘符已经挂载到了 Ubuntu 子系统中;

【3】WSL1 升级到 WSL2

  • 查看当前 WSL 版本号
wsl -l -v

从图示可见,Ubuntu-18.04 的 WSL 版本为 1,Ubuntu20.04LTS 的 WSL 版本为 2;

  • 运行 WSL 2 对系统的要求

  • 开启虚拟机功能
用管理员身份打开 PowerShell 并运行如下命令

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • 确定 Windows 开启了如下功能

  •  确定 CPU 是否开启了虚拟化

  • 下载安装 Linux 内核更新包

X64 : https://link.zhihu.com/?target=https%3A//wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

ARM64 : https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi

  • 指定发行版升级到 WSL2
wsl --set-version 发行版名称 版本号

示例 : 
将 Ubuntu18.04 设置为 WSL2 的命令为 wsl --set-version Ubuntu-18.04 2

【4】WSL 开启 ssh 远程登录功能

  • 安装并启动 ssh 服务端
sudo apt install openssh-server

sudo /etc/init.d/ssh restart
  • 修改配置信息
配置文件
/etc/ssh/sshd_config

Port 22                     #默认即可,如果有端口占用可以自己修改
PasswordAuthentication yes  #允许用户名密码方式登录
PermitRootLogin yes         #允许 root 用户登录
  • 常见错误与解决方案
  • sshd: no hostkeys available -- exiting
在 /etc/ssh 路径下执行 ssh-keygen -A 即可解决

【5】VSCode 连接 WSL 调试

安装按键 Remote Development 以及 Remote-WSL,建立连接

【6】WSL2 中的 Ubuntu20 安装并配置桌面环境

【6.1】WSL2 中的 Ubuntu20 安装桌面环境

1. 安装 gnome 桌面环境


# 安装gnome桌面环境
sudo apt-get install ubuntu-desktop
# 安装相关工具
sudo apt-get install gnome-tweak-tool

--------------------------------------------------

2. 安装 systemctl

git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script/
bash ubuntu-wsl2-systemd-script.sh

--------------------------------------------------

3. Windows 主机 (PowerShell) 重启 WSL 服务

# 停止服务
net stop LxssManager
# 启动服务
net start LxssManager

--------------------------------------------------

4. 安装远程控制软件 xrdp

# 安装xrdp远程控制服务
sudo apt-get install xrdp

将端口从3389改为3390,因为此前默认的3389端口已保留用于 ubuntu shell
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini

配置启动 session,否则远程桌面登录输入密码之后会直接闪退
echo "gnome-session" > ~/.xsession

重新启动 xrdp 服务
sudo systemctl restart xrdp

【6.2】Windows 启动桌面版 Ubuntu

按住 Windows + R 在运行框中输入 mstsc 后按回车,远程桌面登录 Ubuntu;

【7】WSL2非代理桥接实现与局域网其他主机互通

可以按照如下 3 个脚本,将 WSL 的网卡桥接到宿主机,从而实现局域网内的互联互通;

【7.1】WSL2 中配置 IP 地址脚本

#!/bin/bash
new_ip=WSL中需要设置的IP
brd=WSL中桥接地址,广播地址
gateway=WSL中网关地址

net_dev=eth0
echo "password" | sudo -S ip addr del $(ip addr show $net_dev | grep 'inet\b' | awk '{print $2}' | head -n 1) dev $net_dev
sudo ip addr add $new_ip/24 broadcast $brd dev $net_dev
sudo ip route add 0.0.0.0/0 via $gateway dev $net_dev

【7.2】Windows 宿主机中桥接 WSL 网卡脚本

# 检查并以管理员身份运行PS并带上参数
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
    $boundPara = ($MyInvocation.BoundParameters.Keys | foreach{'-{0} {1}' -f  $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
    $currentFile = $MyInvocation.MyCommand.Definition
    $fullPara = $boundPara + ' ' + $args -join ' '
    Start-Process "$psHome\pwsh.exe"   -ArgumentList "$currentFile $fullPara"   -verb runas
    return
}
#首先随意执行一条wsl指令,确保wsl启动,这样后续步骤才会出现WSL网络
echo "正在检测wsl运行状态..."
wsl --cd ~ -e ls
echo "正在获取网卡信息..."
Get-NetAdapter
echo "`n正在将WSL网络桥接到以太网..."
Set-VMSwitch WSL -NetAdapterName 以太网
echo "`n正在修改WSL网络配置..."
wsl --cd ~ -e sh -c ./set_eth0.sh
echo "`ndone"
pause

【7.3】Windows 宿主机中解除

# 检查并以管理员身份运行PS并带上参数
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
    $boundPara = ($MyInvocation.BoundParameters.Keys | foreach{'-{0} {1}' -f  $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
    $currentFile = $MyInvocation.MyCommand.Definition
    $fullPara = $boundPara + ' ' + $args -join ' '
    Start-Process "$psHome\pwsh.exe"   -ArgumentList "$currentFile $fullPara"   -verb runas
    return
}
#首先随意执行一条wsl指令,确保wsl启动,这样后续步骤才会出现WSL网络
echo "正在检测wsl运行状态..."
wsl --cd ~ -e ls
echo "正在获取网卡信息..."
Get-NetAdapter
echo "`n正在将WSL网络桥接到以太网..."
Set-VMSwitch WSL -NetAdapterName 以太网
echo "`n正在修改WSL网络配置..."
wsl --cd ~ -e sh -c ./set_eth0.sh
echo "`ndone"
pause

【7.4】问题与解决方案

1. PowerShell 运行 ps1 脚本时会报 "因为在此系统上禁止运行脚本" 错误

请用管理员身份打开 powershell
输入命令 get-ExecutionPolicy,显示 Restricted,表示状态被禁止;
解决方案,输入命令 set-ExecutionPolicy RemoteSigned,回车,执行策略更改

【8】配置DNS服务

sudo vim /etc/resolv.conf
nameserver 8.8.8.8

【附录 A-1】Win10 家庭版安装 Hyper-V

添加如下代码并保存为文件 Hyper-V.cmd,以管理员身份运行;

pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL

【附录 A-2】BIOS 中开启虚拟机

【附录 B-1】常用的 WSL 命令总结

wsl 启动特定的发行版本
wsl -d <发行名称>

设置默认发行版本
wsl --set-default <发行名称>

参考与致谢

本博客为博主学习笔记,同时参考了网上众博主的博文以及相关专业书籍,在此表示感谢,本文若存在不足之处,请批评指正。

【1】Windows下安装Ubuntu子系统

【2】Windows10安装Ubuntu桌面子系统

【3】Windows——windows10下如何和子系统Ubuntu18.04互传文件

【4】WSL1升级为WSL2

【5】Win10家庭中文版安装Hyper-V

【6】在BIOS中打开虚拟机

【7】wsl使用ssh连接

【8】使用ssh连接WSL

【9】使用VSCode进行WSL2的本机调试

【10】不知道这些WSL命令,怎么能够在win10上顺利运行Linux系统

【11】win10应用商店安装Ubuntu及图形化配置(附VcXsrv安装包)

【12】WSL2运行图像应用或图形界面

【13】超详细Windows10/Windows11 子系统(WSL2)安装Ubuntu20.04(带桌面环境)

【14】WSL系列内容:wsl2 通过桥接网络实现被外部局域网主机直接访问(更新一键执行powershell脚本)

【15】2021/10/28 解决报错:"因为在此系统上禁止运行脚本" 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值