More wonderful articles.

Microservices are not all, just a subset of specific domains

Selection and process should be careful, or it will get out of control.

With all these monitoring components, there’s always one that’s right for you

The Most Common set of “Vim” Techniques for Linux Production

What the hell are we developing with Netty?

Most useful series:

The Most Common set of “Vim” Techniques for Linux Production

The most Common set of “Sed” Techniques for Linux Production

The most Common set of “AWK” Techniques for Linux Production

Welcome Linux and Java backend students to follow the public account.

1. Problem introduction

Previously, when using Tomcat, tomcat was bound to 127.0.0.1 by default. Recently, when using hexo to blog, I found that when using hexo server to start services, the IP address was bound to 0.0.0.0. So what’s the difference between these two IP addresses?

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

2. IP address classification

2.1 IP address

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 id of the network where the IP address resides. Host-id: indicates the number of a host on the network where the IP address resides.

That is:

  IP-address ::=  { <Network-number>, <Host-number> }
Copy the code

2.2 IP address Classification

IP addresses are classified into five categories, namely A to E. They are classified 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 bit of the network id is always 0.
  • Class B address: The network number is 2 bytes. The first two digits of the network number are always 10.
  • Class C address: The network number is 3 bytes. The first three fixed bits of the network number are 110.
  • Class D addresses: The first four bits are 1110, which are used for multicast, or one-to-many communication.
  • Class E addresses: the first four digits are 1111, reserved for later use. Among them, ABC addresses are unicast addresses, which are used for one-to-one communication and are the most commonly used.

2.3 Special IP addresses

Special IP addresses are used to do special things. RFC1700 defines the following special IP addresses.

  1. {0,0}: The network ID and host ID are both 0, which indicates the local host on the local network and can only be used as the source IP address.
  2. {0, host-id}: indicates a host on the local network. Can only be used as a source address.
  3. {-1,-1} : Indicates that all bits of the network ID and host ID are 1 (binary). It is used for the broadcast 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}: directly broadcasts to the specified subnetwork of the specified network. Only the destination address.
  6. {net-id,-1,-1}: directly broadcasts to all subnetworks of the specified network. It can only be used as a destination address.
  7. {127,}: any IP address whose network number is 127. Are internal host loopbacks that can never appear on the network outside the host.

3. Problem solving

Let’s move on to the question we asked before: What’s the difference between 127.0.0.1 and 0.0.0.0 addresses? Let’s start with the common ground:

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

Let’s look at the two addresses separately:

0.0.0.0

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

  • In 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 to an address of 0.0.0.0, then the service can be accessed from both IP addresses.

  • In a route, 0.0.0.0 indicates the default route, that is, the route that corresponds to the routing table when no exactly matched route is found.

Purpose to summarize

  • When a host has not been assigned an IP address, it is used to represent the host itself. (When DHCP assigns IP addresses)
  • As the default route, it indicates any IPV4 host. Used to indicate that the target machine is not available.
  • As a server, it represents any IPV4 address on the local computer.

127.0.0.1

127.0.0.1 is one of the {127,} sets, and any address with network number 127 is called a loopback address, so loopback address! =127.0.0.1, they are containment relationships, that is, the loopback address contains 127.0.0.1. Loopback address: All packets sent to this address should be loopback.

use

  • A loopback test is used to test whether the network device, operating system, or TCP/IP implementation on a machine is working properly using ping 127.0.0.1.
  • DDos attack defense: After receiving DDos attacks, A website records domain name A as 127.0.0.1, allowing attackers to attack itself.
  • The native address that most Web containers are bound to when testing.

localhost

Localhost has more meaning 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 computers we use, localhost points to 127.0.0.1. On Ubuntu, the /ets/hosts file will contain the following:

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
Copy the code

The first line above is the default configuration found on almost every computer. But the meaning of localhost is not limited to 127.0.0.1.

Localhost is a domain name that refers to this computer or this host and can be used to obtain network services running on the computer. On most systems, localhost is pointed to 127.0.0.1 for IPV4 and ::1 for IPV6.

127.0.0.1 localhost: : 1 localhostCopy the code

Therefore, it is important to check whether IPV4 or IPV6 is used

4. To summarize

127.0.0.1 is a loopback address. Does not mean “native”. 0.0.0.0 really means “local machine in this network”. In practice, we can select 0.0.0.0 when we bind the port on the server, so that the user of my service can access my service through multiple IP addresses.

For example, I have A server with an Internet address A and an Intranet address B. If the bound port is specified as 0.0.0.0, my application can be accessed through either the Intranet address or the Internet address. But if I only bind the Intranet address, then I can’t access it through the Internet address. Therefore, if you bind 0.0.0.0, there are security risks. You can bind only Intranet addresses to services that require Intranet access.