On RHEL and similar distributions such as CentOS and OEL you can set the hostname by adding a hostname entry in /etc/sysconfig/network.

HOSTNAME=hydrogen.lan

If you don't have such an entry, the system will fall back to the default behaviour - referencing the hostname configured on the first matching entry in /etc/hosts.

192.168.92.101 hydrogen.lan hydrogen

Now, my question is. Are there any good reasons to use the direct hostname configuration in /etc/sysconfig/network or is it simply redundant in most scenarios?

link|improve this question

feedback

4 Answers

up vote 2 down vote accepted

The box in your example will have hydrogen.lan as its hostname, not hydrogen.

The only way I know of to have hostname return the short hostname and to have hostname -f return the FQDN, is to use both the mentioned options.

So add HOSTNAME=mybox to /etc/sysconfig/network and add

10.10.10.10 mybox.example.com mybox

to /etc/hosts. I like having hostname and hostname -f return different things. By default, Anaconda sets it up like you have it set up now.

link|improve this answer
I don't think it works that way. If the HOSTNAME= is set, that will be the FQDN regardless of what is in /etc/hosts. Your example will have hostname and hostname -f both return "mybox", at least on OEL5U4 where I just tested it. – Roy Dec 5 '09 at 11:35
You are quite right on the hostname being set to hydrogen.lan if that is the first none named in /etc/hosts. Going to try to reverse those and see what happens :) – Roy Dec 5 '09 at 11:41
Ok, reversing the order in /etc/hosts will give "hydrogen" as the hostname and FQDN. It also very much seems like the HOSTNAME= entry will override anything in /etc/hosts for both hostname and FQDN. – Roy Dec 5 '09 at 11:45
Note that having a fully qualified hostname does not seem to break anything. dhclient presents itself to the dhcp server with hostname only, as does the login prompt and the command prompt. – Roy Dec 5 '09 at 11:47
feedback

It will stop the hostname mysteriously changing when someone adds another entry (for a new web site, perhaps) to /etc/hosts (or eighteen months later when the machine is rebooted).

If the hostname is changed accidentally (by someone typing hostname then fat-fingering the return key) you can see what to change it back to.

link|improve this answer
feedback

Running "hostname myhost" from the command line immediately changes the hostname.

I define the short or full hostname by editing /etc/sysconfig/network in order to make the name persistent across reboots.

Adding an entry in /etc/hosts with the system's IP address and FQDN and short name makes the hostname consistent for services that rely on it (apache, lpd, sendmail, etc.)

link|improve this answer
Sure, but may I ask why you choose to set the hostname in /etc/sysconfig/network rather than let the system get it's hostname directly /etc/hosts? – Roy Dec 5 '09 at 18:16
feedback

the hostname command always worked for me just type man hostname

link|improve this answer
1  
Simply using hostname(1) will not make the change persistent. – Roy Dec 5 '09 at 9:53
feedback

Your Answer

 
or
required, but never shown

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