How are 127.0.0.1, 0.0.0.0 and localhost different?
Asked Answered
O

2

18

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.

Oldworld answered 8/8, 2016 at 16:38 Comment(1)
D
26

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!

Deafanddumb answered 8/8, 2016 at 16:46 Comment(2)
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?Oldworld
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.Bradlybradman
W
5

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.

Woolard answered 8/8, 2016 at 16:47 Comment(2)
localhost can be set to any address. It is defined in file hosts which is looked up by the DNS resolver first.Undervalue
That is true, but it won't be the actual localhost. it will try to send the traffic somewhere other than the local host.Woolard

© 2022 - 2024 — McMap. All rights reserved.