Keywords: ping | ICMP | DNS | hostname | URL
Abstract: This article explains the working principle of the ping command based on ICMP protocol, distinguishes between hostnames and URLs, and provides network configuration checks to help readers correctly understand and use ping.
Introduction
On a CentOS 7 virtual machine managed by VirtualBox and Vagrant, users encounter errors when executing ping http://google.com, such as "ping: http://google.com: Name or service not known". This article delves into the root causes and offers solutions through technical analysis.
ping Command and ICMP Protocol
The ping command uses the Internet Control Message Protocol (ICMP) to send echo request packets. ICMP is part of the TCP/IP protocol suite, operating at a lower network layer for diagnosing connectivity. For example, executing ping google.com first resolves the hostname “google.com” to an IP address (e.g., 61.91.161.217) via DNS, then sends ICMP packets. From the provided output, ping google.com successfully returns ICMP responses, but adding http:// prefix causes errors.
Difference Between Hostnames and URLs
The ping command only accepts hostnames (e.g., google.com) or IP addresses (e.g., 61.91.161.217), not full URLs (e.g., http://google.com or https://google.com). URLs include protocol prefixes (e.g., http:// or https://) for application-layer protocols like HTTP/HTTPS, while ping operates at a lower network layer. Thus, when users input ping https://google.com, the system attempts to resolve the entire string as a hostname, which DNS cannot recognize, leading to the "Name or service not known" error. This highlights the hierarchy of network commands: ping is based on ICMP, whereas tools like curl or browsers handle URLs.
Network Configuration Check and Troubleshooting
Referencing other answers, if pinging hostnames also fails, network configuration should be checked. In the provided example, the /etc/resolv.conf file configures DNS servers (e.g., 8.8.8.8 and 8.8.4.4), which is generally correct. However, network interfaces might not be properly activated. Commands like sudo nmcli d can be used to view interface status. If an interface shows as disconnected, use sudo nmtui to edit connections, enable the "Automatically connect" option, and reboot the system. Additionally, ensure VirtualBox network adapters are enabled, and configuration files like /etc/sysconfig/network-scripts/ifcfg-eth0 correctly set DNS and DHCP. For instance, in the sample configuration, eth0 uses DHCP with DNS1=8.8.8.8, aiding DNS resolution.
Code Examples and Parsing
Below are rewritten code examples illustrating correct ping usage. Assume executing in terminal: ping google.com This sends ICMP packets. Incorrect usage: ping http://google.com leads to resolution failure. Network configuration example: cat /etc/resolv.conf should display valid nameserver entries. For special characters in text, such as "<" and ">" in error messages, escape them in HTML to avoid parsing issues. For example, when outputting print("<T>") in a code block, write it as print("<T>") to ensure proper display.
Conclusion
When using the ping command correctly, avoid including protocol prefixes and use only hostnames or IP addresses. Ensuring proper network configuration, including DNS resolution and interface activation, can resolve common connectivity issues. This article provides practical troubleshooting guidance for system administrators and developers through technical analysis, emphasizing the hierarchical structure of network protocols.