I understand that 127.0.0.1 points to localhost, and so does 0.0.0.0 (correct me if I'm wrong). So, what's the difference between 127.0.0.1 and 0.0.0.0?

migrated from security.stackexchange.com Aug 2 '15 at 14:27

This question came from our site for information security professionals.

  • 1
    You may be interested in this Chrome bug tracker discussion about how to handle 0.0.0.0 in address input. – user Aug 2 '15 at 21:34
  • 5
    In most server configurations 0.0.0.0 means, listen on ALL addresses! This will make your server available on the internet. If you do that for some local server without any security (it's only listening on localhost, right?) that's bad. Don't do that! – Josef Aug 3 '15 at 16:25
  • 49
    Answer your question literally, the difference is 2130706433. – Shaz Aug 3 '15 at 16:51
  • 2
    @Mast oh, you are one of the people who setup a totally insecure system, because the firewall will fix it all? Usually, this systems then can be found with shodan.io :3 The next level of this mindset is "we totally can use a 5 year old WordPress installation, we got a WAF". I don't recommend anyone going down this path, ever! – Josef Aug 5 '15 at 14:45
  • 1
    @Shaz I'd like to upvote your comment but as it's currently at 42 I can't bring myself to do it... – Jenny D Mar 27 at 7:44
up vote 180 down vote accepted

What's the difference between 127.0.0.1 and 0.0.0.0?

  • 127.0.0.1 is the loopback address (also known as localhost).

  • 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target (a no particular address placeholder).

    In the context of a route entry, it usually means the default route.

    In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.


What is IP address 127.0.0.1?

127.0.0.1 is the loopback Internet protocol (IP) address also referred to as the “localhost.” The address is used to establish an IP connection to the same machine or computer being used by the end-user.

The same convention is defined for computer’s that support IPv6 addressing using the connotation of ::1. Establishing a connection using the address 127.0.0.1 is the most common practice; however, using any IP address in the range of 127...* will function in the same or similar manner. The loopback construct gives a computer or device capable of networking the capability to validate or establish the IP stack on the machine.

Source: 127.0.0.1 – What Are its Uses and Why is it Important?


Special Addresses

The class A network number 127 is assigned the "loopback" function, that is, a datagram sent by a higher level protocol to a network 127 address should loop back inside the host. No datagram "sent" to a network 127 address should ever appear on any network anywhere.

Source: Network Numbers


If it's a whole class A, what is the point of other arbitrary values for the last three octets?

The purpose of the loopback range is testing of the TCP/IP protocol implementation on a host. Since the lower layers are short-circuited, sending to a loopback address allows the higher layers (IP and above) to be effectively tested without the chance of problems at the lower layers manifesting themselves. 127.0.0.1 is the address most commonly used for testing purposes.

Source: IP Reserved, Loopback and Private Addresses

For more information see https://askubuntu.com question What is the loopback device and how do I use it? and why is loopback IP address from 127.0.0.1 to 127.255.255.254?.


What is IP address 0.0.0.0?

"0.0.0.0" is a valid address syntax. So it should parse as valid wherever an IP address in traditional dotted-decimal notation is expected. Once parsed, and converted to workable numeric form, then its value determines what happens next.

The all-zero value does have a special meaning. So it is "valid", but has a meaning that may not be appropriate (and thus treated as not valid) for particular circumstances. It is basically the "no particular address" placeholder. For things like address binding of network connections, the result can be to assign an appropriate interface address to the connection. If you are using it to configure an interface, it can remove an address from the interface, instead. It depends on the context of use to determine what "no particular address" really does.

In the context of a route entry, it usually means the default route. That happens as a result more of the address mask, which selects the bits to compare. A mask of "0.0.0.0" selects no bits, so the compare will always succeed. So when such a route is configured, there is always somewhere for packets to go (if configured with a valid destination).

In some cases, merely "0" will also work and have the same effect. But this is not guaranteed. The "0.0.0.0" form is the standard way to say "no particular address" (in IPv6 that is "::0" or just "::").

Source: What is the meaning of the IP address 0.0.0.0


In the Internet Protocol version 4 the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target. To give a special meaning to an otherwise invalid piece of data is an application of in-band signaling.

In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

In the context of routing, 0.0.0.0 usually means the default route, i.e. the route which leads to "the rest of" the internet instead of somewhere on the local network.

Uses include:

  • The address a host claims as its own when it has not yet been assigned an address. Such as when sending the initial DHCPDISCOVER packet when using DHCP.
  • The address a host assigns to itself when address request via DHCP has failed, provided the host's IP stack supports this. This usage has been replaced with the APIPA mechanism in modern operating systems.
  • A way to specify "any IPv4-host at all". It is used in this way when specifying a default route.
  • A way to explicitly specify that the target is unavailable.1
  • A way to specify "any IPv4 address at all". It is used in this way when configuring servers (i.e. when binding listening sockets). This is known to TCP programmers as INADDR_ANY. (bind(2) binds to addresses, not interfaces.)

In IPv6, the all-zeros-address is written as "::".

Source: 0.0.0.0


DHCP Discovery/Request

When a client boots up for the first time, it is said to be in the Initializing state, and transmits a DHCPDISCOVER message on its local physical subnet over User Datagram Protocol (UDP) port 67 (BootP server). Since the client has no way of knowing the subnet to which it belongs, the DHCPDISCOVER is an all subnets broadcast (destination IP address of 255.255.255.255), with a source IP address of 0.0.0.0. The source IP address is 0.0.0.0, since the client does not have a configured IP address. If a DHCP server exists on this local subnet and is configured and operating correctly, the DHCP server will hear the broadcast and respond with a DHCPOFFER message. If a DHCP server does not exist on the local subnet, there must be a DHCP/BootP Relay Agent on this local subnet to forward the DHCPDISCOVER message to a subnet that contains a DHCP server.

This relay agent can either be a dedicated host (for example, Microsoft Windows Server), or router (for example, a Cisco router configured with interface level IP helper statements).

...

After the client receives a DHCPOFFER, it responds with a DHCPREQUEST message, indicating its intent to accept the parameters in the DHCPOFFER, and moves into the Requesting state. The client may receive multiple DHCPOFFER messages, one from each DHCP server that received the original DHCPDISCOVER message. The client chooses one DHCPOFFER and responds to that DHCP server only, implicitly declining all other DHCPOFFER messages. The client identifies the selected server by populating the Server Identifier option field with the DHCP server's IP address. The DHCPREQUEST is also a broadcast, so all DHCP servers that sent a DHCPOFFER will see the DHCPREQUEST, and each will know whether its DHCPOFFER was accepted or declined. Any additional configuration options that the client requires will be included in the options field of the DHCPREQUEST message. Even though the client has been offered an IP address, it will send the DHCPREQUEST message with a source IP address of 0.0.0.0. At this time, the client has not yet received verification that it is clear to use the IP address.

...

Client-Server Conversation for Client Obtaining DHCP Address Where Client and DHCP Server Reside on Same Subnet

Enter image description here

Source: Understanding and Troubleshooting DHCP in Catalyst Switch or Enterprise Networks


Default Route

This document explains how to configure a default route, or gateway of last resort. These IP commands are used:

  • ip default-gateway

  • ip default-network

  • and ip route 0.0.0.0 0.0.0.0

ip route 0.0.0.0 0.0.0.0

Creating a static route to network 0.0.0.0 0.0.0.0 is another way to set the gateway of last resort on a router. As with the ip default-network command, using the static route to 0.0.0.0 is not dependent on any routing protocols. However, ip routing must be enabled on the router.

Note: IGRP does not understand a route to 0.0.0.0. Therefore, it cannot propagate default routes created using the ip route 0.0.0.0 0.0.0.0 command. Use the ip default-network command to have IGRP propagate a default route.

Source: Configuring a Gateway of Last Resort Using IP Commands

They are not the same.

127.0.0.1 is part of the 127/8 network which is reserved and points to the same computer.

0.0.0.0 is a special IP address that means different things depending on context.

In the Internet Protocol Version 4, the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target. To give a special meaning to an otherwise invalid piece of data is an application of in-band signaling.

You might have mistaken 0.0.0.0 for 127.0.0.1 when you look at netstat and see the local address listening on as 0.0.0.0, but this is a different way 0.0.0.0 is used.

In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

In the context of routing, 0.0.0.0 usually means the default route, i.e. the route which leads to "the rest of" the internet instead of somewhere on the local network.

https://en.wikipedia.org/wiki/0.0.0.0

  • 0.0.0.0 can also sometimes indicate a broadcast, meaning to all computers on the network. – KronoS Aug 3 '15 at 15:19
  • 8
    @KronoS I thought broadcasts are things like 255.255.255.255 (or more realistically something like 192.168.1.255 for a local network) – Nick T Aug 3 '15 at 16:51
  • 1
    @NickT Ah you are correct: A special definition exists for the IP broadcast address 255.255.255.255. It is the broadcast address of the zero network or 0.0.0.0, which in Internet Protocol standards stands for this network, i.e. the local network. Transmission to this address is limited by definition, in that it is never forwarded by the routers connecting the local network to other networks. – KronoS Aug 3 '15 at 16:58
  • @KronoS You're confusing the broadcast addr with loopback. The loopback is 0.0.0.0 which as described above listens all the local addresses. The broadcast (in a simple way) is the 255 of the networks to "broadcast" to all addresses in that network. – Desorder Aug 3 '15 at 21:13
  • This is incomplete – many operating systems actually let you connect to 0.0.0.0, which then behaves like a loopback address. – grawity Aug 4 '15 at 7:48

127.0.0.1 one of the adresses of local computer, but any adress 127.x.y.z also is another adress of the computer (called "loopback address"), except 127.0.0.0 (loopback subnet) and 127.255.255.255 (broadcast address for the loopback subnet).

127.x.y.z means "here".

0.0.0.0 is totaly different: 0.0.0.0 is not the adress of anything, it is the joker, as * in shell.

You can't send data to 0.0.0.0 or actively open a TCP connection to 0.0.0.0 because there is nothing there; 0.0.0.0 isn't even an unreachable or non routable adress, it is meaningless in a context where an adress is expected.

You can use the joker 0.0.0.0 in contexts where an adress can be optionnaly provided, to mean I don't care.

For example, when you actively open a TCP connection to some TCP server (a TCP server is created by a passive TCP open), you need to specify the address of the TCP server (IP and port number), and you can optionally choose a local address. (Because your socket is not a server socket, nobody can open a connection to it and the address on your side of the connection is usually not very important.)

The bind system call is used to choose the local address of a TCP socket. The data passed to bind is really a set of constraints: constraint on IP address, constraint on TCP port. The usual text notation is IP:port. 0.0.0.0:0 means any IP any port is acceptable, it is the null constraint. 0.0.0.0:20 means local port must be 20, any IP is acceptable (0.0.0.0:20 is used by conforming FTP servers for data connection in active mode).

The TCP/IP subsystem will choose the address for you side of the TCP connection if you don't choose one, using the routing table, based on the destination address: the local TCP socket address will be the local address associated with the route corresponding to the destination address.

I previously mentioned the shell "meta-character" *, but the power of combining meta-characters with characters like *foo* (any file name containing "foo") doesn't exist with IP address constraints, it's all or nothing: either a single IP address is deemed acceptable or all addresses are. Logic does not dictates that it must be this way. You could extend the interface with a richer constraint language.

Precision:

The sentence 127.x.y.z means "here" doesn't imply that all these addresses are the same. They represent different "locations" (socket addresses), inside the local "computer", inside the local IP stack actually.

Remark: a computer with virtualization (emulation, hardware virtualization, paravirtualization, whatever you can think of...) has multiple independent IP stacks.

  • When I typed a random address starting with 127 (e.g 127.16.23.42) in my browser, it didn't connect to localhost. – whoKnows Aug 3 '15 at 21:02
  • @whoKnows Of course. localhost is 127.0.0.1, not 127.16.23.42. – curiousguy Aug 3 '15 at 21:47
  • I see. So 127.0.0.1 means localhost, but 127.x.y.z could mean the local computer, if it was set up that way? – whoKnows Aug 3 '15 at 21:56
  • 2
    @whoKnows No: 127.x.y.z belongs to this computer for any x y z. "localhost" is just a name for 127.0.0.1, so forget "localhost". Use the -n or -d flag of netstat and related programs to show numerical addresses, not names. 127.0.0.1 is not the same IP adress as 127.16.23.42. So 127.0.0.1:80 and 127.16.23.42:80 represent distinct TCP endpoint addresses, a webserver at the one address cannot be found at the other address. – curiousguy Aug 3 '15 at 23:45

Typically you use bind-address 0.0.0.0 to allow connections from outside networks and sources. Many servers like MySQL typically bind to 127.0.0.1 allowing only loopback connections, requiring the admin to change it to 0.0.0.0 to enable outside connectivity.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.