What is the difference between 0.0.0.0, 127.0.0.1 and localhost?
Asked Answered
D

3

519

I am using Jekyll and Vagrant on my mac. I found that Jekyll server will bind to 0.0.0.0:4000 instead of 127.0.0.1:4000. Also gem server will bind to this address by default. I can still visit it via http://localhost:port. But for Jekyll, it seems that the default setting (e.g. 0.0.0.0:4000) requires Internet access. I cannot run Jekyll server without Internet. Is it a small bug?

I also use Vagrant. I have set port forwarding(8080 => 4000) in Vagrantfile, since I install Jekyll in Vagrant virtual machine and test it under Macintosh. If I use the default setting (0.0.0.0:4000), it works. I can visit it from my safari with http://localhost:8080. But if there is not internet, I cannot bind to 0.0.0.0:4000. I use jekyll server -H 127.0.0.1 to bind service to 127.0.0.1:4000 instead, then I cannot visit it via http://localhost:8080.

Can anyone explain the difference between 0.0.0.0, 127.0.0.1 and localhost? And can anyone explain why the difference will cause this problem?

Danby answered 26/12, 2013 at 3:33 Comment(2)
This article may helpful howtogeek.com/225487/…Keith
This may be helpful What's the difference between 127.0.0.1 and 0.0.0.0?Keare
A
705

127.0.0.1 is normally the IP address assigned to the "loopback" or local-only interface. This is a "fake" network adapter that can only communicate within the same host. It's often used when you want a network-capable application to only serve clients on the same host. A process that is listening on 127.0.0.1 for connections will only receive local connections on that socket.

"localhost" is normally the hostname for the 127.0.0.1 IP address. It's usually set in /etc/hosts (or the Windows equivalent named "hosts" usually at C:\Windows\System32\Drivers\etc\hosts). You can use it just like any other hostname - try ping localhost to see how it resolves to 127.0.0.1.

0.0.0.0 has a couple of different meanings, but in this context, when a server is told to listen on 0.0.0.0 that means "listen on every available network interface". The loopback adapter with IP address 127.0.0.1 from the perspective of the server process looks just like any other network adapter on the machine, so a server told to listen on 0.0.0.0 will accept connections on that interface too.

That hopefully answers the IP side of your question. I'm not familiar with Jekyll or Vagrant, but I'm guessing that your port forwarding 8080 => 4000 is somehow bound to a particular network adapter, so it isn't in the path when you connect locally to 127.0.0.1

Anele answered 26/12, 2013 at 3:52 Comment(7)
Hmm, ping localhost seems to work on Windows even without the hosts file pointing 127.0.0.1 to localhost.Ceruse
Agree with Pacerier. At least in Windows, localhost behaves as 0.0.0.0 (not 127.0.0.1) by defaultTekla
"This is mostly correct except for your description of binding to 0.0.0.0. This doesn't bind to every available network interface as you've described but rather binds to all addresses. In TCP stacks this is known as INADDR_ANY – from deleted answer of @strangemonad.Buryat
Just to emphasize another common misunderstanding; you can't connect to 0.0.0.0 with a client, you have to know which actual addresses the server is listening on. For example, if the server runs on a system which has the addresses 10.9.8.7 and 192.168.128.64, you can connect to either of those (and 127.0.0.1 on the server system itself) when the server process listens to 0.0.0.0 (assuming of course that firewalls etc do not separately block your access for other reasons).Beatup
what about telling to listen on http://* vs 0.0.0.0? In my understanding it is the same. Is there a difference?Fortuitous
@PiotrDobrogost how is that not equivalent to listening on every available network interface? There is a one-to-one mapping between network interfaces and subnets where a server listens on, no?Incapacity
@OverInflatedWalrus: at the level we're talking about here, "listen on http://*" doesn't have any real meaning... one listens on (binds to) an IP address and a port number -- so yes, one can bind to port 80, or whatever other port one might talk http over (8080, 3000, 4000, whatever), but that's only half the story -- one also needs to bind to a particular address, which is what's being discussed here. See bind(2) for (a rabbit hole of much) more information, if you're interested.Build
E
41

I will explain the difference of 127.0.0.1 and 0.0.0.0 on example:

Imagine that you run any web server (e.g. Nginx) on a remote machine so that you want anyone on the Internet could access it.

If your Nginx web server listens to 127.0.0.1:80 then nobody could access it beside you logged on this server.

In case when web server listens to 0.0.0.0:80 anybody on the Internet is able to connect to your server by typing in browser your server IP address.

Note instead of port 80 there could be any port that is listened to. I took 80 for this example because this is a default port for web servers and browser does not force user to type it after IP address is entered.

Encumbrancer answered 8/6, 2022 at 7:31 Comment(2)
You did not explain the difference between 127.0.0.1 and 0.0.0.0, you just illustrated it.Tocci
@Tocci I believe lines 3 and 4 answer the poster's question, no?Donal
Y
39

In current version of Jekyll, it defaults to http://127.0.0.1:4000/.
This is good, if you are connected to a network but do not want anyone else to access your application.

However it may happen that you want to see how your application runs on a mobile or from some other laptop/computer.

In that case, you can use

jekyll serve --host 0.0.0.0

This binds your application to the host & next use following to connect to it from some other host

http://host's IP adress/4000 
Yecies answered 12/11, 2015 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.