Lxd + Netplan + Static IP's in same subnet HOW-TO

Ok, this is the solution:

Install LXD (host only)

sudo apt remove --purge lxd lxd-client && apt install bridge-utils
sudo snap install lxd
sudo reboot
sudo lxd init

While installing, don’t create network bridge.

Setup network for host

sudo nano /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: no
      dhcp6: no
  bridges:
    br0:
      dhcp4: no
      dhcp6: no
      interfaces:
        - enp1s0
      addresses: [ 192.168.0.2/24 ]
      gateway4: 192.168.0.1
      nameservers:
          addresses:
              - 192.168.0.1
              - 8.8.8.8
              - 8.8.4.4
      parameters:
          stp: false
          forward-delay: 0
sudo netplan --debug apply

Reboot and confirm that config is fine:

sudo reboot
ifconfig -a
ping google.com

Edit default profile to make sure only bridge is present in config, with no extra nic’s

sudo lxc profile show default // add '> out.yaml' to output to file
sudo lxc profile edit default // add '< out.yaml' to read from file
### This is a yaml representation of the profile.
### Any line starting with a '# will be ignored.
###
### A profile consists of a set of configuration items followed by a set of
### devices.
###
### An example would look like:
### name: onenic
### config:
###   raw.lxc: lxc.aa_profile=unconfined
### devices:
###   eth0:
###     nictype: bridged
###     parent: lxdbr0
###     type: nic
###
### Note that the name is shown but cannot be changed

config: {}
description: Default LXD profile
devices:
  br0:
    nictype: bridged
    parent: br0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: default
used_by:
- /1.0/containers/apache

Setup network for container

sudo lxc exec <container> bash
nano /etc/netplan/50-cloud-init.yaml
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.0.5/24
      gateway4: 192.168.0.1
      nameservers:
        addresses:
          - 192.168.0.1
          - 8.8.8.8
netplan --debug apply

Now everything should work fine.

4 Likes