I followed this thread while writing my cloud-init script.
I've tested and I have this in one part of my boothook script:
echo "HOSTNAME=$HOSTNAME" >> /etc/environment
echo "127.0.1.1 $HOSTNAME" >> /etc/hosts
echo "$HOSTNAME" > /etc/hostname
echo "$HOSTNAME" > /proc/sys/kernel/hostname
hostnamectl set-hostname $HOSTNAME
When I login to my system for the first time:
$ echo $HOSTNAME
aaa.bbbb.domain.org
$ cat /etc/hosts
127.0.1.1 aaa.bbbb.domain.org
$ cat /etc/environment
HOSTNAME=aaa.bbbb.domain.org
But for some reason I can't understand:
$ cat /etc/hostname
aaa
$ cat /proc/sys/kernel/hostname
aaa
$ hostname
aaa
Not sure why! Any clues?
hostnamectl set-hostname $HOSTNAME
manually the moment I log in, it works fine. I tried moving it to a user-script that runs later around rc.local and it didn't help.