Take the 2-minute tour ×

I found some articles related to this, but they were not sufficient for me. I want to know how the localhost works and why is it the same on many different machines?

How does the localhost block the access to the website when we use it in the host file to block the website?

share|improve this question
I'm sorry but you have me completely lost on the second line of your question... what do you mean? –  Windos Aug 11 '11 at 7:03
@Windos, When you add an entry to the hosts file like " www.blah.com" it redirects that site to localhost (used to work with IE8 anyway) –  tzup Aug 11 '11 at 13:37
add comment

5 Answers

up vote 19 down vote accepted

I won't talk about the Windows case here, because it's a bit different, but let's say this: In every* Unix-like operating system, you have two network devices:

  • A loopback device
  • An Ethernet device

The first one is purely "virtual". It's like an Ethernet plug that you can't see. The second one has to do with the Ethernet plug in your computer.

enter image description here

What does the loopback interface do? Every traffic that you send to loopback will come back.

Like your Ethernet device gets an IP address (for example, the loopback device will also have an IP address, namely To make it easier, you can access it over localhost too.

If you have a look at a typical Unix /etc/hosts file, you will see that is mapped to "localhost". So whenever you type "localhost", your computer will know to call

And this is the reason why you can "block" applications. Let's say you don't want your computer to send data to Microsoft. Then you could simply redirect every request to microsoft.com to instead. Every attempt to contact microsoft.com would result in a failure.

* every system that actually has a hardware Ethernet port

share|improve this answer
In 'dows, the DNS Subsystem will resolve localhost to, and traffic to never even goes to an interface, the network stack loops right on back (it's really the exact same way as Unix, but without the virtual interface as a manifestation of the process). –  jcrawfordor Aug 15 '11 at 5:11
Although you can install the Microsoft Loopback Adapter, but it's not quite the same thing. –  slhck Aug 15 '11 at 9:16
Nowadays, many OSes support, so use that for blocking sites instead. Avoids the loop... –  Tom Wijsman Oct 7 '12 at 10:17
add comment

I discovered a presentation at The TCP/IP Guide that struck me as very illuminating:

Normally, when a TCP/IP application wants to send information, that information travels down the protocol layers to IP where it is encapsulated in an IP datagram.  That datagram then passes down to the data link layer of the device’s physical network for transmission to the next hop, on the way to the IP destination.

However, one special range of addresses is set aside for loopback functionality.  This is the range to  IP datagrams sent by a host to a 127.x.x.x loopback address are not passed down to the data link layer for transmission.  Instead, they “loop back” to the source device at the IP level.  In essence, this represents a “short-circuiting” of the normal protocol stack; data is sent by a device’s layer three IP implementation and then immediately received by it.

Also, Rutgers has a Networking course that includes this illustration of normal, inter-host communication,

normal, inter-host communication

and this one of the loopback data flow:

                                                    loopback data flow

share|improve this answer
add comment

Internet Protocol (IP) addresses fall in one of four basic categories.

  1. Addresses used to communicate with other computers around the world,
  2. Addresses used to communicate with computers just within a specific company or network,
  3. Addresses used to broadcast information between computers on a network,
  4. Addresses used to allow a computer to talk to itself.

By agreement, is one of those addresses that fall in the last of those categories.

As humans, we don't tend to remember IP numbers very well, but we have no problem remembering names like superuser.com, www.google.com, and other similar names. When the internet was small (very small), humans shared a "hosts" file to other people on the internet so they didn't have to remember numbers. That hosts file contained pairs of an IP address and one or more host names. When someone tried to access a host by its name, the computer software knew enough to go look it up in the hosts file. The internet has grown so much since then that we now use a domain name system (DNS) in order to resolve names to IP numbers in addition to using the old style hosts file. Most computers are set up to look up names in the hosts file first, then in DNS if that fails.

Now that you have the background, here's how it works (usually):

When you add this entry to your hosts file, any time your computer tries too look up www.foo.com, it will try to reach that site on the IP address because you told it that is the Internet Address for www.foo.com. Remember that will allow a computer to talk to itself so attempting to go to in your browser will try to contact a web server on your computer. If you had put in instead of, it would have tried to contact the computer at address when opening up www.foo.com. localhost www.foo.com foo.com

The bad thing about using the hosts file for things like this is once you add an entry to the hosts file, you are assuming responsibility for keeping that information up-to-date. If you don't put the entry in your hosts file, your computer will try to use DNS to look up the IP address, then use that IP address to contact the server.

The good thing about it is if you never want to allow anyone to talk to www.foo.com by the host name on your computer only, adding the entry to your hosts file may (keyword - may) prevent that from happening. If the goal is to prevent ever reaching a particular site by any means (not just by host name), there are other better and more reliable ways to prevent that from happening like properly using a firewall.

share|improve this answer
+1 for second paragraph. Nice work. –  avirk Aug 12 '11 at 2:24
add comment

For the second part of your question, if you put the hostname of a web site in your hosts file with the address it means that when you type the address into your browser instead of using DNS to find the correct address it finds it in your hosts file first resolves it to localhost and instead of going to the website's address just starts talking to your machine which causes the website to fail to load.

share|improve this answer
add comment

Localhost or is simply a built in 'name' for your local computer.

That is, is called a "loopback", because it is merely a pointer that points back to your machine. It's a reserved IP Address that can also be written as localhost by definition.

The purpose for this is to allow one to talk to one's computer by sending a signal outwards and then looping this back so that the signal is also read by your same computer.

Say you're running a server on your machine-- to access it, you have to send a request to the server (send a packet to, and your server has to read it (your system loops back the packet to your local system on which is then read by your server software).

share|improve this answer
add comment

Your Answer


