Why is "127.0.0.1 localhost" needed in HOSTS file?

All we need is an easy explanation of the problem, so here it is.

I disabled all my network connections and deleted 127.0.0.1 localhost in HOSTS file but cannot find what had I broken by it. My IIS and MS SQL Server 2008 R2 continues to resolve localhost just fine

Why does HOSTS file always contain 127.0.0.1 localhost ?
What had I broken by deleting this entry?

I am on Windows XP Pro SP3 writing here still without localhost in HOSTS file.
Should I put it back and how fast ?


The reasons of interest are many fold – for instance:


—— UPDATE05:

I am not changing the question! I add updates.
Can I ask to stop deleting and editing it until I write that I fished with it?
For ex., just now I wrote the same comment in all posts addressing the same point.

This is the essence of my question/doubt – that the DNS does not make any sense in relation to “localhost” or “127.0.0.222” or “(local)” names, synonyms, aliases, links, addresses, IDs, tokens, whatever.

They are hundreds synonyms to the same entity and they are internal and Windows-es know it without any resolutions since there is no sense to resolve between so many synonyms!

They are related to internal computer mechanisms while DNS is external (between various computers). How can internal IDs can depend on external ones?

All Windowses (including Home Editions) will have internal DNS server in order to function?
and then replicate it when/if connected to network?

Well, the link from comments did not appear in Linked section, as I was told.

I forked a child subquestion:
https://stackoverflow.com/questions/3536351/is-localhost-host-resolved-to-127-0-0-1

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The hosts file just associates canonical or fully qualified names to IP addresses.

For instance, I could have:

127.0.0.1  moes-bar-and-grill

Then anything connecting to moes-bar-and-grill would establish a connection to the loopback device, aka 127.0.0.1, commonly resolved as localhost.

I could also have (and this is quite common)

127.0.0.1  annoying-ad-server.com

Applications continue to work because they will connect to 127.0.0.1 (which is still a configured / up interface) if localhost does not resolve.

I’m not sure why you would want to disable the loopback address, but simply taking localhost out of your host file is not going to do that.

Edit

Well written software will make more than one attempt at resolving anything (resolving in a sense of working around problems, no pun intended) before it just dies and in some cases will continue to function even if things are not as expected. That does not mean that the software will work as advertised, it only means that it was written by a very defensive programmer.

Very defensive does not always mean helpful when it comes to telling the user that serious problems exist, for instance localhost not resolving. I can write stuff that passes tests no matter what a user does to their system, but that does nothing to promote the cause of “This won’t work!”. There is a stark difference between it runs and it works and you will only explore the difference between the two over time with every program that you run.

While everything seems to work, now, I think you may be headed for trouble later.

Disclaimer: I write software for a living.

Method 2

The hosts file is a mapping between the IP address and the host name so that when you type the host name it resolves to the specified address.

By removing the line you are stopping Windows taking “localhost” and mapping it to “127.0.0.1”.

I can think of two reasons why you are seeing it “work”.

  1. You haven’t rebooted the machine so the mapping is still in memory.
  2. The applications you’ve tried it in do the mapping for you.

As long as #2 applies you don’t need it, but were you to use an application that didn’t do the mapping for you it would fail.

So, on balance, you should put the line back. However, you don’t have to do it as a matter of urgency.

Method 3

I believe that Windows-es know by heart both 127.0.0.1 and localhost
(and even more synonyms of themselves).
And, the answer probably, that it does not even resolve anything internally.

I shall see after reboot if it had lost ability to resolve localhost

But why would it?
What is the sense in it?

MS SQL Server resolves (connects through) not only through localhost name, but also through(local) and . (single-symbol dot).

So, localhost is just convenience (to write letters instead of digits and dots).
Conversion from localhost to 127.0.0.1 is silly
because I have never heard that digital addresses (numbers) are resolved to names to be further used internally by computers.

Now, I can connect to my local MSSQLServer not only by 127.0.0.1 (or localhost) but also through any address from 127.0.0.* range (for ex., I checked now having connected to local MSSQLServer through 127.0.0.2)

Though, if it is so, WTF this HOSTS entry?

Thanks to Tim Post for putting me in the direction of right thought and getting me out of turmoil.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply