With LXD v3.17 how do you change the IP address of an existing container?

I am using LXD v3.17

Host is Ubuntu 18.04

I have 2 Ubuntu Bionic containers wg1 and wg2 I created.

I decided to follow this threads advice to change the IP address of wg1:

stgraber
Stéphane GraberMaintainer
Feb '18

If running a modern LXD with a LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.

$ lxc stop wg1
$ lxc network attach lxdbr0 wg1 eth0 eth0
$ lxc config device set wg1 eth0 ipv4.address 10.99.10.42

$ lxc start wg1

So I do that and verify that it supposedly changed by assigning 10.30.180.1:

$ lxc stop wg1
$ lxc network attach lxdbr0 wg1 eth0 eth0
$ lxc config device set wg1 eth0 ipv4.address 10.30.180.1
$ lxc config device get wg1 eth0 ipv4.address10.30.180.1

And the wg1 container “supposedly” has the IP address I set

Then I restart wg1:

$ lxc start wg1

Then…

$ lxc list

and it shows “wg1” with its old IP address not the new one…

wg1 | RUNNING | 10.30.175.11 (eth0)

BUT… lxc config still shows wg1 assigned the NEW IPv4 address ??

$ lxc config device get wg1 eth0 ipv4.address
10.30.180.1

Is this happening because Netplan was introduced between the time Stephane’s above information was posted and today?

If I go into the wg1 container and look at:

root@wg1:~# nano /etc/netplan/50-cloud-init.yaml

# This file is generated from information provided by
# the datasource. Changes to it will not persist across an instance.
# To disable cloud-init’s network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
ethernets:
eth0:
dhcp4: true

In “wg1” container I created the file:

/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

containing…

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

saved it and the exited back to Host and reran commands to change IP of WG1 and
restart the container “wg1” again!

but same thing occurred.

$ lxc config device get wg1 eth0 ipv4.address
10.30.180.1

and…

“lxc list” shows OLD IP still assigned to WG1 ???

$ lxc list

wg1 | RUNNING | 10.30.175.11 (eth0)

If you run the following, it should show /24.

$ lxc network show lxdbr0
config:
  ipv4.address:  10.30.175.1/24
  ipv4.nat: "true"
...

To be able to change the third octet of the network, you should change the /24 into a /16 by running

lxc network edit lxdbr0

Thanks Simos… I know that would work but that’s not what I was hoping to do.
I want to create a couple containers that are isolated from each other so I was hoping it would
be as easy as just assigning an IP to cn1 that wasn’t on the same subnet as cn2.
Doing the change you mentioned would still keep them both on the same subnet and thus reachable by each other :slight_smile :slight_smile:

Those static addresses are assigned over DHCP, so they must be on the subnet of the bridge or dnsmasq/dhclient will fail to apply the requested address.

Ok that explains what I was seeing!

I created & used my own bridge and had no problems.

Thank you!