Can I assign a new IP address to an existing container?

Hello,

I have a container called or-pages running on my local machine. Here are the details

or-pages | RUNNING | 10.45.32.230 (eth0) | | PERSISTENT | 0

I’d like to change the IP address of this container, from the one that was automatically assigned when I created it, to something else like 10.23.66.45 . But I’m not sure how (or if) I can do it. Can you please point me in the right direction?

Thanks,
Akshat

When you ran lxd init, it asked you what type of network to automatically create for you. The default is a 10.x.y.z network, randomly chosen, with netmask of 255.255.255.0. That means that you can change only the ‘z’ in the IP address.

What you can do is reconfigured LXD again and select the netmask 255.0.0.0 instead. Therefore, you can assign any 10.* IP address of your choosing.

1 Like

Hi,

If you are using the default profile with the default network (I suppose its name is lxdbr0) you can modify the IP/mask with:

$ lxc network edit lxdbr0
config:
  ipv4.address: 10.0.0.254/24  <--- ip interface lxdbr0
  ipv4.dhcp.ranges: 10.0.0.100-10.0.0.150  <--- IP range to serve via dhcp
  ipv4.nat: "true"  <--- to create iptables SNAT rule
  ipv6.address: none
  ipv6.dhcp: "false"
  ipv6.nat: "false"
description: ""
name: lxdbr0
type: bridge
used_by: []
managed: true

It’s an example. You can see more options at https://github.com/lxc/lxd/blob/master/doc/networks.md
By default, your container will receive an IP config via dhcp; nevertheless, you can configure a static IP editing /etc/network/interfaces inside the container.

If you want your container has an IP from the real LAN, you have to use bridge mode or macvlan mode. See Another 'networking issue' or 'how to connect containers to more than one network using a bridge or macvlan'

1 Like

Thank you @simos. When I tried to reinitialize my lxd configration using lxd init I got an error saying that

You have existing containers or images. lxd init requires an empty LXD.

For my local use I can delete all the images and reinitialize lxd. However this might not be possible in the production use. Is there any alternative to re initializing?

Thank you @mgregal. This is exactly what I was looking for but unfortunately I can’t run lxc network (I get an unknown command error)

I’m using lxc version 2.0.11

You need to change the network settings, and we described two ways to do that.
My suggestion obviously is the simpler but would need to reinstall LXD.

@mgregal 's suggestion is much better but it is for a newer version of LXD (like LXD 2.21 or LXD 3.0).

A third suggestion is to use lxc config to change those configuration values to the values shown above. That would work with LXD 2.0.11.
Then, you would need to restart the contains in order to boot up with the new network settings.

As Simos says, I’m using LXD version 2.21

@simos @mgregal got it. Thanks!

If I’m not wrong you could use this to reconfigure it

sudo dpkg-reconfigure -p medium lxd

1 Like

Hi @simos,

Can we provide 192.x.x.x subnet for LXC containers instead of 10.x.x.x ?

Sure, you can enter whatever subnet you want during lxd init. We pick a random 10.x.x.0/24 out of the box, but it’s just the automatic value, you can always tell LXD to use something else if you want.