Tested to work on:
- Ubuntu 23.04
Reference:
[https://fabianlee.org/2022/09/20/kvm-creating-a-bridged-network-with-netplan-on-ubuntu-22-04] [https://www.furorteutonicus.eu/2013-08-04-enabling-host-guest-networking-with-kvm-macvlan-and-macvtap]
Make life easy for managing VMs and containers in my home lab.
All VMs and containers will get IP addresses of the same network segment as the host, and reachable from the host.
If using NetworkManager, need to switch to systemd-networkd to manage network.
First, remove NetworkManager from netplan config in /etc/netplan/
folder. Then add a yaml config file
to use networkd (e.g. /etc/netplan/01-networkd.yaml):
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: false
dhcp6: false
Then use netplan apply
to make changes effective.
Then perform the following 4 steps:
- enable as services on startup
sudo systemctl enable systemd-resolved.service
sudo systemctl enable systemd-networkd.service
- start services
sudo systemctl start systemd-resolved.service
sudo systemctl start systemd-networkd.service
- check status
systemctl status systemd-resolved.service
systemctl status systemd-networkd.service
- disable NetworkManager
sudo systemctl disable NetworkManager.service
sudo systemctl stop NetworkManager.service
Put the setup script under /etc/networkd-dispatcher/configured.d/
:
#!/bin/bash
# filename: 10-setup-macvlan.sh
# basic net config, customize for my home network
HWLINK=enp3s0
MACVLN=macvlan0
IP=192.168.0.35/24
NETWORK=192.168.0.0/24
GATEWAY=192.168.0.254
# ------------
# setting up $MACVLN interface
# ------------
echo "# setting up $MACVLN interface..."
ip link add link $HWLINK $MACVLN type macvlan mode bridge
ip address add $IP dev $MACVLN
ip link set dev $MACVLN up
# ------------
# routing table
# ------------
# empty routes
echo "# empty routes..."
ip route flush dev $HWLINK
ip route flush dev $MACVLN
# add routes
echo "# add routes..."
ip route add $NETWORK dev $MACVLN metric 0
# add the default gateway
echo "# add the default gateway..."
ip route add default via $GATEWAY
Reboot to make effective.
If things are okay, you will get something like following:
xxx: $ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
<snip>
6: macvlan0@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
<snip>
xxx: $ ip a show macvlan0
6: macvlan0@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.0.35/24 scope global macvlan0
valid_lft forever preferred_lft forever
inet6 xx--xx:9950/64 scope global temporary dynamic
valid_lft 86392sec preferred_lft 14392sec
inet6 xx--xx:84f5/64 scope global dynamic mngtmpaddr
valid_lft 86392sec preferred_lft 14392sec
inet6 fe80::xx--xx:84f5/64 scope link
valid_lft forever preferred_lft forever
config:
limits.cpu: "2"
limits.memory: 4GiB
description: LXD profile that use macvlan
devices:
eth0:
nictype: macvlan
parent: macvlan0
type: nic
root:
path: /
pool: pool2
type: disk
name: macvlan-profile
used_by:
- /1.0/instances/win10
- /1.0/instances/d7vlan