220

Assuming the following is defined in .../hosts:

127.0.0.1 localhost

What, if any, are the actual differences between using 127.0.0.1 and localhost as the server name, especially when hitting processes running locally that are listening for connections?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 21
    You could say `localhost` is protocol independent, both IPv4 and IPv6 will work. – Steve-o Sep 12 '11 at 02:05
  • 1
    Another different is that most browsers treat `localhost` as a secure origin, whereas `127.0.0.1` is not treated as secure. –  Oct 22 '20 at 22:46

5 Answers5

169

Well, the most likely difference is that you still have to do an actual lookup of localhost somewhere.

If you use 127.0.0.1, then (intelligent) software will just turn that directly into an IP address and use it. Some implementations of gethostbyname will detect the dotted format (and presumably the equivalent IPv6 format) and not do a lookup at all.

Otherwise, the name has to be resolved. And there's no guarantee that your hosts file will actually be used for that resolution (first, or at all) so localhost may become a totally different IP address.

By that I mean that, on some systems, a local hosts file can be bypassed. The host.conf file controls this on Linux (and many other Unices).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 10
    The main difference is that the connection can be made via Unix Domain Socket, as stated here: http://stackoverflow.com/questions/3715925/localhost-vs-127-0-0-1 – Don Viegues Sep 21 '14 at 13:12
  • `/etc/nsswitch.conf` selects if `hosts` or DNS is used first for a host lookup, if nss is running. – Mark Lakata Feb 14 '19 at 20:16
  • 2
    @DonViegues That's MySQL specific and IMO bad design. It will see localhost and tries to use unix-socket instead of connecting using IP but for 127.0.0.1 it just uses IP. – Arman Ordookhani Jul 18 '19 at 08:45
  • @ArmanOrdookhani AFAIK if you are connecting to a remote host via SSH and then from there to a MySQL (like a SSH tunnel to reach the MySQL server) you have to use 127.0.0.1, as the whole thing goes over TC/IP. But if 2 processes running on the same machine want to connect, localhost (unix socket) is faster/has lees overhead. Thanks! – Don Viegues Jul 30 '19 at 01:48
  • 3
    @DonViegues Yeah you are right unix socket has less overhead than TCP/IP. I just wanted to mention this behaviour is specific to MySQL and not something in OS or networking level. – Arman Ordookhani Jul 30 '19 at 19:26
65

Wikipedia sums this up well:

On modern computer systems, localhost as a hostname translates to an IPv4 address in the 127.0.0.0/8 (loopback) net block, usually 127.0.0.1, or ::1 in IPv6.

The only difference is that it would be looking up in the DNS for the system what localhost resolves to. This lookup is really, really quick. For instance, to get to stackoverflow.com you typed in that to the address bar (or used a bookmarklet that pointed here). Either way, you got here through a hostname. localhost provides a similar functionality.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • 28
    This is actually important. localhost can resolve to an IPv6 address, whereas 127.0.0.1 cannot. Using localhost on an IPv6 only system can work, whereas 127.0.0.1 will not since IPv4 is not available. – Erwin Jansen Dec 04 '17 at 19:13
  • On my Windows10 system the DNS lookup is really, really slow, about 1100 milli secs. So I switched to 127.0.0.1 – Wolfgang Kuehn Jan 25 '21 at 17:26
40

Some applications will treat localhost specially.

For example, the MySQL client will treat localhost as a request to connect to the local Unix domain socket instead of using TCP to connect to the server on 127.0.0.1. This may be faster, and may be in a different authentication zone.

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Wayne Walker
  • 2,316
  • 3
  • 23
  • 25
10

Well, by IP is faster.

Basically, when you call by server name, it is converted to original IP.

But it would be difficult to memorize an IP, for this reason the domain name was created.

Personally I use http://localhost instead of http://127.0.0.1 or http://username.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
The Mask
  • 17,007
  • 37
  • 111
  • 185
  • localhost is faster, is does not use TCP/IP – Don Viegues Sep 21 '14 at 13:13
  • @Don Viegues - what you say is contradicted to the other answers in this thread. Can you elaborate? – Dikla Feb 05 '15 at 07:02
  • Yes, I will copy and paste other comment of mine: The main difference is that the connection can be made via Unix Domain Socket, as stated here: http://stackoverflow.com/questions/3715925/localhost-vs-127-0-0-1 – Don Viegues Feb 06 '15 at 17:30
  • 10
    @DonViegues that is specific to MySQL, which is handling `localhost` in a [special way](https://en.wikipedia.org/wiki/Localhost#Special_cases). Other applications will likely still lookup `localhost`. – William Denniss Jul 01 '16 at 02:15
-8

There is nothing different. One is easier to remember than the other. Generally, you define a name to associate with an IP address. You don't have to specify localhost for 127.0.0.1, you could specify any name you want.

d_ethier
  • 3,873
  • 24
  • 31