LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   Correct way to define hostname in /etc/hosts (https://www.linuxquestions.org/questions/red-hat-31/correct-way-to-define-hostname-in-etc-hosts-673816/)

thespaceman 10-02-2008 03:53 PM

Correct way to define hostname in /etc/hosts
 
Hi,

I've just done my first install of RHEL 5.1 and every went ok and the server is on the network.

My question is what is the correct way to define the hostname within /etc/hosts? The entry after the install is like this:

127.0.0.1 myserver.mydomain.com myserver localhost


Normally (in Solaris,HP-UX etc) the hosts file would be as follows:

127.0.0.1 localhost
10.10.10.10 myserver myserver.mydomain.com


So for Linux should I leave /etc/hosts as is or should I go for the Solaris method?

The address 10.10.10.10 is the IP associated with the primary interface as is defined as an IP within the NIC device config file. The hostname is set to myserver.

Thanks.

CRC123 10-02-2008 04:06 PM

Leave it whatever way the Linux one has it. I got this from 'man hosts':
Quote:

For each host a single line should be present with the following information:
IP_address canonical_hostname [aliases...]

stuart_cherrington 10-03-2008 02:26 AM

If you're going to run any apps (like Oracle) then you need the FQDN first in the lists of hostnames.

thespaceman 10-03-2008 12:03 PM

So this is correct:

127.0.0.1 myserver.mydomain.com myserver localhost


But how would you define the 10.10.10.10 address? Obviously other hosts on the same DNS domain would associate myserver with 10.10.10.10. So would it be normal not to define the primary IP (hostname aliase) in /etc/hosts?

Is this done for performance reasons?

chrism01 10-05-2008 10:49 PM

/etc/hosts existed in the early days of arpanet (internet forerunner). All machines had the same file and new copies were emailed out regularly. When the net got too big, they came up with DNS/bind which would automatically find systems on demand.
These days /etc/hosts will often only have the 127.0.0.1 address. If you define another address in there, it will not be visible across the net.
It should be in the file that defines the NIC eg eth0. On my F8 system that's
/etc/sysconfig/networking/devices/ifcfg-eth0

jschiwal 10-05-2008 11:00 PM

If you don't run your own DNS server, then enter each host on your private lan , in the hosts file. If you have the avahi service running, you can use instead use "<hostname>.local" to refer to another computer by name.

rocinante 10-12-2008 10:27 AM

Quote:

Originally Posted by thespaceman (Post 3299147)
So this is correct:

127.0.0.1 myserver.mydomain.com myserver localhost


But how would you define the 10.10.10.10 address? Obviously other hosts on the same DNS domain would associate myserver with 10.10.10.10. So would it be normal not to define the primary IP (hostname aliase) in /etc/hosts?

Is this done for performance reasons?

I think it is probably done for machines that are using dhcp and may have their address change. This way local processes can still resolve the actual hostname without needing dns or changing /etc/hosts every time.

If you are using a static address you can certainly add a line for the 10.x.x.x address with the host name, and make the hosts file appear just like it does on a Solaris box.

You should also note that if there are more than one entry in /etc/hosts with the same address, only the last will be used. So if you had :

127.0.0.1 myserver.mydomain.com myserver localhost
10.10.10.10 myserver.mydomain.com myserver
[...]
then myserver.mydomain.com myserver would resolve to 10.10.10.10, not 127.0.0.1.

On my machines with static addresses I *always* set up the hosts file with only localhost on the 127.0.0.1 line and a separate line for the "real" hostname and it's associated IP.


All times are GMT -5. The time now is 11:58 AM.