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 " $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

$ cat /etc/hosts  aaa.bbbb.domain.org

$ cat /etc/environment

But for some reason I can't understand:

$ cat /etc/hostname

$ cat /proc/sys/kernel/hostname

$ hostname

Not sure why! Any clues?

  • If I run 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. – phpguru Oct 17 '15 at 7:32

I actually discovered after a lot of tinkering and researching that what I am seeing is the intended behavior, although it is different from what I am used to on CentOS/RHEL systems.

To summarize this from here:

CentOS & RHEL deployment guide say the hostname should be the FQDN; Debian specifies the hostname should NOT be the FQDN.

The kernel maintains the system hostname. This file should contain only the system hostname, not a fully qualified domain name.

I'm fine with it this way (sure makes shell prompts shorter!) I just wanted to make sure I wasn't doing something wrong.


you must edit /etc/hosts and add hostname like this:    aaa.bbbb.domain.org aaa.bbbb
  • Why? OP already added an entry for their domain to point to the loopback interface to. – David Foerster Oct 17 '15 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.