Why is "127.0.0.1 localhost" needed in HOSTS file?

2

2

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

Gennady Vanin Геннадий Ванин

Posted 2010-08-20T12:27:50.597

Reputation: 605

1If you do something that results in something else breaking, it's generally a good idea to undo whatever you did :) Removing the host association to localhost will probably break things, even if nothing (yet) seems obviously broken. Note : Removing the entry in hosts doesn't take down the interface, it just breaks localhost resolving to that interface. – Tim Post – 2010-08-20T13:12:22.937

1-1 for unnecessary profanities. – Hello71 – 2010-08-20T14:17:35.607

1Do you have anything else to say, @vgv8 ? Two people answered your original question. Please don't get mad at us for not sitting here for hours refreshing this page just in case you make an edit so we can immediately revise our answer. – Tim Post – 2010-08-20T14:37:27.173

@Tim, I always have a lot to say but to be heard I need to formulate it first! Plz do not refresh, THIS IS NOT CHAT! There are various reasons why I can return late, for ex., because we have 12 hours difference between us. I need to think, to read, to sleep it over – Gennady Vanin Геннадий Ванин – 2010-08-20T14:58:31.570

@Tim, thanks! My updates reflecting your concerns and attention/inteerest to my question were updated by me and deleted by superior members but you still can see remnants in the "Linked" section to the right of this page or in revisions of main post – Gennady Vanin Геннадий Ванин – 2010-08-20T15:15:14.947

@vgv8 - Tim's point is that people answering won't get notified by your question updates unless they "favorite" it. The correct way to add details related to one of the answers given is to comment under this answer. If you added a lot of details which can benefit other answers, then editing the question is the good way to go, but note that you should put a "I have updated my question with more details regarding to what you said" comment under their answer, so they get notified about it. – Gnoupi – 2010-08-20T15:18:35.337

2Stop changing the question. This is not a chatroom. – random – 2010-08-20T15:20:22.403

@Gnoupi, thanks, your comment was the most important comment I ever had in trilogy. Can others see revisions for what had been deleted in main post? Once more deleted links appear in the section "Linked" in the right sidebar. Do others also see them? Or I add them into comments – Gennady Vanin Геннадий Ванин – 2010-08-20T15:27:05.607

@vgv8 - it's enough to put a link in the comments, or an answer, for it to appear on the "linked" category, on side. Revisions are visible for everyone, by clicking the "edited xxminutes ago" link. I haven't fully followed concerns about you, but from your question, I recommend two things: keep it clear, so that people can read and understand them, and don't change the topic to another question. If another question pops in your mind, you can ask a new one, separately. – Gnoupi – 2010-08-20T15:36:10.073

@Gnoupi, thanks. I shall post new questions (to get more downvotes UNTIL the button "ADD COMMENTS" disappear and one more suspension). I NEVER change - I add updates (or make corrections of slips in bold or noted as changed – Gennady Vanin Геннадий Ванин – 2010-08-20T15:41:37.167

2@vgv8 - people don't downvote without a reason. You have to balance the updates you make with the fact of keeping a clear question. A question with two pages of "updates" is impossible to read, and gets downvoted for not being clear. – Gnoupi – 2010-08-20T15:49:41.327

@Gnoupi. certainly! I am grateful! The negative response always draws more attention and people, who, eventually, can answer or add relevant helpful info. – Gennady Vanin Геннадий Ванин – 2010-08-20T16:17:32.293

@Tim, plz note that Linked posts are different from the links given in my main post – Gennady Vanin Геннадий Ванин – 2010-08-20T16:19:07.063

@ALL-ALL-ALL, See my answer! One more VICTORY OF ABSTRACT REASONING – Gennady Vanin Геннадий Ванин – 2010-08-20T18:01:35.753

@Hello71, u r on my list of spammers. Plz do not write in my posts, I shall ignore your writings – Gennady Vanin Геннадий Ванин – 2010-08-21T03:55:09.513

Forked subquestion http://stackoverflow.com/questions/3536351/is-localhost-host-resolved-to-127-0-0-1

– Gennady Vanin Геннадий Ванин – 2010-08-21T04:43:49.890

2@vgv8 these sites are not about the people. Don't hold personal grudges against other users, care only about the individual content. – Gnoupi – 2010-08-21T06:40:16.597

@vgv8 - the links in the "linked" column are appearing only if it's from Super User. Also, please stop editing your question for no reason. You had your answer, you marked it as accepted, stop splitting hairs. If you have another question, ask it in another one. It should be only one question per "question". It seems obvious when said, but it's apparently not obvious enough. – Gnoupi – 2010-08-21T10:54:55.250

Answers

13

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.

Tim Post

Posted 2010-08-20T12:27:50.597

Reputation: 851

@ Tim Post,
Unfortunately, I do not have reputation to upvote your answer(it requires 15). Only marked it as answer though I have doubts/comments that, I believe, deserve NOT to be lost in comments
– Gennady Vanin Геннадий Ванин – 2010-08-20T16:40:50.867

Ok, i've got ability to upvote by accepting your answer. +1, it is pity that correct answers are frequently undervoted – Gennady Vanin Геннадий Ванин – 2010-08-20T17:09:37.133

@Tim Post, I added my answer. Does it make sense to you? – Gennady Vanin Геннадий Ванин – 2010-08-20T17:26:12.540

@vgv8 - I've updated my answer. – Tim Post – 2010-08-20T20:04:54.630

@Tim, see my comments to other answers. I cannot get WTF internal computer mechanisms (and their labels, synonyms, IDs, addresses, links, whatever you may call them, for internal use) can depend on external ones (DNS)? – Gennady Vanin Геннадий Ванин – 2010-08-21T03:20:03.330

@moderator, I cannot submit (or edit) posts in SU, MSU, SO though I did not submit any questions for 24 hours (and I am banned from all other sites). How can I communicate to resolve this issue? – Gennady Vanin Геннадий Ванин – 2010-08-21T18:09:12.627

@vgv8: See my comment on http://superuser.com/questions/178706/is-localhost-host-resolved-to-127-0-0-1-closed

– Tamara Wijsman – 2010-08-21T19:06:44.303

@TomWij, thanks, see my comment to your comment there (I cannot post in MSU with the same issue and I am banned from MSO and MSF) – Gennady Vanin Геннадий Ванин – 2010-08-21T20:15:26.007

3

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.

ChrisF

Posted 2010-08-20T12:27:50.597

Reputation: 39 650

That's why the colleagues are for! If I reboot and it stops, then, I shall need to reboot again. I'm rebooting once per day (when go to sleep and get up at morning), if not more rare, so it would take me 2+ days to see if I shall not forget about this issue on the way. I tried to enable/disable network connections, various caches but evreything seems to continue working for me. – Gennady Vanin Геннадий Ванин – 2010-08-20T13:04:52.943

2@vgv8: Executing ipconfig /displaydns | findstr /C:localhost will show you that it is still in the cache. Executing ipconfig /flushdns will force a flush of the cache. If you won't, it will happen when the TTL expires. The default TTL is 86400 seconds, so after a day the localhost entry will expire and thus will not resolve anymore... – Tamara Wijsman – 2010-08-20T16:10:49.767

@TonyWij, I wrote that I made flushdns many times as well as much more already – Gennady Vanin Геннадий Ванин – 2010-08-20T17:08:04.503

@TonyWij, I also wrote that localhost as well as 127.0.0.222 work with all network connections disabled as well as without network cards inserted in my computer at all. Which internal DNS on Windows XP? – Gennady Vanin Геннадий Ванин – 2010-08-20T17:37:38.663

@vg8 You didn't state the DNS cache specific so that's why I thought about suggesting it to you, as there the localhost entry can be found if it's in the HOSTS file. Disabling or removing all connections doesn't cause the internal network features of Windows to stop functioning... You would have to disable the DNS Client service for this, which I wouldn't suggest as this is a huge impact on surfing. (As the caching stops working...) – Tamara Wijsman – 2010-08-20T20:30:21.863

@TomWij, 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, as they are related to internal computer mechanism while DNS is external (between various computers). I believe that I stated all (though it is transparent) but they were deleted. Is it possible to lock posts from editing until the lock is removed by author? – Gennady Vanin Геннадий Ванин – 2010-08-21T03:06:46.597

Locking is not possible, the complete history of posts can be viewed. Comments can also be removed without notice, but as far as I followed this question that didn't happen so far... As for DNS: Domain Resolving happens on your computer first with the HOSTS file and the DNS Client Cache, if the name can't be resolved there it will make a request to a DNS server. Your browser or SQL server might be written to automatically resolve localhost to 127.0.0.1 before the DNS stuff is even touched, but not every application will be able to do so... As Tim Post said. – Tamara Wijsman – 2010-08-21T10:23:43.260

0

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.

Gennady Vanin Геннадий Ванин

Posted 2010-08-20T12:27:50.597

Reputation: 605

That is great for the services you listed, but what breaks? I know you can't possibly have installed everything that dies if 'localhost' can't be resolved. I still don't quite understand why you wanted to get rid of it anyway? Maybe I'm just dense, I just want to give you a good answer. – Tim Post – 2010-08-20T19:41:26.007

The doubt is universal and conceptual WTF local names and their synonyms have to do with DNS? It is not comprehensible conceptually - if I unplug network the computer will stop working or shall all Windows head to having internal DNS server? – Gennady Vanin Геннадий Ванин – 2010-08-21T03:15:30.893

Check my comment on ChrisF his answer. If you unplug the network the only thing that will stop functioning are the request to an external DNS server and external computers; thus the HOSTS file, DNS Client Cache and internal network services will remain functional. – Tamara Wijsman – 2010-08-21T10:27:33.843