ᕕ( ᐛ )ᕗ Jimyag's Blog

multipass 虚拟机桥接主机网络

安装和配置 multipass

sudo snap install multipass 

sudo snap install lxd

multipass set local.driver=lxd
sudo snap connect multipass:lxd lxd

sudo snap info multipass

创建网桥

在宿主机上创建网桥

sudo vim /etc/netplan/00-installer-config.yaml
network:
  version: 2
  ethernets:
    eno1:
      dhcp4: true
      dhcp6: true
    enp1s0:
      dhcp4: true
      dhcp6: true
    enp2s0:
      dhcp4: true
      dhcp6: true
    enp4s0:
      dhcp4: true
      dhcp6: true
  bridges:
    br0:
      addresses:
      - 192.168.2.10/24
      routes:
      - to: default
        via: 192.168.2.1
      nameservers:
        addresses:
        - 192.168.2.1
      dhcp4: true
      dhcp6: true
      interfaces:
      - enp2s0
      - eno1
      - enp4s0
      - enp1s0

其中 enp2s0,eno1,enp4s0,enp1s0 是物理机的网卡,名字会和你的环境不一样,可以通过 ip a 查看,替换为自己的实际的网卡名。 按照需要设置网桥的地址、路由、DNS。

sudo netplan apply

查看 multipass 可用网卡

multipass networks

如果可以看到 br0 说明设置成功

Name              Type       Description
...
br0               bridge     Network bridge with enp1s0
eno1              ethernet   Ethernet device
enp1s0            ethernet   Ethernet device
enp2s0            ethernet   Ethernet device
enp4s0            ethernet   Ethernet device

创建虚拟机

multipass find
Image                       Aliases           Version          Description
core                        core16            20200818         Ubuntu Core 16
core18                                        20211124         Ubuntu Core 18
core20                                        20230119         Ubuntu Core 20
core22                                        20230717         Ubuntu Core 22
20.04                       focal             20240606         Ubuntu 20.04 LTS
22.04                       jammy             20240514         Ubuntu 22.04 LTS
23.10                       mantic            20240606         Ubuntu 23.10
24.04                       noble,lts         20240608         Ubuntu 24.04 LTS
....

选择一个镜像创建虚拟机,这里我选择 ubuntu 24.04,也就是 noble

multipass launch noble --name test -c 1 -d 10G -m 1G --network br0

multipass ls # 查看其 vm ip

multipass shell test # 进入虚拟机

--name 表示虚拟机名

-c 表示 cpu 核数

-d 表示磁盘大小

-m 表示内存大小

--network 表示使用网桥 br0

修改虚拟机路由

以下操作都在虚拟机内执行

虚拟机会使用默认的虚拟机内部的路由,这里把默认的路由修改为主机的路由

先查看虚拟机内部的网卡

ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:00:ac:3c:53 brd ff:ff:ff:ff:ff:ff
    inet 10.0.197.187/24 metric 100 brd 10.0.197.255 scope global dynamic enp5s0
       valid_lft 2110sec preferred_lft 2110sec
3: enp6s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:00:9e:3f:53 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.11/24 brd 192.168.2.255 scope global enp6s0
       valid_lft forever preferred_lft forever

桥接的网卡是 enp6s0,mac 地址是 52:54:00:9e:3f:53

修改 netplan 配置

sudo vim /etc/netplan/50-cloud-init.yaml

找到对应的 mac 地址,增加routes,nameservers

network:
    ethernets:
        default:
            dhcp4: true
            match:
                macaddress: 52:54:00:ac:3c:53
        extra0:
            dhcp4: false
            dhcp4-overrides:
                route-metric: 200
            match:
                macaddress: 52:54:00:9e:3f:53
            addresses:
            - 192.168.2.11/24
            routes:
            - to: default
              via: 192.168.2.1
            nameservers:
              addresses:
              - 192.168.2.1
    version: 2

应用配置

sudo netplan apply

#multipass