Setting Up LXC Containers on the Host LAN in Ubuntu 22.04
Making my LXC containers accessible on the host machine LAN is something I had successfully done using the old networking interfaces way.
However, Ubuntu 22.04 uses Netplan and I don’t care to fight with the default way anymore by removing netplan etc so it was time to bite the bullet and figure out how to do it with Netplan.
I found this how to on how to change netplan from network manager to networkd, (see the bottom).
The commands in case it goes away:
sudo systemctl enable systemd-resolved.service
sudo systemctl enable systemd-networkd.service
sudo systemctl start systemd-resolved.service
sudo systemctl start systemd-networkd.service
sudo systemctl disable NetworkManager.service
sudo systemctl stop NetworkManager.service
Setting up br0, also from the above tutorial, the current /etc/netplan/01-network-manager-all.yaml:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: false
dhcp6: false
bridges:
br0:
interfaces: [enp7s0]
addresses: [192.168.68.250/24]
routes:
- to: default
via: 192.168.68.1
metric: 100
on-link: true
mtu: 1500
nameservers:
addresses: [1.1.1.1, 8.8.8.8]
parameters:
stp: true
forward-delay: 4
dhcp4: no
dhcp6: no
And then sudo netplan apply in order to create the new bridge having the static IP 192.168.68.250, note that enp7s0 is my NIC.
Let’s pretend I have a container called www74, the flow is then to:
1.) Do lxc exec www74 bash or SSH into it if you’ve got that setup.
2.) Run: echo ‘network: {config: disabled}’ > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
3.) nano /etc/netplan/50-cloud-init.yaml and replace contents with:
network:
version: 2
ethernets:
eth0:
dhcp4: false
dhcp6: false
addresses: [192.168.68.221/24]
routes:
- to: default
via: 192.168.68.1
metric: 100
on-link: true
mtu: 1500
nameservers:
addresses: [1.1.1.1, 8.8.8.8]
Note that in this case we want www74 to have the static IP 192.168.68.221 on the host’s LAN.
4.) Finally:
lxc stop www74
lxc network attach br0 www74 eth0 eth0
lxc start www74
Related Posts
Tags: containers, Linux, lxc, lxd, networking, sysadmin