How to Use the Ping Command in Linux

Latest Comments
No comments to show.

Among the tools utilized in network administration and computer diagnostics, the ping command stands out as one of the most fundamental and widely-used. A command-line utility available in virtually every operating system, ping serves as a vital diagnostic tool for network engineers, system administrators, and even curious tech enthusiasts.

The ping command sends small packets of data to a specified IP address or host, and in return, it expects to receive a reply. By doing so, it helps users determine if a network device is reachable and provides valuable information about the network’s response time and reliability. Whether you’re troubleshooting connectivity issues, monitoring network performance, or just starting your journey into network management, the ping command is a must-know.

General syntax for ping command:

ping [IP-ADDRESS]

1. Specify the number of packets

ping -c [number] [IP-Address]

Use the -c option to have the ping command automatically stop after a certain number of packets have been sent.

Example:

When you run the ping command with the -c 5 option to send 5 packets to the IP address 127.0.0.1, you can expect to see an output similar to the following:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.059 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.057 ms

— 127.0.0.1 ping statistics —
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.045/0.055/0.059/0.006 ms

2. Check Localhost network

ping localhost

If you’re having trouble connecting to a remote computer or website, ping the localhost to ensure you’re connected.

Example:

When you execute the ping command with localhost, you’re essentially pinging your own machine. Here’s a sample output you might see:

PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.031 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.031 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.030 ms

— localhost ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 2998ms
rtt min/avg/max/mdev = 0.030/0.031/0.033/0.001 ms

3. Send pings only for a limited period of time

ping -i [number] [IP-Address]

The -i option sets the timeout interval in seconds before each packet is sent.

Example:

The command ping -i 5 127.0.0.1 will send ICMP Echo Request packets to the loopback address 127.0.0.1 every 5 seconds. Here’s a sample output:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.038 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.039 ms
— 127.0.0.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 15005ms
rtt min/avg/max/mdev = 0.035/0.038/0.040/0.007 ms

In this example, each request has been replied to in less than a millisecond, and no packets have been lost. The output may vary depending on your system configuration and the current system load.

4. Flood ping to target host

ping -f [IP-address]

The -f option will send the packets as fast as possible. This can flood the network, so it’s often referred to as “flood ping.” It’s mainly used for stress testing and should be used with caution.

Since the packets are sent as fast as possible, there might not be detailed output like the usual ping. Instead, you’ll just see a series of dots, and backspaces might be printed as packets are sent and received.

Example:

A typical output might look something like this:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
………………………………………………….^C
— 127.0.0.1 ping statistics —
468751 packets transmitted, 468750 received, 0% packet loss, time 330ms
rtt min/avg/max/mdev = 0.025/0.035/0.139/0.002 ms, pipe 2, ipg/ewma 0.182/0.037 ms

Note that the ^C above represents pressing Ctrl + C to stop the command. It’s important to know that the flood option does require root privileges, so you may need to run this command with sudo if you’re not the root user.

5. Change ping packet size

ping -s [number] [IP-Address]

Use the -s option to increase the default packet size, you can send light and heavy packets.

Example:

The command ping -s 1000 127.0.0.1 sends ICMP echo requests to the loopback address 127.0.0.1 with a packet size of 1000 bytes. Here’s an example of what the output might look like:

PING 127.0.0.1 (127.0.0.1) 1000(1028) bytes of data.
1008 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.050 ms
1008 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.054 ms
1008 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.050 ms
1008 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.054 ms
— 127.0.0.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3071ms
rtt min/avg/max/mdev = 0.050/0.052/0.054/0.002 ms

6. Display only the summary lines

ping -q [IP-Address]

The -q option outputs a single line with the regular ping information, followed by the statistics.

Example:

When you run the ping command with the -q flag on the loopback address 127.0.0.1, it sends ICMP ECHO_REQUEST packets to network hosts. The -q flag makes the command run in “quiet” mode, where it only displays the summary statistics when finished. The output might look something like this:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

— 127.0.0.1 ping statistics —
10 packets transmitted, 10 received, 0% packet loss, time 8999ms
rtt min/avg/max/mdev = 0.045/0.057/0.082/0.012 ms

7. Set Time Limit for receiving packages

ping -w [seconds] [IP-Address]

This option stops receiving ping output after a certain length of time.

Example:

When you run the ping command with the -w 10 option to the loopback address 127.0.0.1, it sends ICMP echo request packets to the address and waits up to 10 seconds for the replies. Here’s an example output you might see:

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.041 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.038 ms

— 127.0.0.1 ping statistics —
10 packets transmitted, 10 received, 0% packet loss, time 8999ms
rtt min/avg/max/mdev = 0.038/0.041/0.045/0.002 ms

More Linux commands:

Directory Operations
rmdir · cd · pwd · exa · ls

File Operations
cat · cp · dd · less · touch · ln · rename · more · head

File System Operations
chown · mkfs · locate

Networking
ping · curl · wget · iptables · mtr

Search and Text Processing
find · grep · sed · whatis · ripgrep · fd · tldr

System Information and Management
env · history · top · who · htop · glances · lsof

User and Session Management
screen · su · sudo · open

The post How to Use the Ping Command in Linux appeared first on Hongkiat.

Categories

No responses yet

Leave a Reply

Your email address will not be published.