10

I don't understand the difference between these terms and how they are connected.

I looked at a hosts file on my computer and could see that 127.0.0.1 and localhost are connected but not sure how and I don't know where 0.0.0.0 fits into all this.

I've seen other answers to this question but I'm a newbie and the other answers about loopbacks and meta-addresses weren't easy to follow.

j obe
  • 1,026
  • 2
  • 10
  • 17

2 Answers2

17

Yes, on your local system localhost does translate into 127.0.0.1, which will always be the IP address of the machine that you are currently using. For example, on your computer accessing 127.0.0.1 (or localhost) will just attempt to open a connection with your own computer (useful if you're hosting a local web server)

Meanwhile, 0.0.0.0 has a different use. On a server or any machine with more than 1 ip address, it can be used to refer to all of the IP addresses that the machine uses. For example, if you create a simple java application with a serverSocket, you may want the server socket to listen for incoming connections on all of the servers IP addresses. You would then set the server socket to listen on 0.0.0.0. Hope this helps!

Logan Rodie
  • 585
  • 3
  • 12
  • Thanks that helps, I guess the most common reason for people using these is to develop and test websites and apps on their own machine? – j obe Aug 09 '16 at 11:13
  • 1
    `localhost` and `127.0.0.1` are only for outgoing connections. `0.0.0.0` is only used for listening (aka "bind") connections, and is a wildcard meaning "listen to all network interfaces on this machine". If you have only 1 network interface (i.e. one ethernet port, or one Wi-Fi connection), they appear to be the same, but once you have more than one connection, it makes a difference. – Mark Lakata Feb 14 '19 at 20:29
2

Any address in the whole 127.0.0.0/8 block will loop sent traffic back inside the computer.

Localhost is normally set to 127.0.0.1, but it could be set on any address in the 127.0.0.0/8 block.

The 0.0.0.0 address really means any IPv4 address.

Ron Maupin
  • 5,095
  • 4
  • 21
  • 27
  • localhost can be set to any address. It is defined in file hosts which is looked up by the DNS resolver first. – rodolk Aug 09 '16 at 00:27
  • That is true, but it won't be the actual localhost. it will try to send the traffic somewhere other than the local host. – Ron Maupin Aug 09 '16 at 00:32