Oct 7, 2023 4 min read

How to Use Ping Command on Linux

Use Ping Command on Linux with our step-by-step tutorial. It is a simple tool for testing network connectivity.

Ping Command on Linux
Table of Contents

Introduction

Before we begin talking about how to use Ping Command on Linux. Let’s briefly understand - What is Ping Command?

The Ping command is a simple tool for testing network connectivity. With the ping command, you can determine if a device is reachable, measure the round-trip time for data packets, and troubleshoot network issues.

By sending a small packet of data and waiting for the response, ping helps identify network congestion or problems with specific devices. It is essential for network administrators to diagnose and resolve connectivity problems quickly. Learn more about how to use the ping command effectively for troubleshooting in this concise guide.

In this tutorial, you will use the ping command on Linux. We will also address a few FAQs on how to use Ping Command on Linux.

Advantages of Ping Command

  1. Network Troubleshooting: Ping helps identify connectivity issues, packet loss, and network congestion.
  2. Device Reachability: Verify if devices are reachable on a network, ensuring reliable communication.
  3. Latency Measurement: Measure round-trip time to assess network performance and response times.
  4. Diagnose DNS Problems: Pinpoint DNS resolution issues by pinging domain names.
  5. Verify Network Configurations: Validate firewall rules, routing tables, and network settings using ping for accurate network setup.

How to Use the Ping Command

1) The syntax for the ping command is as follows:

ping [OPTIONS] DESTINATION

2) To see how ping command works, let's check out this quick example below:

ping google.com

The output will look like this:

Output

PING google.com (172.217.22.206) 56(84) bytes of data.
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=1 ttl=53 time=40.2 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=2 ttl=53 time=41.8 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=3 ttl=53 time=47.4 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=4 ttl=53 time=41.4 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7ms
rtt min/avg/max/mdev = 40.163/42.700/47.408/2.790 ms

The ping command resolves a domain name, into an IP address. It then starts sending ICMP packages to destination IP. If the destination IP is reachable, it will respond back. Further, the ping command will print an output line including the below fields:

  • The number of the data bytes.
  • The IP address of the destination.
  • ICMP sequence number for every packet.
  • The Time to Live - ttl=53?
  • The ping time, measurable in milliseconds. It is the round trip time for the packet to reach the host. As well as the response to return to sender.

There is a one-second interval between sending a new packet.

If you want to stop the command, press Ctrl+C. It will stop receiving the packages the moment it faces any interruption. You can see the percentage of packet loss in the stats once the command stops. You can get the complete data of where the package loss happened using the traceroute command.

If you didn't get a reply, then it doesn't always mean that the destination IP is not active. Sometimes hosts have a firewall that blocks the ICMP traffic, or maybe they are set not to respond to the ping requests.

Ping command exits with code 0 if successful, otherwise it exists with code 1 or 2. These codes can be helpful when using the ping command in the shell.

Specifying the Number of Packets

1) Now that you are aware, ping command sends packages until it gets some interruption. So if you want to limit the number of packets to be sent, use -c option followed by the number of packages that you want to send.

For more clarification, say if you want to ping vegastack.com 3 time, you should use the following command:

ping -c 3 vegastack.com

Specify the Source Interface

There is a default route through which ping send the packages. If you have multiple interfaces and you want to specify any particular interface, use the -I option.

The below command will ping vegastack.com. It will be using em2 as a source interface:

ping -I em2 vegastack.com

Specifying the Internet Protocol

Ping command either use IPV4 or IPV6 protocol, it will depend on your machine's DNS settings.

If you want to force ping to use IPV4, just type -4 or ping4:

ping -4 DESTINATION

2) For IPv6, pass -6 option or use the ping6:

ping6 DESTINATION

FAQs to Use Ping Command on Linux

What does it mean if I receive a "Destination Host Unreachable" error? 

This error indicates that the destination device or IP address is not reachable. It could be due to network connectivity issues, incorrect IP address, or a firewall blocking the connection.

Can I use the ping command to test internet connectivity? 

Yes, you can ping a well-known IP address like 8.8.8.8 (Google's DNS server) to check if you have internet connectivity. If you receive a response, it means you are connected to the internet.

How can I stop the ping command?

To stop the ping command on Linux, press Ctrl + C in the terminal. This will terminate the ongoing ping process and display the final ping statistics.

Is it possible to adjust the number of ping sent? 

Yes, by default, ping sends four ICMP echo request packets. You can modify this using the "-c" option followed by the desired number of packets. For example, "ping -c 10 google.com" will send 10 ping packets.

What does "Request Timed Out" mean? 

If you see "Request Timed Out" in the ping results, it means the destination device did not respond within the specified timeframe. It could indicate a network problem or the device blocking ICMP requests.

Can I perform continuous ping tests? 

Yes, you can use the "-t" option to continuously send ping requests until manually stopped. For example, "ping -t 8.8.8.8" will keep sending ping packets until interrupted.

How can I resolve an "Unknown host" error? 

An "Unknown host" error indicates that the domain name is not recognized. Ensure you have spelled the domain correctly or try using the IP address instead. It could also indicate DNS server issues.

Conclusion

We hope this detailed tutorial helped you to use Ping Command on Linux.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.