Nictype=routed: network is not working with Ubuntu 18.04 (but ok with Centos 7)

Network is not working with nictype=routed and Ubuntu 18.04. What I’m doing wrong?

$ct=c991
ostemplate="ubuntu/18.04"
ip="..."
lxc launch images:${ostemplate} ${ct}

lxc stop ${ct}
lxc config device add ${ct} root disk path=/ pool=lxd size=${disk}
lxc config device add $ct eth0 nic nictype=routed parent=eno1 ipv4.address=$ip
lxc start ${ct}

Now lxc list shows there is no IPV4 for this container and network is not working inside:

# lxc list
+------+---------+------------------------+------+-----------+-----------+
| NAME |  STATE  |          IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+------+---------+------------------------+------+-----------+-----------+
| c991 | RUNNING |                        |      | CONTAINER | 0         |
+------+---------+------------------------+------+-----------+-----------+

But with ostemplate="centos/7" network is working:

+------+---------+------------------------+------+-----------+-----------+
| NAME |  STATE  |          IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+------+---------+------------------------+------+-----------+-----------+
| c991 | RUNNING | x.x.x.x (eth0)         |      | CONTAINER | 0         |
+------+---------+------------------------+------+-----------+-----------+

(ip is hidden for security reason)

Hi!

This has been discussed here, 3.19 and Routed networking mode configuration example needed

Just after that reply by @tomp was a post (now withdrawn) by @MSTY that still, these instructions still do not work on Ubuntu container images. Then, @tomp replies, but the withdrawn post makes the conversation difficult to follow. In effect, the issue is that netplan on Ubuntu container images really tries to do the default, which is DHCP on the interface. You need to replace (in the container) the file /etc/netplan/* to the version found in that thread. You can do that either manually (lxc file push), or you can pass the information to the container through cloud-init.

Thanks @simos

The other approach I use is to override the default profiles ETH0 with a none device, and then add the routed device as ETH1.

E.g

lxc config device add c1 eth0 none
lxc config device add c1 eth1 nic nictype=routed name=eth1...

But on Ubuntu container images (ubuntu:) you would need to use cloud-init in the LXD profile if you want to do something like the following (i.e. launch the container in a single line).

lxc launch ubuntu: myroutedcontainer --profile default --profile routed_192.168.1.200

Isn’t it?

I have put the above together and wrote the following tutorial. Enjoy!

1 Like

Yes I would use cloud init to just set dns server if I wanted to avoid putting individual ips in a profile.

@simos @tomp But why the problem exists at all?

For centos container /etc/sysconfig/network-scripts/ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=c994
NM_CONTROLLED=no
TYPE=Ethernet
MTU=
DHCP_HOSTNAME=c994

And it’s working.

For ubuntu 18.04 /etc/netplan/10-lxc.yaml:

network:
  version: 2
  ethernets:
    eth0: {dhcp4: true}

So it’s dhcp in both cases, but with ubuntu it’s not working.

I changed this to:

network:
  version: 2
  ethernets:
    eth0: {dhcp4: true; hostname: test995}

where test995 is the name of my test conatiner and network is working now. So, may be it’s error in some template? Does this file generates automatically when I’m adding routed network?

btw, is there a way to set default nameserver (8.8.8.8) both for ubuntu and centos? Or should I do it manually?

Hi @Nick_Knutov

So this comes down to the difference in behaviour of DHCP client process inside distributions.

In CentOS 7, the dhclient process is still running, but it seemingly does not remove preconfigured addresses from the interface until it actually gets a lease (which it won’t). So the LXD configured IP remains.

In Ubuntu 18.04 however, it uses netplan which in turn uses systemd-networkd. I’m not sure which one of those clears the existing interface config, but it does happen.

The reason adding eth0: {dhcp4: true; hostname: test995} “fixes” the problem is because that is an invalid netplan config, and using netplan apply command will show this. Because this is invalid, netplan fails to start, and this leaves the LXD configured IP in place.

You can configure nameservers using cloud-init, see

Unfortunately there doesn’t appear to be a way I can find in netplan of telling it to just configure nameservers and leave the rest of the IP configuration alone. If you just set nameservers in netplan it still wipes out the LXD configured IPs on the interface.

I found the best way to solve my problem is:

lxc launch images:${ostemplate} ${ct}
lxc stop ${ct}
...
lxc config device add $ct eth0 nic nictype=routed parent=eno1 ipv4.address=$ip
lxc file delete $ct/etc/netplan/10-lxc.yaml
lxc start $ct
lxc exec $ct -- systemd-resolve --set-dns=8.8.8.8  --set-domain=localdomain --set-llmnr=yes --set-mdns=no --set-dnssec=no --interface=eth0
1 Like