LXC container hostname problems

jl25

New Member
Feb 21, 2017
3
1
1
31
Hello,

So I have an LXC container running Ubuntu 16.04. I created a script that changes the default hostname (LXC-Ubuntu) to a randomly generated series of letters and numbers.

Running a command through /etc/rc.local each time I reboot the system, the hostname is changed using this script:

Code:
#!/bin/sh

rhex=`cat /dev/urandom | tr -cd 'a-f0-9' | head -c 8`
nh="Ubuntu-$rhex"

if [ "$1" = "" ]; then
  hostname $nh
  echo -n "The hostname is now $nh"
else
  nh="$1"
fi

echo "This script will configure the host with the following settings:"
echo Hostname: $nh

echo /etc/hostname:
echo $nh

echo /etc/hosts:
cat /var/tmp/newhosts | sed -e "s/nh/$nh/g"

echo $nh > /etc/hostname
cat /var/tmp/newhosts | sed -e "s/nh/$nh/g" > /etc/hosts

"newhosts" file:
Code:
127.0.0.1    localhost
127.0.1.1    nh

The changes seems to take effect after rebooting, however after another reboot to verify they are permanent my changes are lost and it resets /etc/hosts and /etc/hostname back to the original hostname.

I have tested this on a separate VM I have to success, so I have came to the conclusion that it may be caused by ProxMox itself. The LXC container in my VE has a hostname of LXC-Ubuntu as well and I believe this may be overwriting my script changes.

Is there any way to resolve this issue?

Any help is appreciated
 
  • Like
Reactions: damaged_menu