LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2005, 10:51 AM   #1
itgl72
LQ Newbie
 
Registered: May 2003
Posts: 19

Rep: Reputation: 0
Proper Layout of /etc/hosts


I know this is a basic topic, and I have set these up many a time in simple environements and gotten the machine to work but I wanted to ask the forum about this question since I have read several help files online that have not really NAILED DOWN my question on /etc/hosts in a way in which I may want to use it.

I'm looking for the best practice in setting up the /etc/hosts for a linux box called "linuxserv" with an internal domain of abc.com and external domain of xyz.com.



Heres how I set up a basic system, IS IT WRONG TO GIVE IT TWO NAMES THAT POINT TO 127.0.0.1?

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
127.0.0.1 linuxserv.abc.com linuxserv


What about adding the internal IP address of the system like so, DOES THE FORMAT BELOW LOOK ACCEPTABLE?

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
127.0.0.1 linuxserv.abc.com linuxserv
192.168.1.2 linuxserv.abc.com linuxserv


Now lets say linuxserv also had a second network interface that links to the external network. What about adding the external IP address of the system like so, DOES THE FORMAT BELOW LOOK ACCEPTABLE?

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
127.0.0.1 linuxserv.abc.com linuxserv
192.168.1.2 linuxserv.abc.com linuxserv
234.X.X.X linuxserv.xyz.com linuxserv


What would make any of these unacceptable, or wrong in a best practice environement?
 
Old 04-22-2005, 11:33 AM   #2
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
Quote:
#lugo The next line is all that was in the file. I am commenting it out.
#127.0.0.1 debian localhost
#
#lugo Put in next block to try to get networking.
127.0.0.1 localhost
192.168.1.1 debian.domain.org debian
192.168.1.2 apricot.domain.org apricot
Don't know if that any help, it's my file. It's for computer 1, debian, on a two computer network. The other computer is apricot.domain.org. You may put what you want for domain.org.link. It's a Debian system.
 
Old 04-25-2005, 08:22 AM   #3
itgl72
LQ Newbie
 
Registered: May 2003
Posts: 19

Original Poster
Rep: Reputation: 0
That did not really clear up my question. I'm looking for someone to reply with information regarding my setup posted above. I understand the basics but wanted to get a better grasp of the hosts file and its formats.
 
Old 04-25-2005, 09:05 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
127.0.0.1 linuxserv.abc.com linuxserv
192.168.1.2 linuxserv.abc.com linuxserv
234.X.X.X linuxserv.xyz.com linuxserv

I believe that these lines would cause a problem. One thing that is sometimes done is to block access to a certain website by making its IP address 127.0.0.1 in the hosts file.

127.0.0.1 localhost localhost.localdomain
127.0.0.1 www.crackerland.com

That is assuming that /etc/hosts is searched first. That is true if a line in your host.conf is:
order hosts,bind
 
Old 04-25-2005, 09:25 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Here's the first few lines of my primary /etc/hosts file:
Code:
$ cat /mnt/fc3/etc/hosts | more
90.0.0.4        tss-4   TSS-4
90.0.0.3        tss-3   TSS-3
90.0.0.2        tss-2   TSS-2
90.0.0.1        tss-1   TSS-1
127.0.0.1       localhost.localdomain   localhost
127.0.0.1       view.atdmt.com
127.0.0.1       clk.atdmt.com
127.0.0.1       nytmembercenter.112.2o7.net
127.0.0.1       promotions.real.com
127.0.0.1       media.fastclick.net
Note the first five lines: They are each assigning two names to the specified address. (The 90.0.0.* lines are for our internal [i.e., "home"] network. Our firewall used 90.0.0.* for internal networks, and M$ seems to prefer uppercase names for NETBIOS, which one of the family's systems is using.)
 
Old 08-05-2013, 12:51 PM   #6
john.gatewood
LQ Newbie
 
Registered: Aug 2012
Posts: 1

Rep: Reputation: Disabled
What is wrong with the original hosts file?

Here is my understanding of hosts files:

Repeating the "linuxserver" entry without a comment (#) before it is not correct.
This entry is interpreted as an alias for the IP address on the line.
In the hosts file with multiple repeats of the same alias the last entry of that alias wins.
If you want to clarify that the hosts file entry is a Linux Server, change the entries to " # linuxserv"

Also, with respect to "Is it correct to have two lines with the same IP" the answer is no.
-The last entry of the same IP is the one that has precedence.
-The correct method is to put all the aliases on one line separated by white space (spaces or tabs)

One additional point:
-For the server you are on, the last alias on the line should be "loghost".
-This has two purposes.
-The first is to clarify the host machine in the file.
-The second is to explicitly tell applications which is the host to write log files to.

I normally put the loghost line fairly close after the lines:
"
# Do not remove the following line, or various programs
# that require network functionality will fail.

127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
"
 
Old 08-05-2013, 04:49 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Ummm. not quite.

You can have as many lines as you want. As many names as you want on a single line even. What ever looks good to you.

When looking up the name given the IP number, only the first one will be given. When looking up an IP number for a given name, the IP number of the first line with the matching name is given. The format convention is:

IPnumber canonical-hostname alias alias...

The canonical-hostname is assumed to mean the FQDN name equal to that used by the domain name system. But it doesn't have to. The alias names are just that - alternate names, sometimes short, alternate spellings, or just to allow for name changes (the alias would conventionally be the old name.. but during a change it could even be the new name).

This is a simple linear search - comments are ignored.

For a reference see the manpage (man hosts).

The only reason the 127.0.0.1 line should be present is that the local loopback interface (lo0) is usually initialized before the other network interfaces are, and before a DNS server might be started - thus allowing the localhost to exist by default. This also applies to the IPv6 local host (the ::1 is the IPv6 version of localhost).

Last edited by jpollard; 08-05-2013 at 04:54 PM.
 
Old 08-05-2013, 05:23 PM   #8
vorbote
Member
 
Registered: Aug 2009
Location: 2,625 m of paranoia above sea level
Distribution: Arch Linux, Debian GNU/Linux, Fedora, Void Linux, FreeBSD, OpenBSD and Win10
Posts: 77

Rep: Reputation: Disabled
Quote:
Originally Posted by itgl72 View Post

Heres how I set up a basic system, IS IT WRONG TO GIVE IT TWO NAMES THAT POINT TO 127.0.0.1?
No. You can have as many as you want.

Quote:
Originally Posted by itgl72 View Post
What about adding the internal IP address of the system like so, DOES THE FORMAT BELOW LOOK ACCEPTABLE?

Code:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
127.0.0.1               linuxserv.abc.com    linuxserv
192.168.1.2           linuxserv.abc.com    linuxserv
It is acceptable, it will work. The purists will tell you it is better to have an authoritative DNS server somewhere in the LAN but I disagree; if this is a small home/small biz network you can get away with using static addresses and editing hosts files. Then again, using a DNS server makes life sooo easy.

Quote:
Originally Posted by itgl72 View Post
Now lets say linuxserv also had a second network interface that links to the external network. What about adding the external IP address of the system like so, DOES THE FORMAT BELOW LOOK ACCEPTABLE?

Code:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
127.0.0.1               linuxserv.abc.com    linuxserv
192.168.1.2           linuxserv.abc.com    linuxserv
234.X.X.X               linuxserv.xyz.com           linuxserv
Perfectly legal and valid. I have used it myself with extra IP addresses assigned to aDLS boxen.

Quote:
Originally Posted by itgl72 View Post
What would make any of these unacceptable, or wrong in a best practice environement?
None is unacceptable nor wrong. Each has its place and depends on the circumstances and the choices you make. There is no perfection, there is the path to perfection. Best practices is not something set in stone but a dialectic exercise to asymptotically approach what best works for a given, dynamic, situation.

Last edited by vorbote; 08-05-2013 at 05:24 PM. Reason: spell flyswatting
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
External Hosts Resolve but Local Hosts Do Not kjm9 Linux - Networking 7 11-19-2005 03:51 PM
hosts.allow & hosts.deny question... jonc Linux - Security 9 03-05-2005 09:41 PM
Adding shell commands to hosts.deny and hosts.allow ridertech Linux - Security 3 12-29-2003 03:52 PM
hosts.deny and hosts.allow defaults? gui10 Linux - Security 5 12-20-2001 01:57 AM
hosts.allow / hosts.deny fistz Linux - Newbie 1 04-19-2001 07:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration