Ubuntu template doesn't respect lxc.uts.name

Hi there! Try to do:

lxc-create -t download -n test-1 -B loop --fssize 2G --fstype ext4 -- -d ubuntu -r focal -a amd64
sed -i 's/lxc.uts.name = test-1/lxc.uts.name = ololololo/g' /var/lib/lxc/test-1/config
lxc-start test-1
lxc-attach test-1 -- hostnamectl

result is:

   Static hostname: test-1
         Icon name: computer-container
           Chassis: container
    Virtualization: lxc
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-33-generic
      Architecture: x86-64

but, for example, on centos 7 it works as expected, Static hostname: will be ololololo .
Why ubuntu template does not respect lxc.uts.name ?

Depends on what the distro does for the hostname.

IIRC CentOS usually lacks a /etc/hostname file so it doesn’t attempt to set it on boot, whereas all Debian derivatives are required to have a /etc/hostname file which is written to /proc/sys/kernel/hostname at boot time and so replaces the preset uts value.

Hey, @stgraber ! Nice to see you again :slight_smile:
So, in other words i can’t change hostname with lxc.uts.name ? Maybe there are some other methods/variables to do that? :slight_smile:

The other method is to write the hostname you want to /etc/hostname in the container.

Changing hostname isn’t its own capability either and is tied to cap_sys_admin so no luck with a lxc.cap.drop there either.

O-o-o-or, i can just remove /etc/hostname from template? :slight_smile:

What do u think, if i change in ur lxc-download script

tar  --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_CACHE_PATH}/rootfs.tar.xz" -C "${LXC_ROOTFS}"


tar --exclude="${EXCLUDE_PATTERN}" --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_CACHE_PATH}/rootfs.tar.xz" -C "${LXC_ROOTFS}"

with EXCLUDE_PATTERN="etc/hostname"

It works like a charm for me, but i don’t know if it broke something in future …

Normally there’s no need for that because /etc/hostname is a templated file so when the container gets created, the container name is put in /etc/hostname.

As Debian/Ubuntu always have a /etc/hostname file, I’m reasonably sure that it’s a bad idea to remove it and certainly not something we’d be willing to do by default.

oh, ofc i don’t ask you to change ur script on mainstream. I copied the lxc-download to lxc-test with my local changes and it’s pretty enough 4 me. Anyway i understood the idea. Thx u so much 4 ur time! :slight_smile: