Linux ping Command Tutorial With Examples – POFTUT

Linux ping Command Tutorial With Examples


Ping is one of the most used tool for network diagnostics. When internet is not working, can not access file shares etc we will firstly ping command. Ping is one of the most known tool in the IT area. Ping actually uses ICMP packets to get information about network status. In this tutorial we will look practical use cases of Ping tool.

Syntax

Syntax of ping command for Linux is like below.

$ ping [options] [hop1...] destination

Help

$ ping -h
Help
Help

ICMP

ICMP is a network protocol mainly used by ping command. ICMP have different type of messages to exchange network, host, router, switch information. Ping command uses ICMP Request which is numbered 8 and ICMP Reply which is numbered 0.

Ping

We will just ping with default settings for ping command. By default ping will work infinitely. We can stop ping command with processes cancel keyboard shortcut CTRL+c .

$ ping poftut.com
Ping
Ping

After stopping ping command there will be some statistics about ping command. There will be following information

  • Packet transmitted shows the count of total packet sent
  • Received show total count of packets received
  • Packet loss show the percentage of lost or not responded packages
  • Time shows how long the ping command worked.
  • Rtt min show minimum round trip time which is how long take a ICMP request and response
  • Rtt avg shows average round trip time
  • Rtt max shows maximum round trip time

Increase Interval

By default ping command uses 1 second intervals. Interval is the time between two ping or ICMP request. This amount can be changed with -i option

$ ping -i 2 poftut.com
Increase Interval
Increase Interval

Decrease Interval

In previous example we have increased the interval. We can also set lower interval for ping requests. But there is a minor difference. To set interval lower than 0.2 we need root privileges. In this example we will set interval as 0.5 second.

$ ping -i 0.5 poftut.com
Decrease Interval
Decrease Interval

Check Local Interface Status

Ping have some diagnostic feature. We can check the status of the local network interface with  destination address 0

$ ping  0
Check Local Interface Status
Check Local Interface Status

Set Packet Count

As we stated before ping command will work endlessly if there is no interrupt for ping. Providing this interrupt can be difficult job if we use ping as a batch script. We may need to run ping some little time and use the output. In this situations we can set the packet count. Ping will only send specified count. In this example we will send only 3 ping or ICMP requests to the destination.

$ ping -c 3 poftut.com
Set Packet Count
Set Packet Count

Audible Ping

There are some alert mechanism in the ping command. Ping command can provided some voice alert if some change occur. This change can be the destination host is down or up. But keep in mind that this may not work in some terminals.

$ ping -a poftut.com
Audible Ping
Audible Ping

Find IP Address

The IP address will be printed in the first line of ping output if we specify the destination with its host name.

$ ping  poftut.com
Find IP Address
Find IP Address

In this example host name is poftut.com and related IP address is 45.79.133.118

Change Ping Packet Size

Ping command uses ICMP packets and puts some meaningless data in the ICMP packet. This data size is 56 byte by default. This data size can be changed with -s parameter. In this example we will set the data size for ICMP packet 200.

$ ping -s 200  poftut.com
Change Ping Packet Size
Change Ping Packet Size

Set Time To Live or TTL

IP packets have Time To Live count. This TTL will set how much hop the packet can transmitted. By using -t option TTL value can be set. In this example we will set TTL as 22 which is by default 52.

$ ping -t 22  poftut.com
Set Timeout or Time To Live
Set Timeout or Time To Live

Set Timeout

While sending ICMP request packets we wait for some amount of time for the ICMP response. This waiting time can be change with -w option. After the timeout value the ping command will exit. In this example we will set timeout value as 2 seconds.

$ ping -w 2  poftut.com
Set Timeout
Set Timeout

Set Route or Path For Ping

The ICMP packets send by ping command will be routed according to the default routes in the network. Sometimes this default routes will not enough for the test case. In this situations the route information can be provided to the ping command by adding them before the destination. In this example we will set 192.168.122.1 route byte hand. We want to ping destination poftut.com

$ ping 192.168.122.239 192.168.122.1  poftut.com

But keep in mind that the network devices should be supported host provided routing. This can be a security implication and disabled in the network devices.

LEARN MORE  How To Prevent SQL Injection in Php Applications?

Online Ping Tool

There are some online ping tools which provided the scale ability to select source host.

https://asm.ca.com/en/ping.php

Flood ICMP Packets

Flood is a term used to specify uncontrolled high level network traffic. Ping command can flood ICMP requests to the destination with -f option. This will generate mass ICMP requests without setting any interval or count. In order to flood we need root privileges.

$ ping -f poftut.com
Flood
Flood

As we can see from example there is no verbose information about the ICMP requests. There will be only a summary about the ping command.

Show Version

Ping command version information can be printed with -v parameter like below.

$ ping -V
Show Version
Show Version

As we can see that ping utility is provided by iputils package which version is s20150815

Leave a Comment