Article directory
-
-
- What is 127.0.0.1
- what is ping
- The difference between TCP sending data and ping
- Why can I still ping 127.0.0.1 when the network is disconnected?
- What is the difference between ping the loopback address and ping the local address?
- Is there any difference between 127.0.0.1 and localhost and 0.0.0.0
- Summarize
-
- The difference between telnet and ping
- the difference
-
-
- 1. Different subjects
- Two, different characteristics
- 3. Different purposes
-
If you goddess loves you, if you ask her, she may not tell you.
But Netcom is not available, you can know it by ping
.
You may know the answer when you see the title, but do you understand the reason behind it?
So what happens if you replace 127.0.0.1
with 0.0.0.0
or localhost
? Do you know the difference between these IP
?
Not much to say, let’s drive directly.
Unplug the network cable and disconnect the network.
disconnect wifi
Once the network cable is unplugged, the grievances are gone
Then enter ping 127.0.0.1
in the console.
PING 127.0.0.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.080 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.093 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.074 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.079 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.079 ms ^C --- 127.0.0.1 ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.074/0.081/0.093/0.006 ms
Explanation, unplug the network cable, ping 127.0.0.1
is can be pinged.
In fact, seeing this article here, the first half of the question in the title has already been answered. But we can think a little deeper.
Why can ping
pass 127.0.0.1
when the network is disconnected?
Does this mean that you can surf the Internet without paying for it?
No.
First of all, we need to enter the basic science popularization link.
Students who don’t understand will understand after reading it, and if they understand it, they should check for omissions and fill in the gaps.
What is 127.0.0.1
First, this is an IPV4
address.
IPV4
address has 32
bits, one byte has 8
bits, total 4
bytes.
Among them, the addresses starting with 127 belong to the loopback address, which is also a special address of IPV4
. There is no reason, it is artificially stipulated.
And 127.0.0.1
is one of the many loopback addresses. The reason why it is not 127.0.0.2
but 127.0.0.1
is because it is defined in the source code, and it does not make sense.
/* Address to loopback in software to local host. */ #define INADDR_LOOPBACK 0x7f000001 /* 127.0.0.1 */
loopback address
The address of IPv4
is 32
bits, 2 to the 32nd power, which is about 4 billion + billion
. The population of the earth alone is 7.6 billion, and the amount of 4 billion IPs is not enough to fit between teeth. In fact, IPs are indeed used up.
So there is IPV6
, the address of IPv6
is 128
bits, which is about 2 to the 128th power ≈10 to the 38th time Party. It is said that the amount of sand on the earth is about 10 to the 23rd power, so the IP of IPV6 can be considered endless.
IPV4 is in groups of 8 bits, and each group is separated by ..
IPV6 uses 16 bits as a group, and each group is separated by :. If it is all 0, then it can be omitted.
ipv6 loopback address
The loopback address under IPV4 is 127.0.0.1
, and under IPV6
, it is expressed as ::1
. The consecutive 0 is omitted in the middle, the reason why it is not 7 colons but 2 colons: is because an IPV6 address contains Only ? times two consecutive colons are allowed.
One more thing: the ping 127.0.0.1 command is used under IPV4. Under IPV6, the ping6 ::1 command is used.
What is ping
Ping is an application layer command, which can be understood as it belongs to the same layer as games or chat software. It’s just that the chat software can send and receive messages, and can also like and so on. It has many complicated functions. As a small software, ping has a relatively simple function, which is to try to send a small message to the target machine to determine whether the target machine is reachable. Determine whether the target machine network can be connected.
The bottom layer of the ping application uses the ICMP protocol of the network layer.
IP, ICMP and Ping layer
Although the ICMP protocol and the IP protocol both belong to the network layer protocol, but in fact ICMP also uses the IP protocol for message transmission.
The relationship between ip and icmp
Therefore, everyone here can simply understand that pinging a certain IP is to send a message to a certain IP address.
The difference between TCP sending data and ping
Under normal circumstances, we will use TCP for network data transmission, so we can see the difference between it and ping.
Ping and other application layer software belong to the application layer.
Then let’s compare it horizontally. For example, chat software, if it uses TCP to send messages.
In order to send a message, you must first know where to send it. Everything in Linux is a file, so the destination you want to send a message to is also a file, which leads to the concept of socket.
To use a socket
, it needs to be created first.
The way to create in TCP transmission is socket(AF_INET, SOCK_STREAM, 0);
, where AF_INET
means that host:port in IPV4 will be used way to resolve the network address you will enter later. SOCK_STREAM
refers to the use of byte stream-oriented TCP protocol, works at the transport layer.
After creating the socket
, you can happily write the data to be transmitted into this file. During the process of calling the socket’s sendto
interface, the process will enter the kernel state from user mode, and finally call the sock_sendmsg
method.
Then enter the transport layer, with the TCP
header. After a series of operations such as the IP
header on the network layer and the MAC
header on the data link layer. Enter the send queue ring buffer of the network card, and send it along the network card.
Going back to ping
, the whole process is basically similar to sending data by TCP
, the main difference is that socket
is created when socket
is created >socket(AF_INET,SOCK_RAW,IPPROTO_ICMP), SOCK_RAW
is a raw socket, works at the network layer, so build ICMP
(Network layer protocol) data, is perfect. After entering the kernel mode, ping also calls the sock_sendmsg
method at the end. After entering the network layer, add ICMP and IP headers, and add MAC to the data link layer Header is also sent along the network card. Therefore, in essence, there is not much difference in the program flow between ping and ordinary applications sending messages.
This also explains** why when you suspect that there is a problem with the network, the first thing people ask you is if you can ping it? **Because it can be simply understood that ping means that you have assembled a data packet yourself, and let the system send it out according to the path that other software sends data. If it can pass, it means that the data sent by other software can also pass.
Why can I still ping 127.0.0.1 even when the network is disconnected
As mentioned earlier, when there is a network, ping sends the data through the network card at the end.
Then when the network is disconnected, the network card is not working, but everything is normal when pinging the loopback address. We can see the working principle in this case.
ping loopback address
From the application layer to the transport layer to the network layer. This path is almost the same as when pinging the external network. At the network layer, the system will obtain the corresponding routing information in the routing table according to the destination IP, and this includes selecting which network card to send the message.
When it is found that the target IP is an external network IP, it will be sent from the “real network card”.
When it is found that the target IP is a loopback address, it will select the local network card.
The local network card is actually a **”fake network card”, it does not have a ring buffer
like the “real network card”, the “fake network card” will push the data to a < In the linked list of code>input_pkt_queue. This linked list is actually shared by all network cards, and various messages sent to this machine are hung on it. After the message is sent to this linked list, another soft interrupt** will be triggered.
The tool man “ksoftirqd” (this is a kernel thread) specialized in handling soft interrupts, after receiving soft interrupts, it will immediately fetch the message from the linked list, and then The data link layer, network layer, etc. are passed up layer by layer and finally given to the application program.
Tool man ksoftirqd
Ping the loopback address and send data to the loopback address via various protocols such as TCP both follow this path. The entire path from sending to receiving does not pass through the “real network card”. **The reason why 127.0.0.1 is called the local loopback address can be understood as, if the message is sent to this address, it will not go out of the network, and it will come back after switching on the local machine. **So even if the network is disconnected, you can still ping
pass 127.0.0.1
.
What is the difference between ping the loopback address and ping the local address
We execute ifconfig
in mac.
$ifconfig lo0: flags=8049<UP, LOOPBACK, RUNNING, MULTICAST> mtu 16384 inet 127.0.0.1 netmask 0xff000000 ... en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 inet 192.168.31.6 netmask 0xffffff00 broadcast 192.168.31.255 ...
You can see lo0, which means the local loopback interface, and the corresponding address is the 127.0.0.1 we mentioned earlier, which is the loopback address.
and eth0, which means the first network card of this machine, and the corresponding IP address is 192.168.31.6, which is called local IP.
Before, I always thought that if I pinged the local IP, it would go out through the “real network card”, and then encounter the first router, and then send it back to the local machine.
In order to verify this statement, packet capture can be performed, but the result is not the same as the above statement.
ping 127.0.0.1
ping local address
It can be seen that pinging the local IP is the same as pinging the loopback address, and the relevant network data all go through lo0, the local loopback interface, which is the aforementioned **”fake network card”**.
As long as the local loopback interface is used, the data will not be sent to the network, and it will be sent back after going around in the local network protocol stack. Therefore, there is no difference between ping the loopback address and pinging the local address.
Is there any difference between 127.0.0.1 and localhost and 0.0.0.0
Going back to the question in the animation at the beginning of the article, it can be regarded as an old frequent visitor in interview stereotyped essays.
When I used nginx
for the first time, I found that I could access the welcome page of nginx
normally with these IP
. At one time, I thought that these IP
were all the same.
Visit 127.0.0.1:80
Visit localhost:80
Visit 0.0.0.0:80
Access the IP address of this machine
But there are still some differences in essence.
First of all, localhost
is not called IP
, it is a domain name, just like "baidu.com"
, it is a form of things, but the default It will be resolved to 127.0.0.1
, of course this can be modified under the /etc/hosts
file.
So by default, there is really no difference between using localhost
and using 127.0.0.1
.
The second is 0.0.0.0
, executing ping 0.0.0.0 will fail, because it indicates an invalid destination address in IPV4
.
$ ping 0.0.0.0 PING 0.0.0.0 (0.0.0.0): 56 data bytes ping: sendto: No route to host ping: sendto: No route to host
But it is still very useful. Recall that when we start the server, we usually listen
an IP and port, waiting for the connection from the client.
If listen
is the local 0.0.0.0
at this time, then it means all IPV4 addresses on the local machine.
/* Address to accept any incoming messages. */ #define INADDR_ANY ((unsigned long int) 0x00000000) /* 0.0.0.0 */
for example. The 127.0.0.1
and 192.168.31.6
mentioned just now are both the IPV4 address of this machine. If you listen to 0.0.0.0
, then use the above Both addresses can access this server.
Of course, 0.0.0.0
cannot be used when the client connect
. You must specify which server IP to connect to.
Summary
127.0.0.1
is the loopback address.localhost
is domain name, but it is equal to127.0.0.1
by default.- The
ping
loopback address is the same as theping
local address, and the lo0 “fake network card” will go through the network layer and data link Finally, turned a hard corner before leaving the network card, inserting data into a linked list and then notifying soft interrupt ksoftirqd to carry out the logic of receiving data, doesn’t leave the network at all. So even if the network is disconnected, you canping
connect to the loopback address. - If the
listen
of the server is0.0.0.0
, then127.0.0.1
and the local address can be used access to the service.
The difference between telnet and ping
ping www.baidu.com
The result is as follows:
Try telnet Baidu’s http port:
telnet www.baidu.com 80
The result is as follows;
Try to telnet Baidu’s ftp port:
telnet www.baidu.com 21
The result is as follows:
Difference
1. Different subjects
1. ping: ICMP protocol, ICMP only contains control information, no port. Internet packet explorer, a program for testing network connection volume.
2. telnet: TCP protocol, with port, can carry data, is the standard protocol and main method of Internet remote login service.
Second, different characteristics
1. Ping: It is a service command working in the application layer of the TCP/IP network architecture, mainly to send ICMP Echo request messages to specific destination hosts.
2. telnet: Provides users with the ability to complete remote host work on the local computer. Use the telnet program on the end user’s computer to connect to the server.
Third, different purposes
1. Ping: It is used to determine whether the local host can successfully exchange (send and receive) data packets with another host, and then according to the returned information, it can be inferred whether the TCP/IP parameters are set correctly.
2. Telnet: You can control the server locally. To start a telnet session, a username and password must be entered to log into the server. Telnet is a commonly used method for remotely controlling a Web server.