Table of Contents

LXD Network Setup-DHCP & Static IP Addressing

LXD is one of the more powerful containers that you can use, and it comes native in Ubuntu and can be downloaded and installed on other linux systems.

Note: This is not a tutorial, but is a cheat sheet for quick reference and does not discuss in any detail the LXD commands other than those directly related to setting up networking. This is a ton of amazing tutorials written by brilliant authors that do an amazing job, please take a look.

Once the container is initialized you need to create a new container instance.

lxc launch ubuntu:21.10 S1-TestContainer

In your host computer you will need to edit your netplan file (/etc/netplan/00-installer-config.yaml). The file may be named something different but there should only be one in the file – this file is the setup file for networking and looks like the following:

network:
  ethernets:
    eno3:
      dhcp4: true
    eno4:
      dhcp4: true
    eno5:
      addresses:
      - 10.10.1.30/24
      gateway4: 10.10.1.1
      nameservers:
        addresses:
        - 75.75.75.75
        - 75.75.75.76
        search: []
    version: 2

In your host computer you will need to edit your netplan file (/etc/netplan/00-installer-config.yaml). The file may be named something different but there should only be one in the file – this file is the setup file for networking and looks like the following:

A few things to note,

Interface eno5 has network connectivity, so as you update the config file you will need to point everything back to eno5 otherwise you will loose you network connectivity.

Now, update the netplan config file to look like the following. In this I did remove the network interfaces that are not being usedI am not using for readability. You can keep them or remove them as you wish.

network:
  version: 2
  ethernets:
    eno5:
      dhcp4: false
  bridges:
    br0:
      dhcp4: false
      addresses:
        - 10.10.1.30/24
      routes:
        - to: default
          via: 10.10.1.1
      nameservers:
      addresses:
        - 75.75.75.75
        - 75.75.75.76
      interfaces:
        - eno5

Save the configuration and commit it. Again, be sure you have updated the correct network interface and ip addresses or you will loose connectivity.

Now you need to create the container and the bridge between the interface in your container an the interface in your host. In essence the command marks the container “S1-TestContainer” as a bridged container and links the interface “br0” in your host operating system with the container’s interface eth0.

Create the container with the following step:

lxc launch ubuntu:20.04 S1-TestContainer

Now that the container is created you will need to bridge the container interface with the hosts network interface.

sudo lxc config device add  S1-TestContainer eth0 nic nictype=bridged parent=br0 name=eth0

Now, list out the containers and you should get a dynamic IP address assigned by your local dhcp server.

and  you should see the following output:

+—————————+—————–+—————————-+——–+——————-+——————–+
|   NAME                   |  STATE       |        IPV4                   | IPV6 |   TYPE            | SNAPSHOTS |
| S1-TestContainer | RUNNING | 10.10.1.141 (eth0) |          | CONTAINER | 0                     |
+—————————+—————-+—————————-+——–+——————-+——————–+

Note: When you create an LXD container it initially assigns you an internal ip address from the its own internal DHCP server, this flow overrides the internal interface and provides you a direct bridge to your local internet. you will need to execute the “lxc config device add” for every container you build.

Set a static IP Address

Once you have completed the previous steps, and desire to assign a static IP address to the container you will need to go into your running container.

lxc exec S1-TestContainer –- /bin/bash

Once inside S1-TestContainer you will need to update its netplan to set the Static IP Address. The config file will look something like the following:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses:
        - 10.10.1.31/24
      routes:
        - to: default
          via: 10.10.1.1
      nameservers:
        addresses:
          - 75.75.75.75

As with enabling the new configuration in the host computer you will also need to enable the configuration file in your container.

 

Leave a Reply

Your email address will not be published. Required fields are marked *