Beginners Guide for Ping Command in Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

PING, also known as “Packet Internet Groper”, is the most common networking tool used in Windows, Linux, and macOS to test the connection between the source and the destination.

The destination, or remote system, could be a web server, your router, or a computer on your local network; you will specify them with their domain or IP address.

When you specify the destination (domain or IP address), the ping command will send a series of ICMP (Internet Control Message Protocol) packets to the remote system and wait for the response.

In the response output, you will get different pieces of information using which you can determine whether the remote system is alive or not, the latency of the network, and how many packets are dropped.

Although you might already be aware of the basic usage of this command, stick with this article to learn several variations and options for the ping command (with examples).

Tutorial Details

DescriptionPing
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesping
Internet RequiredNo
Discussed Tools in this Article

Ping Command Usage

The simplest way to use the ping command (which you might already know) is to specify the domain or IP address of the destination without any options.

In the following command, “google.com” is being ping to test whether the domain is alive or not by sending ICMP packets of 56 bytes.

$ ping google.com

#OR 

$ ping 142.250.192.14

Output:

Pinging the remote host
Pinging the remote host

Note that the ping command will keep pinging the remote system unless you manually stop it by pressing the “Ctrl+C” shortcut key.

Once you stop the ping command, it will give you a summary of all the transmissions made between the source and destination that includes the following information:

  • from: It identifies the target by its IP address (the simplest way to obtain the IP address of any domain name).
  • icmp_seq: It’s the sequence of the ICMP packet sent to the server in order.
  • ttl: It tells how long a packet is allowed to stay in a network before it is thrown away by a router.
  • time: It tells the amount of time it took for the referenced packet to reach the remote system and come back to the origin.

In addition, you will receive a summary of the total number of transmitted and received packets, the number of lost packets, and the average time spent exchanging a packet.

Now that you know how the ping command works and how to read its output, let’s see some of the other examples of the ping command’s usage with its options.

Also Read: How to Ping Multiple Hosts at High Performance with fping in Linux

Ping a Host Only a Specific Number of Times

The default behavior of the ping command is to keep pinging the remote system unless the user manually stops the ping command.

However, you can use the “-c” flag with the number of packets to send on the remote system, after which the ping command will automatically stop.

For example, the following command will only ping “google.com” twice.

$ ping -c 2 google.com

Output:

Sending a definite number of packets to the remote system
Sending a definite number of packets to the remote system

Apart from the number of packets, you can stop the ping command by specifying the timeout that you will learn in the next section.

Setting the Ping Timeout

Aside from defining the number of packets to send, you can also specify the timeout using the “-w” flag, at which the ping command will automatically stop.

For example, the following command will only ping β€œgoogle.com” for five seconds.

$ ping -w 5 google.com

Output:

Setting the timeout for the ping command
Setting the timeout for the ping command

Change the Time Interval While Sending Each Packet

The default behavior of the ping command is to send packets at an interval of one second. However, you can use the “-i” flag to change the time interval between two pings to prevent getting blocked by the remote system.

For example, the following command will send ICMP packets to the remote system after two seconds on each request.

$ ping -i 2 google.com

Output:

Sending packet to remote system after two seconds
Sending packet to remote system after two seconds

It should be noted that you can specify the time interval in milliseconds by assigning a value in floating point (decimal value), but you must do so as root or with sudo privileges.

Note: Reducing the interval time below one second will increase the chance of packet loss (especially in a slow network).

For example, the following command will send the ICMP packet to the remote system after 0.5 seconds (500 ms).

$ ping -i 0.5 google.com

Output:

Sending packet to remote system after point five milliseconds
Sending packet to remote system after point five milliseconds

Changing the ICMP Packet Size

By default, the ping command sends the ICMP packets in 56 bytes, as you can see in the earlier outputs, but you are not limited to using this predefined size. Using the “-s” flag, you can send a remote system a custom-sized ICMP packet in bytes.

The following command will send an ICMP packet size of 100 bytes to “google.com“.

$ ping -s 100 google.com

Output:

Using custom packet size while sending on remote system
Using custom packet size while sending on remote system

Do not Lookup the Symbolic Names for the Host

By default, the ping command looks for the symbolic names in specified DNS records to give the host name instead of an IP address. This could be easily avoided by using the “-n” flag.

For example, the following command will only give you the numeric output for “google.com” instead of symbolic names.

$ ping -n google.com

Output:

Do not look for the symbolic names
Do not look for the symbolic names

DoS Attack with Ping Flooding

One of the simple ways to determine the response of your remote system (or server) under high-load conditions is by initiating the DoS attack using the ping command, also known as “ping flooding“.

In this scenario, the ping command will send a large number of ping requests as fast as possible in a very short time frame.

Warning: If you are ping flooding on your own server, then it’s okay (unless you are using shared hosting), but if you are testing on someone else’s server, you might get no response or get blocked by the server as many servers do not allow ICMP requests.

For example, the following command with the “-f” flag will stress test “google.com” unless you manually interrupt the ping command.

Note: The following command will only be initiated by root or users with sudo privileges.

$ ping -f google.com

Output:

Ping overload on the remote system
Ping overload on the remote system

When you initiate the ping flood, you will see some dots keep appearing and disappearing in the output.

Every “.” dot is a request, and a backspace is the answer. Since the backspace clears the dots, the dot appears and disappears.

Also Read: Block/Unblock Ping ICMP Requests in the Linux System

Every Successful Ping is Accompanied by a Sound Alert.

If you use the “-a” flag with the ping command, every successful ping request to the remote system will make a sound.

Note: Some systems might not hear the beep noise. If it happens to you, notify us of your system name in the comment section.

$ ping -a google.com

Output:

Alert with sound on every successful ping
Alert with sound on every successful ping

So, that’s all for now.

If you have any questions or queries related to this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.