What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

Viewed 209000

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?

3 Answers

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.

Related