Lxd override hostname defined in /etc/hostname

hi!, I have a container with a hostname defined in /etc/hostname “mycontainer.lxd”
but the container was created with just the name “mycontainer”. So, when I create a snapshot for example, this hostname changes and generate me some problems. I must restart the container to take the name defined in /etc/hostname.

How I can do that “/etc/hostname” always have priority or, maybe changing some .yaml file avoid to the container change his hostname or put it the correct hostname?

thanks

When you set up LXD with the default private bridge, you are asking from LXD to provide a DNS server (dnsmasq) to resolve the names of the containers. If the container is called mycontainer, then this DNS server will have an entry for mycontainer and will answer to DHCP requests from the specify container about the mycontainer name. Then, it’s up to your container image to honor the DHCP reply and use it.

However, you can also manually change the network settings in a container, and use a different name, like mycontainer2. This will make it a bit messy because both mycontainer and mycontainer2 will work for this container.

Therefore, how do we solve this impending mess when we really want to change the name of a container? We solve it my making sure to update both LXD and the container with the new name!

Let’s create mycontainer.

$ lxc launch ubuntu:18.04 mycontainer
Creating mycontainer
Starting mycontainer

What’s the name in /etc/hostname?

$ lxc exec mycontainer -- cat /etc/hostname
mycontainer

What does systemd know about this?

$ lxc exec mycontainer -- hostnamectl status
   Static hostname: mycontainer
         Icon name: computer-container
           Chassis: container
        Machine ID: d28fb7af31513827378d9dd878022d40
           Boot ID: df6a83db2c6baaef9e399282939e3856
    Virtualization: lxc
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-33-generic
      Architecture: x86-64

Let’s change first the container name to mycontainer2.

$ lxc rename mycontainer mycontainer2
Error: Renaming of running container not allowed
Exit 1

Oh, busted!

$ lxc stop mycontainer

$ lxc rename mycontainer mycontainer2

$ lxc start mycontainer
Error: not found
Exit 1

$ lxc start mycontainer2

What does the container know about its name now?

$ lxc exec mycontainer2 -- cat /etc/hostname
mycontainer

$ lxc exec mycontainer2 -- hostnamectl status
   Static hostname: mycontainer
         Icon name: computer-container
           Chassis: container            
        Machine ID: d28fb7af31513827378d9dd878022d40
           Boot ID: df6a83db2c6baaef9e399282939e3856
    Virtualization: lxc
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-33-generic
      Architecture: x86-64

Oh, oh! The container still remembers the first name that was given during the first boot. It’s a static hostname because it is written in the file /etc/hostname.

How do we fix this so that the container is really called mycontainer2?

$ lxc exec mycontainer2 -- hostnamectl set-hostname mycontainer2

That was it? Did this really work? Let’s see.

$ lxc exec mycontainer2 -- cat /etc/hostname
mycontainer2

$ lxc exec mycontainer2 -- hostnamectl status
   Static hostname: mycontainer2
         Icon name: computer-container
           Chassis: container
        Machine ID: d28fb7af31513827378d9dd878022d40
           Boot ID: df6a83db2c6baaef9e399282939e3856
    Virtualization: lxc
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-33-generic
      Architecture: x86-64
2 Likes

Great answer! a lot of thanks.
I did not know about the “rename” command. That is enough.

1 Like