1. Introduction of problems

In the past, tomcat was bound to 127.0.0.1 by default when starting tomcat. Recently, while blogging with Hexo, I found that when starting tomcat with hexo server, the bound IP address was 0.0.0.0. So what’s the difference between these two IP addresses?

Before explaining the differences between the two addresses, let’s review the basics of IP addresses.

2. IP address classification

2.1 IP Address Representation

An IP address consists of two parts, net-id and host-id, that is, the network ID and the host ID. Net-id: indicates the network ID of the IP address. Host-id: indicates the number of a host in the network where the IP address resides.

That is:

Ip-address ::= {< network-number >, < host-number >} Copy codeCopy the code

2.2 IP Address Classification

IP addresses are classified into five categories, namely, A to E, according to the byte length of their net-ID and the first few digits of their network number.

  • Class A address: the network number is 1 byte. The first digit of the network number must be 0.
  • Class B address: the network number is 2 bytes. The first two digits of the network number must be 10.
  • Class C address: The network number is 3 bytes. The first three digits of the network number are 110.
  • Class D addresses: The first four bits are 1110, used for multicast, or one-to-many communication.
  • Class E addresses: the first four bits are 1111, reserved for later use. Among them, ABC addresses are unicast addresses, which are used for one-to-one communication and are most commonly used.

2.3 Special IP Address

Special IP addresses are used to do special things. The following special IP addresses are defined in RFC1700.

  1. {0,0}: Both the network id and host ID are 0, indicating local host on the local network, which can only be used as the source address.
  2. {0, host-id}: indicates a host on the local network. Can only be used as the source address.
  3. {-1,-1} : indicates that all bits of the network id and host ID are 1 (binary). It is used for broadcasting on the local network and can only be used as the destination address. Packets sent to this address cannot be forwarded outside the network where the source address resides.
  4. {net-id,-1}: broadcasts directly to the specified network. It can only be used as a destination address.
  5. {net-id,subnet-id,-1}: broadcasts directly to the specified subnetwork of the specified network. Only used as destination address.
  6. {net-id,-1,-1}: broadcasts directly to all subnetworks of the specified network. It can only be used as a destination address.
  7. {127,}: indicates any IP address whose network id is 127. They are internal host loopbacks and must never appear on the network outside the host.

3. Problem solving

Let’s look at the question we asked earlier: What’s the difference between 127.0.0.1 and 0.0.0.0? Let’s start with the common ground:

  1. All belong to special addresses.
  2. These are all class A addresses.
  3. All IPV4 addresses.

Let’s take a look at these two addresses:

0.0.0.0

In IPV4, address 0.0.0.0 is used to indicate an invalid, unknown, or unavailable target.

  • On a server, 0.0.0.0 refers to all IPV4 addresses on the host. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a service on the host listens at address 0.0.0.0, then the service can be accessed from both IP addresses.

  • In routing, 0.0.0.0 indicates the default route, that is, the route that corresponds to a route that does not match perfectly in the routing table.

Purpose to summarize

  • When a host has not been assigned an IP address, it is used to indicate the host itself. (When DHCP assigns IP addresses)
  • Used as the default route, it stands for any IPV4 host. Used to indicate that the target machine is unavailable.
  • It is used as a server and represents any IPV4 address on the local machine.

127.0.0.1

127.0.0.1 belongs to the {127,} set, and all addresses with network number 127 are called loopback addresses. =127.0.0.1, they are containment relationships, i.e. the loopback address contains 127.0.0.1. Loopback address: All packets destined for this address should be loopback.

use

  • Loopback test: Use ping 127.0.0.1 to test whether the network device, operating system, or TCP/IP implementation on a machine is working properly.
  • DDos attack defense: After receiving DDos attacks, the website records domain name A as 127.0.0.1, allowing the attacker to attack itself.
  • The native address that most Web container tests bind to.

localhost

Localhost makes more sense than 127.0.0.1. Localhost is a domain name, not an IP address. The reason we often think of localhost as the same as 127.0.0.1 is because on most of our computers, localhost points to 127.0.0.1. In Ubuntu, the /ets/hosts file contains the following information:

127.0.0.1 localhost 127.0.1.1 jason-Lenovo-v3000 # The following lines are desirable for IPv6 capable hosts ::1 Ip6-localhost ip6-loopback fe00::0 ip6-localnet FF00 ::0 ip6-mcastPrefix FF02 ::1 ip6-allNodes FF02 ::2 IP6-allRouters Replication codes for the routersCopy the code

The first line above is the default configuration found on almost every computer. But localhost is more than just 127.0.0.1.

Localhost is a domain name for this computer or this host that you can use to get network services running on your computer. On most systems, localhost is pointed to 127.0.0.1 for IPV4 and ::1 for IPV6.

127.0.0.1 localhost ::1 Localhost replication codeCopy the code

Therefore, ensure that IPV4 or IPV6 is used

4. To summarize

127.0.0.1 is a loopback address. Does not mean “native”. 0.0.0.0 is the real “native on the local network”. In practice, when we bind the port on the server, we can choose to bind to 0.0.0.0, so that my service visitors can access my service through my multiple IP addresses.

For example, I have A server with an external IP address A and an internal IP address B. If the bound port is specified as 0.0.0.0, the application can be accessed using either the external or internal IP address. But if I only bind the Intranet address, then I can’t access it through the Internet address. Therefore, binding 0.0.0.0 also has security risks. For services that only need Intranet access, you can bind only Intranet addresses.

Here to share a system of Java books, the whole network spread over 10W+, suitable for small white to god of all kinds of needs are sorted out.

Java to master the interview with the most complete information package (including download methods)

Computer Classics essential Books: Computer Classics Required reading list (including download)

Well, that’s all for today’s article. See you next time.

Welcome to follow my official account: “Code farming Attack”, share Python, Java, big data, machine learning, artificial intelligence and other technologies, pay attention to code farming technology improvement, career breakthrough, thinking transition, 100,000 + code farming growth charge first stop, accompany you have a dream to grow together.