If the network is disconnected, can you still ping 127.0.0.1? The difference between telnet and ping

If your goddess loves you , if you ask her, she may not tell you.

But if the Netcom is not connected , you pingwill know immediately.

You may know the answer when you see the title, but do you understand the reason behind it?

So what happens if you 127.0.0.1replace it with 0.0.0.0or ? Do you know the difference between localhostthese few ?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 it in the console ping 127.0.0.1.

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

It means that the network cable can be unplugged, ping 127.0.0.1and it 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 I still pingget through when the network is disconnected 127.0.0.1?

Does this mean that you can surf the Internet without paying Internet fees?

cannot.

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, it's an IPV4address.

IPV4The address has 32bits, a byte has 8bits, a total 4of bytes.

Among them, those starting with 127 belong to the loopback address , which is also IPV4a special address of , which makes no sense and is artificially stipulated.

127.0.0.1It is one of many loopback addresses. The reason is not 127.0.0.2, but 127.0.0.1because it is defined in this way in the source code, and it doesn't make sense.

/* Address to loopback in software to local host.  */
#define    INADDR_LOOPBACK     0x7f000001  /* 127.0.0.1   */

pictureloopback address

IPv4The address is 3232 bits, 2 to the 32nd power, probably 40+亿. The population of the earth alone is 7.6 billion, and the amount of 4 billion IPs is not enough to fit between the teeth . In fact, the IPs are indeed used up .

So there is IPV6, IPv6the address of is 128bits, which is about 2 to the 128th power ≈ 10 to the 38th power . 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 a group of 8 bits, and each group is separated by a dot .

IPV6 uses 16 bits as a group, and each group is separated by a : sign. If it is all 0, then it can be omitted.

pictureipv6 loopback address

The loopback address under IPV4 is 127.0.0.1, IPV6under , expressed as ::1. The consecutive 0s are omitted in the middle . The reason why there are not 7 colons but 2 colons: is because only two consecutive colons are allowed to appear in an IPV6 address .

One more thing: the ping 127.0.0.1 command is used under IPV4 . The ping6::1 command is used under IPV6 .

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. It is to try to send a small message to the target machine to determine whether the target machine is reachable . In fact, it is to determine whether the network of the target machine can be connected.

The bottom layer of the ping application uses the ICMP protocol of the network layer .

pictureLayers of IP, ICMP and Ping

Although both the ICMP protocol and the IP protocol belong to the network layer protocol , in fact ICMP also uses the IP protocol for message transmission .

pictureThe 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.

insert image description here

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 socket, it first needs to be created.

The way to create it in TCP transmission is socket(AF_INET, SOCK_STREAM, 0);, where AF_INETindicates that the host:port method in IPV4 will be used to resolve the network address you will enter later. SOCK_STREAMIt refers to the use of byte stream-oriented TCP protocol and works at the transport layer .

After creating socket, you can happily write the data to be transferred into this file. sendtoIn the process of calling the socket interface, the process will enter the kernel state from the user state , and finally call sock_sendmsgthe method.

Then enter the transport layer, bring TCPthe head. After a series of operations, the network layer brings IPthe head, and the data link layer brings MACthe head. Enter the sending queue ring buffer of the network card , and send it along the network card.

Going back ping, the whole process is basically TCPsimilar to sending data. The main difference is that the original socket is used when creating and works at socketthe network layer , so it is very suitable to construct data (network layer protocol) up. After entering the kernel mode, ping is also the method called at the end . After entering the network layer and adding ICMP and IP headers , the data link layer adds MAC headers , which are also sent along the network card. Therefore, in essence, there is not much difference in the program flow between ping and ordinary application sending messages.socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)SOCK_RAWICMPsock_sendmsg

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 ping 127.0.0.1 even when the network is disconnected?

As mentioned earlier, when there is a network, ping finally sends the data through the network card .

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.

pictureping the 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 the target IP is found to be a loopback address , the local network card will be selected .

The local network card is actually a **"fake network card" , it does not have something like a "real network card" ring buffer, and the "fake network card" will push data to a linked listinput_pkt_queue called . 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 ) that specializes in handling soft interrupts, it will immediately go to the linked list to take out the message after receiving the soft interrupt, and then pass it up along the data link layer, network layer, etc. Finally given to the application.

pictureTool man ksoftirqd

This path is used to ping the loopback address and send data to the loopback address through various protocols such as TCP . 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, it still pingworks 127.0.0.1.

What is the difference between ping the loopback address and ping the local address?

We execute it in mac ifconfig.

$ 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 represent the first network card of this machine, and the corresponding IP address is 192.168.31.6 , which is called the 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.

pictureping 127.0.0.1

pictureping 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 pinging 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 for the first time before nginx, I found that IPI could access nginxthe welcome page of . At one point I thought they IPwere all the same.

pictureAccess 127.0.0.1:80

pictureVisit localhost:80

pictureaccess 0.0.0.0:80

pictureAccess the IP address of the machine

But there are still some differences in essence.

First of all, localhostit is not called IP, it is a domain name, just like "baidu.com", is a form of things, but it will be resolved to by default 127.0.0.1, of course, this can /etc/hostsbe modified under the file.

So by default, there is really no difference between using localhostand using .127.0.0.1

The second is 0.0.0.0that executing ping 0.0.0.0 will fail because it IPV4indicates an invalid destination address in .

$ 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 provide listenan IP and port, waiting for the connection from the client.

If listenat this time is the local machine , it represents all the IPV4 addresses0.0.0.0 on the local machine .

/* Address to accept any incoming messages. */
#define    INADDR_ANY      ((unsigned long int) 0x00000000) /* 0.0.0.0   */

for example. 127.0.0.1The and just mentioned 192.168.31.6are both the IPV4 addresses of the local machine. If you monitor 0.0.0.0, you can use the above two addresses to access the server.

Of course, the client connectcannot be used 0.0.0.0. You must specify which server IP to connect to.

Summarize

  • 127.0.0.1is the loopback address . localhostis the domain name , but defaults to equals 127.0.0.1.
  • pingThe loopback address and pingthe local machine address are the same. The lo0 "fake network card" will go through the logic of the network layer and data link layer, and finally make a sharp turn before the network card is about to exit , and insert the data into a After the linked list, the soft interrupt will notify ksoftirqd to carry out the logic of receiving data , and it will not go out of the network at all . Therefore, the loopback address can be passed even if the network is disconnected ping.
  • If the server listenis 0.0.0.0, then both the user 127.0.0.1and the local address can access the service at this time.

The difference between telnet and ping

ping www.baidu.com

The result is as follows:

img

Try telnet Baidu's http port:

telnet www.baidu.com 80

The result is as follows;

img

Try to telnet Baidu's ftp port:

telnet www.baidu.com 21

The result is as follows:

img

the 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 ports, capable of carrying data, is the standard protocol and main method of Internet remote login service.

Two, 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.

3. 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.

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/132122371