win11 wsl ubuntu24.04

一:开启Hyper-V

在这里插入图片描述
win11家庭版开启hyper-v

  1. 桌面新建 xxx.txt 文档,将下面的代码粘贴到文档中保存;并将 xxx.txt 重命名为 install_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
    
  2. 右键install_hyper_v.cmd,选择“以管理员身份运行”,等待几分钟后,直至命令行界面提示选择“Y/N”,输入 Y 以重启电脑完成安装

二:安装wsl

在这里插入图片描述

三:安装ubuntu24.04

在这里插入图片描述

三:桥接模式,固定IP

  1. 执行Get-NetAdapter,可以列出系统所有的网卡
    在这里插入图片描述

  2. 查看虚拟网卡
    在这里插入图片描述

  3. wsl虚拟网络和主机有线网络桥接起来

    Set-VMSwitch "WSL (Hyper-V firewall)" -NetAdapterName "WLAN"
    # Set-VMSwitch "wsl虚拟网卡" -NetAdapterName "物理网卡"
    
  4. 手动修改WSL网络

    sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
    sudo ip addr add 192.168.6.16/24 broadcast 192.168.6.255 dev eth0
    sudo ip route add 0.0.0.0/0 via 192.168.6.1 dev eth0
    
    #!/bin/bash
    new_ip=192.168.6.16
    brd=192.168.6.255
    gateway=192.168.6.1
    nameserver=114.114.114.114
    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
    sudo sed -i "\$c nameserver $nameserver" /etc/resolv.conf
    
  5. 取消桥接

Set-VMSwitch "WSL (Hyper-V firewall)" -SwitchType Internal
  1. 重启系统失效

  2. 开启桥接脚本
    wsl-bridge-on.ps1:

    echo "Change WSL (Hyper-V firewall) Bridge"
    wsl --cd ~ -e ls
    Get-NetAdapter
    Set-VMSwitch "WSL (Hyper-V firewall)" -NetAdapterName "Ethernet"
    wsl --cd ~ -e sh -c ./setIP.sh
    pause
    

    setIP.sh

    #!/bin/bash
    sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
    sudo ip addr add 192.168.1.152/24 broadcast 192.168.1.255 dev eth0
    sudo ip route add 0.0.0.0/0 via 192.168.1.1 dev eth0
    
  3. 关闭桥接脚本
    wsl-bridge-off.ps1:

    echo "WSL (Hyper-V firewall) Bridge OFF"
    Set-VMSwitch "WSL (Hyper-V firewall)"  -SwitchType Internal
    wsl --shutdown
    wsl --cd ~ -e ls
    pause
    

四:U盘使用

  1. windows 系统查看u盘盘符
    在这里插入图片描述

  2. wsl 挂载

sudo mount -t drvfs F: /mnt/g
  1. 卸载U盘
sudo umount /mnt/g

五:wsl 从c盘迁移到其它盘

  1. 查看虚拟机名称和状态
wsl -l -v
  1. 停止虚拟机
wsl --shutdown
  1. 导出/备份
wsl --export Ubuntu-22.04 D:\Ubuntu_WSL\Ubuntu.tar
wsl --unregister Ubuntu-22.04
wsl --import Ubuntu-22.04 D:\Ubuntu_WSL D:\Ubuntu_WSL\Ubuntu.tar
  1. 恢复默认用户
Ubuntu2204 config --default-user sws

六:wsl 将分布版设置为默认值

wsl --set-default Ubuntu-24.04

参考资料

Win11 WSL2桥接网络实现局域网连接与Ubuntu18.04/20.04VNC桌面搭建以及Linux命令行外网代理实现
WSL系列内容:wsl2 通过桥接网络实现被外部局域网主机直接访问(更新一键执行powershell脚本)
适用于 Linux 的 Windows 子系统文档

### 如何在WSL上安装Ubuntu 24.04 #### 准备工作 确保Windows Subsystem for Linux (WSL) 已经被启用并更新到了最新版本。可以通过PowerShell以管理员身份运行`wsl --update`来完成此操作。 #### 安装过程 通过命令行可以快速部署Ubuntu 24.04WSL之上: ```bash wsl --install -d Ubuntu-24.04 ``` 这条指令将会自动下载并设置好环境,通常情况下会默认安装到C盘[^2]。 如果希望自定义安装路径,则需按照以下流程操作: 1. **导出已有的Ubuntu发行版** 首先需要有一个已经存在的Ubuntu实例用于导出: ```bash wsl --export Ubuntu-24.04 D:\tmp\Ubuntu-24.04.tar ``` 2. **移除旧的Ubuntu实例** 使用下面的命令删除现有的Ubuntu分发版: ```bash wsl --unregister Ubuntu-24.04 ``` 3. **重新导入Ubuntu映像文件** 将之前保存下来的`.tar`文件作为新的Ubuntu实例导入特定位置: ```bash wsl --import Ubuntu-24.04 "F:\VirtualPCs\Ubuntu-24.04" "D:\tmp\Ubuntu-24.04.tar" ``` 以上步骤允许用户灵活调整存储位置以及管理多个不同版本的操作系统副本。 对于那些想要更精确控制整个安装过程的人而言,还可以考虑从官方渠道获取最新的ISO镜像,并遵循标准服务器安装程序来进行配置,这包括但不限于网络参数设定、磁盘分区规划等高级选项[^4]。 最后,在成功启动新安装好的Ubuntu之后,建议立即执行软件包更新和必要的安全补丁应用,以保障系统的稳定性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值