ping command in Linux with examples

This ping command guide follows my previous 90 Linux Commands frequently used by Linux Sysadmins article. As time allows, I will continue to publish articles on the 90+ commands geared toward Linux sysadmins and Linux power users. Let’s continue this series with the ping command.

The Linux command line is a treasure trove of powerful tools that can help you manage your system and troubleshoot network-related issues efficiently. One such tool that every Linux user should have in their toolbox is the “ping” command. In this article, we’ll delve into the world of the “ping” command, its functionalities, and provide you with practical examples of how to use it effectively.

 

What is the Ping Command?

ping command in Linux with examples

The “ping” command is a network utility used to test the reachability of a host on an Internet Protocol (IP) network. It also measures the round-trip time it takes for packets to travel from the source to the destination and back. Essentially, “ping” helps you check whether a remote host is up and running.

 

ping – Basic Syntax

The basic syntax of the “ping” command is straightforward:

ping [options] host_or_IP_address

 

Here are a few examples of using the ping command:

Basic Ping Test

To check the connectivity to a specific IP address or domain, you would use the ping command followed by the address. For example, to check connectivity to Google’s public DNS, you’d use:

ping 8.8.8.8

To ping a domain, such as example.com, you’d use:

ping example.com

Ping with a Specific Count

By default, ping will continue to send echo requests until it’s interrupted. To limit the number of requests, you can use the -c option followed by the number of pings you want to send. For example, to send 4 pings to example.com:

ping -c 4 example.com

Ping with a Deadline

The ping command can also be instructed to stop after a set amount of time, regardless of how many packets have been sent or received. This can be useful for automating tests in scripts. To ping an address for 10 seconds and then stop, you would use the -w option (or -t on macOS), like so:

ping -w 10 example.com # On Linux
ping -t 10 example.com # On macOS

Changing the Packet Size with -s

By default, ping sends packets of 56 bytes to the target host. However, you can change the size of the packet by using the -s option. This can be useful for testing network performance under different loads. For instance, to send packets of 120 bytes to example.com, you would use:

ping -s 120 example.com

Specifying the Time to Live with -t

Time to Live (TTL) is a field in the IP packet that indicates the maximum number of hops a packet is allowed before being discarded. You can specify the TTL value with the -t option in Linux. (note that -t has a different meaning in macOS, as mentioned earlier)

For example:

ping -t 128 example.com

Flood Ping with -f

This option sends packets as fast as possible and is mostly used for stress testing. Flood pinging can consume a lot of network bandwidth and should be used with caution. It requires root privileges. Here’s how you would flood ping example.com:

sudo ping -f example.com

Audible Ping with -a

The -a option causes ping to beep every time a response is received. This can be useful if you’re waiting for a response from a host that is not up and running yet or that you expect to come online soon. For instance:

ping -a example.com

Specifying a Timeout with -W

The -W option sets a timeout in seconds for how long ping waits for a reply. If you want to wait no more than 2 seconds for a reply, you can use:

ping -W 2 example.com

Ping an IP Address Only Once with -n

In some cases, you may want to send a single echo request without resolving the host name, which can speed up the process. Use -n to avoid the DNS resolution step:

ping -n example.com

Interval Between Packets with -i

By default, ping waits for one second between sending each packet. You can change this interval with the -i option. To send a ping every 0.5 seconds, use:

ping -i 0.5 example.com

These options can help system administrators to perform more detailed diagnostics of network issues, allowing for a more nuanced understanding of the network’s behavior and performance. Find even more by using man ping.

 

Conclusion

The “ping” command is an indispensable tool for network troubleshooting and monitoring in Linux. Whether you’re checking the availability of a remote server or analyzing network performance, “ping” provides valuable insights. Armed with the knowledge of its options and usage, you can confidently manage your Linux system and maintain a reliable network connection.

Remember, mastering the “ping” command is just the tip of the iceberg when it comes to Linux network utilities, but it’s an excellent starting point for anyone on their journey in the world of Linux and IT.

So, go ahead, open your terminal, and start pinging away to explore the vast network landscape of Linux!

Useful links/references: 

Related commands:

  • traceroute – print the route packets trace to network host.
  • hping – TCP/IP packet assembler/analyzer.
  • smokeping –  keeps track of your network latency.

Tags: , ,

Discussion

  1. Hey. Does flood ping do parallel asynchronous pings, or just continuous?

    If you have a patchy internet connection, like a wi-fi hotspot connection to a 'phone in a poor reception area, it’d be nice to know how to monitor the connection to catch it as soon as the line to the ISP restores. Would ping be a good candidate for such a purpose?

  2. Hi @AnthonyRKing Good question!!

    Flood ping doesn’t perform parallel asynchronous pings, instead it sends packets as fast as possible repeatedly. While it can help monitor network stability, it may not be ideal for patchy connections. Instead, consider using regular ping with a shorter interval to monitor the connection:

    ping -i 0.5 example.com

    This way, you can quickly detect when the line to the ISP restores, making it a better candidate for such purposes.

    You can also consider using “mtr” (My TraceRoute). MTR combines the functionality of “ping” and “traceroute” and provides real-time information on both latency and route changes, making it a useful for monitoring and diagnosing intermittent connectivity issues.

    I’ll write an article for mtr next, in the meantime, see: mtr - Network Diagnostics - Documentation

  3. Very good; I’ll look forward to that then. The other thing to eliminate, or compare to, would be the Wi-Fi connection. You don’t get a router dashboard from a hot-spot (as far as I am aware), so I could also be pinging the device serving the hot-spot. It’s not obvious how to get an IP address for it though.

  4. I understand what you mean.

    Hm… If you download Fing on your smartphone. It will scan the entire Wi-Fi network you connect to and give you the IP address to most everything that’s connected on that network. Give it a try and see if it’s does.



Top ↑