In which file is hostname alias persistent, if not /etc/hosts

aliashostname

In order to have an alias for my server, which can be seen in a "hostname -a" command, I edited the /etc/hosts file to add the alias at the end of the entry containing the hostname.

For example, my hostname is host1 and I want to have alias hostalias, I have below entry in /etc/hosts:

    192.168.0.1 host1 hostalias

With this change I am able to use "hostname -a" to see hostalias.

However, I can change it only once! If I edit the file /etc/hosts again to something like this:

    192.168.0.1 host1 hostalias2

the output of "hostname -a" is still hostalias.

Even after I remove the hostalias2 and reboot the server, it's still saying hostalias.

BUT, if I change the alias the first time after reboot, it takes effect.

So in fact I have two questions:

  1. Where is the hostname alias persistent, if not /etc/hosts (so it can survive a reboot).

  2. Why can it only be changed once per boot?

More information: It is a RHEL 6.2 server.

Best Answer

If you are on Fedora / RHEL / CentOS, the hostname is set by the rc.sysinit script after retrieving the desired value from the /etc/sysconfig/network file.

As to why it can only be changed once per boot - it can be changed anytime. You have to provide a new hostname to the hostname command to change it, just editing a file won't do so. Keep in mind though that if you change it this way, any programs that were running before you changed it may still have the old value cached.

Related Question