5

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.

3
  • 6
    Which operating system? Commented Jun 25, 2013 at 10:05
  • 2
    Possibly you have a name service cache daemon. Try after sudo nscd -i hosts (to invalidate the host cache). Commented Jun 25, 2013 at 13:11
  • @StephaneChazelas Thanks Stephane, it is indeed caused by name service cache.
    – cutemic
    Commented Oct 10, 2013 at 4:32

2 Answers 2

2

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.

1
  • yes, it is RHEL. But it is not about changing hostname, but adding hostname alias via the /ect/hosts file.
    – cutemic
    Commented Oct 8, 2013 at 10:40
2

@StephaneChazelas is right in this comment.

Possibly you have a name service cache daemon. Try after sudo nscd -i hosts (to invalidate the host cache).

I can't make a comment the answer of a question so I answer this question myself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .