Linux ping Command with Examples

April 2, 2024

Introduction

The Linux ping command is a network utility used to test a host's reachability on an Internet Protocol (IP) network. Its name comes from the sonar "ping" used in submarines, where a sound pulse is sent out to detect objects.

ping sends out ICMP (Internet Control Message Protocol) echo request packets to the target host and waits for echo replies. This method allows users to measure round-trip time and packet loss. The command is especially useful for troubleshooting network connectivity issues and assessing network performance.

In this guide, we will show you how to use the Linux ping command and provide useful examples and alternatives.

Linux ping command with examples - a tutorial.

Prerequisites

  • A machine running Linux or UNIX-like system.
  • Access to the terminal (Ctrl + Alt + T).
  • A user account with root privileges.

ping Command Syntax

The ping command has the following syntax:

ping [options] [hostname/IP address]

The options are not mandatory. They control the command's behavior, and they can be combined.

The following example shows the ping command output when used without options:

ping google.com

Pinging continues until you press Ctrl + C to stop the process.

An example output of the ping command.

The output shows the following info:

  • from. The destination and its IP address. Note that a website's IP address may be different depending on your geographical location.
  • icmp_seq=1. The sequence number of each ICMP packet. Increases by one for every subsequent echo request.
  • ttl=58. The Time to Live value from 1 to 255. It represents the number of network hops a packet can take before a router discards it.
  • time=13.1 ms. The time it took a packet to reach the destination and come back to the source. Expressed in milliseconds.

The section below explores the most commonly used ping command options.

ping Command Options

The ping command accepts various options that customize how you test network connectivity. You can specify the number of ping requests sent, the data packet size, how long to wait for responses, etc.

The following table covers some of the most commonly used ping command options:

OptionDescription
-aGenerates a sound when the peer can be reached.
-c [count]Limits the number of ping requests sent to the target.
-i [interval]Sets the time (in seconds) to wait between sending ping requests.
-I [interface_address]Sets the source IP address to the specified interface IP address. The option is required when pinging an IPv6 link-local address. You can use an IP address or the device name.
-nDisplays only numeric output (IP addresses instead of hostnames).
-qSets the source IP address to the specified interface IP address. The option is required when pinging an IPv6 link-local address. You can use an IP address or the device name.
-s [packet_size]Specifies the size (in bytes) of data included in each ping request.
-t [ttl]Sets the maximum number of hops (ttl = Time to Live) a ping request can traverse before being discarded.
-vProvides more detailed output about each ping request and response.
-w [deadline]Sets the maximum time (in seconds) for the entire ping operation to run.
-W [timeout]Determines the time, in seconds, to wait for a response.

To view the complete list with the remaining ping options, run the man command man ping in the terminal. The section below provides hands-on examples of using the ping command and some of its options.

ping Command Examples

The most common use of the ping command is to test the network connectivity of a website or another computer. However, it can also be used to troubleshoot network issues, detect faulty devices on the network, and more.

The examples below show the various uses of ping.

ping localhost to Check Local Network

If you encounter issues reaching a website or a remote machine, you can ping localhost to confirm your network connection is working. Use one of the three commands below to check the local network interface:

ping 0

This is the quickest way to ping localhost. After running this command, the terminal resolves the IP address and provides a response.

ping localhost

You can use the name to ping localhost, as the name refers to your computer.

ping 127.0.0.1

You can also use the IP address 127.0.0.1 to ping localhost. Whichever method you choose, the ping output looks the same:

Pinging the 127.0.0.1 localhost to check the Internet connection.

Specify the Internet Protocol

IPv6 is the IP address alphanumeric format that will supersede IPv4. It will occur because there is a limited number of IPv4 addresses, and we are running out of possible combinations. To request an IPv6 address, add -6. Add -4 after the ping command to request an IPv4 address. For example:

ping -6 [hostname]
ping -4 [hostname]
Specifying which Internet protocol to use for pinging.

Change Time Interval Between Ping Packets

The default interval between each ping request is set to one second. You can increase or decrease that time using the -i switch. To decrease the ping interval, use values lower than 1. For example:

ping -i 0.5 google.com

The command pings the specified domain every 0.5 seconds.

To increase the ping interval, enter any value higher than 1.

Change Ping Packet Size

In some scenarios, you may want to use the -s option to increase the packet size from the default value of 56 (84) bytes. The number in the parenthesis represents the ping bytes sent, including 28 bytes of the header packet.

For example, to increase the packet size to 1000 bytes, run the command below:

ping -s 1000 google.com
Changing the default ping packet size.

This option is useful when testing network performance. You can test if a network link throttles when you increase the packet size to a few thousand bytes.

Flood a Network to Test Performance

The -f (flood) option allows you to test network performance under heavy load. Note that the option requires root to execute. This command sends a large number of packets as soon as possible. The syntax is:

sudo ping -f [hostname/IP]
Flooding a network to test the performance.

The output prints a dot for every sent package and a backspace for every response. The statistics line shows a summary of the ping command.

Limit Number of Ping Packets

To make the ping command automatically stop after it sends a certain number of packets, use the -c option followed by a number. For example:

ping -c 2 google.com
Limiting the number of ping packets.

As seen in the image above, the ping command stopped sending packets after two requests.

Set Time Limit for ping Command

To stop receiving a ping output after a specific amount of time, specify the -w option followed by an interval in seconds. For example, to stop printing ping results after 25 seconds, run the following command:

ping -w 25 google.com

Suppress ping Output to Print only Summary Statistics

If you do not want to clog your screen with information for every packet, specify the -q (quiet) option to display only the summary statistics. The -q option prints one line with the regular ping information and then provides the statistics at the end.

We usually combine the quiet output with other options. In the following example, we limit the ping to 10 packets and suppress the output:

ping -c 10 -q google.com
Suppressing the ping command output and showing only the statistics.

Add Timestamp Before Each Line in ping Output

If you want to note the time when you execute the ping command, specify the -D option. The option causes ping to print a timestamp in UNIX format before each line. For example:

ping -D google.com
Adding a timestamp to the ping command output.

Get an Audible Ping When a Host is Reachable

When you use the -a option, the system plays a sound when there is a response from a host. An audible ping is useful when you are troubleshooting network issues and do not want to look at the screen until there is a response. Following is an example of using the -a option:

ping -a google.com

The output looks the same as a regular ping command output.

Show Ping Version and Exit

You can check the ping utility version on your system by appending the -V option to the ping command. For example:

ping -V
Checking the ping command version in Linux.

ping Command Alternatives

The ping command is fundamental for network troubleshooting, but it has its limitations. This section explores several alternative tools that network administrators can use to analyze network connectivity and performance.

traceroute Command

Similar to ping, the traceroute command checks connectivity but also reveals the route packets take, helping pinpoint bottlenecks or problematic hops. The syntax for the traceroute command is:

traceroute [options] [hostname/IP] [packet length]

nmap Command

Unlike ping which focuses on basic connectivity, nmap is a powerful port scanner that discovers open ports on a target device.

The tool allows users to identify running services, potential vulnerabilities, and the device's operating system. It provides a more in-depth analysis of a network device compared to the basic reachability check offered by ping.

The syntax for the nmap command is:

sudo nmap [hostname/IP]

nslookup Command

The nslookup command is primarily used for name resolution. The command can verify if your machine can translate domain names into IP addresses, which is a crucial step for connecting to websites. Although it isn't a direct replacement for ping, it can be a helpful alternative for specific situations related to connectivity troubleshooting.

The syntax for nslookup is:

nslookup [options] [domain_name]

Conclusion

This tutorial has shown how to use the Linux ping command through hands-on examples and offered a couple of alternatives that cover other networking aspects that ping does not.

Next, master Linux networking commands with our Linux network commands tutorial and download a free PDF cheat sheet to always have them at hand.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Use IP Command in Linux with Examples
September 12, 2024

The ip command is a Linux net-tool for system and network administrators used for configuring network...
Read more
Best Tools to Monitor Network Bandwidth on a Linux Server
September 19, 2024

There are many different tools for monitoring network traffic on a Linux server. Each option has its...
Read more
How to List Users in Linux, List all Users Command
August 4, 2022

Linux OS is unique because of its multiuser characteristic. It allows multiple users on one system, at the...
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the...
Read more