How to use ping command in Linux with examples

Ping command is used to test network connectivity between two systems. It also provides details about how much time was taken to return data from other system. Input for PING command is IP address or the URL and PING sends a data packet to the specified address with the message “PING” and after some time it gets a response from the server/host. This recorded time is called latency. Ping is measured in microseconds. Faster ping or low latency means faster connection. Ping uses ICMP(Internet Control Message Protocol) to send an ICMP echo message to the specified host if that host is available then it sends ICMP reply message.

Syntax

ping www.infojinx.com

The ping command continue indefinitely and has to be stopped manually by ctrl+c.

Understanding print statistics

Firstly it will give details of packet received and dropped followed by min/avg/max time response time.

  • min: minimum time to get a response
  • avg: average time to get responses
  • max: maximum time to get a response

Examples of ping command use

  1. Change time interval between packets sent by ping command
    By default there is gap of 1 second between packets sent by ping command. To change this time interval you can use -i switch.
    Syntax

    ping -i 5 ip_address/host_name
    Example
    ping -i 5 infojinx.com
    ping -i 0.5 infojinx.com

    Reduce time interval below 0.2 can only be done by root user.

  2. Change size of data packet sent by ping command
    By default packet send by ping command is of 64 bytes (56 bytes + 8 bytes ICMP header). You can change this packet size by using switch -s. Suppose you change size to 90 bytes then 90 bytes + 8 bytes of ICMP header will be sent.
    Syntax

    ping -s size_of_packet ip_address/host_name

    Example

    ping -s 80 infojinx.com

     

  3. Control number of data packet sent by ping command
    By default ping command continuously sends data packets and to exit ping command you need to press control+c. But by using switch -c you can control number of packets to be send and after assigned number of packets are sent, ping command exits on its own.
    Syntax

    ping -c number_of_packet ip_address/host_name

    Example

    ping -c 2 infojinx.com
  4. Set timeout for ping command
    Basically what timeout does is, it stops ping command after set time has elapsed irrespective of how many packets ping command has sent or received. For setting timeout you will have to use switch -w with time specified in seconds.
    Syntax

    ping -w time ip_address/host_name

    Example

    ping -w 5 infojinx.com
  5. Set ping to get an audible reply
    Using switch -a can get you an audible sound whenever a response is returned.
    Syntax

    ping -a ip_address/host_name

    Example

    ping -a infojinx.com