提示:本文记录如何在Windows 11的Hyper-V环境中为Multipass的Ubuntu虚拟机实例添加外网网卡并设定固定IP
前言
为了让同一局域网的其他主机访问本机用Multipass创建管理的服务器,需要为Multipass创建的Ubuntu实例设置固定IP。
在Windows 11系统中,Multipass默认使用Hyper-V的Default Switch网络,由Multipass创建的实例仅能被宿主机访问。
一、Multipass是什么?
Multipass 是Ubuntu系统开发商提供的一个在不同软件系统上快速部署Ubuntu虚拟机的工具包,目前支持在Windows\MacOS\Linux系统上安装管理Ubuntu虚拟机。
本文在Windows 11的Hyper-V管理器中为Hyper-V添加外部交换机设备,并在Ubuntu虚拟机中添加新网卡,设置固定IP,让与宿主系统Windows 11处于同一局域网的其他主机访问本机的Ubuntu虚拟机。
二、设置步骤
1.在Hyper-V中添加外部交换机设备
在Hyper-V管理器中选择“虚拟交换机管理器”,如下(示例):
添加"EXT-SWITCH",如下:
为Ubuntu实例添加新网卡,如下:
2.获取宿主机Windows 11的网络IP
在控制台输入以下命令:
PS C:\Users\MyPC> ipconfig
示例主机显示输出如下:
记录下IP地址及网段:
IPv4 地址:192.168.3.18
子网掩码: 255.255.255.0
默认网关: 192.168.3.1
3.在Ubuntu虚拟机中添加新网卡
在Windows 11的控制台打开Ubuntu虚拟机实例的shell终端
PS C:\Users\MyPC> multipass shell primary
编辑Ubuntu虚拟机实例的网络配置:
ubuntu@primary:~$ cd /etc/netplan/
ubuntu@primary:/etc/netplan$ ll
total 12
drwxr-xr-x 2 root root 4096 Aug 2 09:19 ./
drwxr-xr-x 96 root root 4096 Aug 1 13:51 ../
-rw-r--r-- 1 root root 637 Aug 1 21:47 50-cloud-init.yaml
ubuntu@primary:/etc/netplan$ sudo vim 50-cloud-init.yaml
修改 50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0:
dhcp4: true
match:
macaddress: 52:54:00:70:f3:22
set-name: eth0
eth1:
addresses: [192.168.3.249/24]
gateway4: 192.168.3.1
nameservers:
addresses: [192.168.3.1]
version: 2
此处,我给Ubuntu虚拟机配置固定IP为:192.168.3.249
应用新配置的网络设定,使其生效
ubuntu@primary:/etc/netplan$ sudo netplan apply
4. 进入Ubuntu虚拟机,确认新网络生效
PS C:\Users\MyPC> multipass shell primary
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-122-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Tue Aug 2 09:47:42 CST 2022
System load: 1.6 Processes: 124
Usage of /: 4.1% of 38.58GB Users logged in: 1
Memory usage: 6% IPv4 address for eth0: 172.24.38.237
Swap usage: 0% IPv4 address for eth1: 192.168.3.249
* Super-optimized for small spaces - read how we shrank the memory
footprint of MicroK8s to make it the smallest full K8s around.
https://ubuntu.com/blog/microk8s-memory-optimisation
10 updates can be applied immediately.
9 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable
Last login: Tue Aug 2 09:15:39 2022 from 172.24.32.1
ubuntu@primary:~$
可以看到新网卡已经生效
总结
以上是在我的Windows 11系统中配置Multipass的Ubuntu虚拟机实例使用固定IP的方法,更多内容可以参考Multipass官方文档