This tutorial explains how to use the ping command in Linux and Windows. Learn what the ping command is used for and how the ping command works.
What is the ping command?
Ping stands for Packet InterNet Groper. Ping is a software application, utility, or tool that is used to test and diagnose connectivity related issues on a network. It is based on the ICMP protocol. ICMP protocol is part of IP standards and available in all IP networks. Since the ping command is based on the ICMP protocol, it is also available and supported in all IP networks.
What is the ping command used for?
Ping command is used for testing connectivity between hosts and debugging connectivity related issues on an internetwork. It allows network administrators to test whether a remote device is reachable or whether the network connection for a local device is properly configured and installed.
Which protocol does the ping command?
The ping command uses ICMP protocol. ICMP protocol allows devices to exchange messages about various network conditions. It contains three types of messages: errors, query, and response.
Error messages are used to inform the sender about a condition or cause that prevents the sender's packets from reaching their destination. Query and response messages are used to exchange information about various network conditions.
Query and response messages are always used in pairs where one device sends a query message and another sends a reply or response of the query message. The ping command uses query-response messages.
How does the ping command work?
On the source device, a user specifies a ping destination. The ping application creates an IP datagram and gives it to the IP software of the source computer. The IP software checks the datagram. If the datagram is correct, it checks an entry in its forwarding table for the destination. If the forwarding table has an entry for the destination device, it forwards the datagram to the destination. If the forwarding table does not contain an entry for the destination, it uses ARP (on IPv4) or Neighbor Discovery (on IPv6) to locate the default router. If the default router is connected, it forwards the datagram to the default router. The default router uses its routing table to find a path to the destination. If the path contains multiple intermediate routers, they all use their routing logic to forward the datagram to the destination. Finally, the destination host receives the datagram. If ICMP and IP modules on the destination host are working properly, the destination host creates a reply datagram and sends it back to the source host. Reply datagram follows the same steps and procedure to reach the source host.
If the source receives the reply datagram, it is considered a successful ping. A successful ping means that both hosts have proper connectivity and if the path has any routers, they are also running fine.
Ping command variants
The original version of the ping was written in 1983 by Mike Muuss at the University of California at Berkeley. Since then, several variants of the ping command have been created and implemented.
Some variants send only four packets and exit while others send consecutive packets until they are asked to stop. Most variants allow users to specify whether to send a request and wait for a reply or send a series of requests and wait. In the end, all variants display statistics about message loss or success and report the amount of time it takes for packets to return.
Usually, using the ping command without any option is sufficient to indicate that a problem exists, but it is not always sufficient to pinpoint problems. To help identify the exact cause of the problem, almost all variants support several options.
These options allow us to control how the ping command should work. For example, we can specify the amount of data being sent in each packet, the number of packets that should be sent, arguments in the packet header, a delay between packets sent, the number of times to retry, etc.
This controlled implementation helps debug several connectivity related issues such as the network path that can't handle big size packets, hosts that have low buffer size, systems that can't handle lots of traffic.
Differences between Linux and Windows implementation of the ping command
In Windows, the ping command uses a 32 bytes long message and by default sends only four messages. The system that receives ping messages adds an 8-byte timestamp to messages and sends them back to the sender. The timestamp is used to calculate round-trip delays through the network.
In Linux, the ping command uses a 64 bytes long message and by default sends continuous messages until it is asked to stop. It assigns a sequence number to each message and reports when it receives the response of the message. Replies are not necessarily to be received in the same order the messages were sent out.
Apart from the default implementation, there are also differences between the supported options in both versions. Let's understand what the ping command options are and how they are used in Windows and Linux.
The ping command in Windows
To use the ping command in Windows, access the command prompt. On the command prompt, type "ping" and specify the IP address, hostname, or URL of the remote system and press the Enter key.
For example, to test whether Google is up and accessible from the local Windows system, use the following command.
ping www.google.com
The following image shows the output of this command.
To view all options that the ping command supports in Windows, use the following command.
ping /?
The following image shows the output of the above command.
The following table provides a brief description of the most common options of the ping command in Windows.
Option | Description |
-n | Specify the number of messages |
-l | Set the size of the buffer |
-4 | Force using IPv4 |
-6 | Force using IPv6 |
Let's understand these options through examples.
The following command sends 5 messages of buffer size 1000 bytes.
ping -n 5 -l 1000 www.google.com
The following image shows the output of the above command.
By using the -4 and -6 options, you can force the ping command to send IPv4 and IPv6 packets, respectively. The following image shows how to use these options.
The ping command in Linux.
To use the ping command in Linux, access the shell prompt, and use the following syntax.
$ping [IP address, hostname, or URL of the remote system]
For example, to ping www.google.com, use the following command.
$ping www.google.com
The ping command in Linux sends messages continuously. To stop the ping command from sending more messages, press the ctrl+c keys.
The following image shows the output of the above command.
The ping command supports more options and arguments in Linux than it supports in Windows. The following table lists the most common options of the ping command in Linux.
Option | Description |
-i | Set the interval between packets |
-c | Specify the number of packets to be sent |
-f | Send packets as much and fast as the network allows |
-q | Display only static or summary |
Let's understand these options through examples.
By default, the ping command sends messages in an interval of one second. You can increase or decrease this interval by using the -i option.
The following command sets the time interval to 4 seconds.
$ping -i 4 www.google.com
The following command sets the time interval to 0.5 seconds.
$ping -i 0.5 www.google.com
The following image shows the output of both commands.
To send only a specific number of messages, use the option -c. For example, the following command sends only 4 packets.
$ping -c 4 www.google.com
The following image shows the output of the above command.
To test the transmission speed of the network, you can use the -f option. The -f option forces the ping command to send messages as fast as the network allows. Only the superuser or root user can use this option.
#ping -f www.google.com
The output of this command doesn't display all detail of every message it sent. It prints a dot (.) when a packet is sent and removes that dot (.) when the response of that packet is received. When you press ctrl+c, it will display the static of all messages it sent.
The following image shows the output of the above command.
If you are pinging a host that does not belong to your network, sometimes this option may not work. Since this option forces the ping command to send too many packets in a very short period, hosts or servers on the public network may consider the use of this option as a DoS attack and block it through a firewall. For example, as you can see in the image above, the ping command sent 1106 packets in just 41ms.
You can also use the ping command to resolve the hostname. To know the IP address of the remote system, use the following command.
$ping -c 1 www.google.com
The following image shows the output of the above command.
If you don't want to view the reply of every sent packet or you simply want to see the summary of all packets, you can use the -q option. For example, the following command sends 10 packets and displays a summary only.
$ping -c 10 -q www.google.com
The following image shows the output of the above command.
To view a complete list of all supported options, use the following command.
$ping --help
The following image shows the output of the above command.
Testing a loopback interface
TCP/IP protocol stack provides a loopback interface. To test whether the TCP/IP stack is properly implemented on the local system, you can send a ping request to the loopback interface. If the loopback interface replies to ping messages, the TCP/IP protocol stack is properly configured. The IP addresses of the loopback interface are 127.0.0.1 and ::1 in IPv4 and IPv6, respectively.
To test the IPv4 implementation, use the following command.
ping 127.0.0.1
To test the IPv6 implementation, use the following command.
ping ::1
Instead of IP addresses, you can also use the hostname of the system.
ping localhost
The following image shows the output of the above commands.
That's all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social network.
No comments:
Post a Comment