Operating System - HP-UX
1819736 Members
2821 Online
109606 Solutions
New Discussion юеВ

Re: Host file localhost entry location

 
SOLVED
Go to solution
Craig A. Sharp
Super Advisor

Host file localhost entry location

I need to do a sort on the hosts file to keep it in order after automatically adding entries to the bottom of the file.

I happen to have several 10.x.x.x entries in the file and as such, the 127.0.0.1 entry gets put in numeric order with the rest of the entries. This puts the localhost entry 5th on the list. I have heard that the entry must be the first on the list.

Is this true and if so what impact would it have to place the entry elseware in the file.

Does anyone have any examples of host file sort scripts that may be better for me to use instead of just "sort hosts -o hosts.out".

Thanks,

Craig A. Sharp
Roush Industries
8 REPLIES 8
melvyn burnard
Honored Contributor

Re: Host file localhost entry location

I do not think that the localhost entry is required to be the mandatory first entry in the hosts file, but from experience I have seen some strange effects when it is NOT the first entry, from STM not working correctly to problems in ServiceGuard.

I always prefer to have it as the first entry by default, but that is again my own preference/experience.
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
James R. Ferguson
Acclaimed Contributor

Re: Host file localhost entry location

Hi Craig:

I agree with Melvyn insofar as there is no reason I know that requires the 'localhost' entry to be the first one.

I have very limited entries beyond it and the local server's hostname since use DNS services.

In an MC/ServiceGuard cluster I have, the 'localhost' entry is actually the fifth one without problems.

If you wanted to sort your /etc/hosts, you could extract the non-commentary, sort it and reinsert it:

# awk '$1 ~/[0-9]/ {print$0}' /etc/hosts|sort > /tmp/hosts

...JRF...
...JRF...

Carlos Fernandez Riera
Honored Contributor
Solution

Re: Host file localhost entry location

Yes, i prefer to put localhost on the first uncommect lien followed by hostname ip and name.

I supose i were make a script using awk .

awk ' NR < 5 { print $0 }' /etc/hosts > /tmp/1
awk ' NR >5 { print $0 } ' > /tmp/2
sort /tmp/2 > /tmp/3
cat /tmp/1 > /etc/hosts
cat /tmp/3 >> /etc/hosts

unsupported
Craig A. Sharp
Super Advisor

Re: Host file localhost entry location

Hi all,

Thanks for the fast answers. I am starting to work in a script to do what I need. I am planning to keep the initial commented lines that hp provides and then sort and append the sorted file to the new file. I will post the script to the forum.

Thanks for your help.

Craig
Shannon Petry
Honored Contributor

Re: Host file localhost entry location

There is a recommended method for creating a hosts file, which does not match anything I have seen here. These methods are mainly for backward compatibility with SunOS 2,3,4, AIX 3&4, and SCO UNIX. Some other OS' could care less! Wish I could remember where the reference was, but it was in Sun documentation!

#comments
1.1.1.1 dns.server.name
1.1.1.2 hostname.of.lan0
127.0.0.1 localhost
# all others

I work with many systems, and have never had a problem using these criteria!

Regards,
Shannon
Microsoft. When do you want a virus today?
Shannon Petry
Honored Contributor

Re: Host file localhost entry location

Forgot to mention that if there is no dns server, then just omit that line!

Sorry,
Shannon
Microsoft. When do you want a virus today?
Curtis Larson
Trusted Contributor

Re: Host file localhost entry location

myself, i agree with melvyn, put it first. call me superstitious, but I always put it first and I've never had any problems.

As a suggestion, to make your maintanence easier I'd also put important hosts at the begining. Such as, DNS servers, local interfaces, gateways, nfs servers, nis servers, time servers, etc. About 90% of the entries in the hosts file I usual don't care about, and the 10% that are important to me I want to see right away at the top.
Craig A. Sharp
Super Advisor

Re: Host file localhost entry location

Thanks to all for your replies. I have decided to use the awk method to sort the hosts file and leave the localhost entry at the beginning.
So far following the sort, all is well.

Thanks,

Craig