Linux Ping Command Tutorial with Examples

Introduction

Most Linux users are familiar with the ping command and know how to use it in its basic form. However, there are many additional ping options and variations. In this guide, we will walk you through some of the most useful Linux ping commands with examples.

The commands in this article work with any UNIX or Linux distribution. The test system we used to provide the examples and output samples is Ubuntu 18.04.2 LTS.

linux ping command tutorial

Prerequisites

  • Linux or UNIX-like system
  • Access to a terminal
  • A user with the necessary permissions to run the commands

How ping Works in Linux

The Linux ping command is a simple utility used to check whether a network is available and if a host is reachable. With this command, you can test if a server is up and running. It also helps with troubleshooting various connectivity issues.

The ping command allows you to:

  • Test your internet connection.
  • Check if a remote machine is online.
  • Analyze if there are network issues, such as dropped packages or high latency.

When you try to “ping” a remote host, your machine starts sending ICMP echo requests and waits for a response. If the connection is established, you receive an echo reply for every request.

The output for the ping command contains the amount of time it takes for every packet to reach its destination and return.

The terminal keeps printing the responses until interrupted. After every session, there are a few lines with ping statistics.

output of the linux ping command

How to Use the ping Command

The basic ping syntax includes ping followed by a hostname, a name of a website, or the exact IP address.

ping [option] [hostname] or [IP address]

Hence, to check whether a remote host is up, in this case, google.com, type in your terminal:

ping google.com

Press Ctrl + C on your keyboard to stop the process.

diagram of how pink works in Linux
  1. from: The destination and its IP address. Note that the IP address may be different for a website depending on your geographical location.
  2. icmp_seq=1: The sequence number of each ICMP packet. Increases by one for every subsequent echo request.
  3. ttl=52: 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.
  4. time=7.68 ms: The time it took a packet to reach the destination and come back to the source. Expressed in milliseconds.

ping Commands in Linux with Examples

ping “localhost” to Check Local Network

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

  1. ping 0 – This is the quickest way to ping localhost. Once you type this command, the terminal resolves the IP address and provides a response.
  2. ping localhost – You can use the name to ping localhost. The name refers to your computer, and when we use this command, we say: “ping this computer.”
  3. ping 127.0.0.1 – Some people prefer using the IP address to ping localhost.

Whichever method you choose, the ping output looks the same.

Linux Ping Command Tutorial with Examples

Specify the Internet Protocol

IPv6 is the IP address alphanumeric format that will supersede IPv4. The reason for this is there is a limited number of IPv4 address, and we are running out of possible combinations.

To request IPv6 or IPv4 address, add -6 or -4 after the ping command and before a hostname/IP.

ping -6 hostname/IPv6
ping -4 hostname/IPv4

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.

ping -i 0.5 google.com

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

Change Ping Packet Size

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

For example, to increase the packet size to 1000 bytes:

ping -s 1000 google.com

You get this output:

increasing the packet size to 1000 example

This command 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 Using ping to Test Performance

You can use ping flood to test your network performance under heavy load.

Ping flood -f option requires root to execute. Otherwise, apply sudo to your ping command to flood a host. This command sends a large number of packets as soon as possible.

sudo ping -f hostname-IP
Testing network performance with ping flood.

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.

How to Limit the Number of Pings

The default setting for the ping command is to keep sending the request until you interrupt it. You can limit the number of pings using one of the two methods.

Limit Number of Ping Packets

To make the ping command automatically stop after it sends a certain number of packets, use -c and a number. This sets the desired amount of ping requests, as shown in this example:

ping -c 2 google.com
sets the amount of ping requests

As you can see on 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, add -w and an interval in seconds to your command.

For example, to stop printing ping results after 25 seconds, enter the ping command:

ping -w 25 google.com

Other Uses for ping

Suppress Ping Output to Print only Summary Statistics

If you do not want to clog your screen with information for every packet, use the -q switch to display the summary only.

The -q option prints one line with the regular ping information and then provides the statistics at the end. The letter “q” in this command stands for “quiet” output.

ping -c 10 -q google.com

We usually combine the quiet output with other options. In this case, we will limit the ping to send 10 packets and suppress the output.

limit the ping to send 10 packets and suppress the output

Add Timestamp Before Each Line in ping Output

If you want to note the time of day when you execute the ping command, add the -D option.

ping -D google.com

This prints a timestamp in UNIX format before each line.

print timestamp in UNIX format before each line

Note: You need to use uppercase D, as these commands are case sensitive.


Get an Audible Ping When a Host is Reachable

When you use the –a switch, 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.

ping -a google.com

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

Show Ping Version and Exit

At any time, you can check the version of the ping tool on your system.

Append -V to the ping command to display the version of this utility.

ping -V google.com
check the version of the ping tool

Commonly used Ping Commands include:

Ping Option What The Command Does
a Generates a sound when the peer can be reached.
b Allows to ping a broadcast IP address.
B Prevents the ping to change the source address of the probe.
c (count) Limits the number of sent ping requests.
d Sets the SO-DEBUG option on the used socket.
f Floods the network by sending hundreds of packets per second.
i (interval) Specifies an interval between successive packet transmissions. The default value is one second.
I (interface address) Sets the source IP address to the specified interface IP address. The option is required when pinging IPv6 link local address. You can use an IP address or name of the device.
l (preload) Defines the number of packets to send without waiting for a reply. To specify a value higher than 3, you need superuser permissions.
n Displays IP addresses in the ping output rather than hostnames.
q Shows a quiet output. One ping line is displayed and the summary of the ping command at the end.
T (ttl) Sets the Time To Live.
v Provides verbose output.
V Displays the ping version and exits to a new command prompt line.
w (deadline) Specifies a time limit before the ping command exits, regardless of how many packets have been sent or received.
W (timeout) Determines the time, in seconds, to wait for a response.

Conclusion

Now you know how to use the Linux ping command. The examples in the tutorial should help you understand how to use “ping” on a day-to-day basis.

Ping helps you check the network connectivity both locally and with a remote host.

To view the complete list with the remaining ping options, run the man command man ping inside the terminal.

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/223865.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论