How to Use Ping Command in Linux: Syntax and Examples
Use the
ping command in Linux to test the reachability of a host on a network by sending ICMP echo requests. The basic syntax is ping [options] destination, where destination can be an IP address or domain name.Syntax
The basic syntax of the ping command is:
ping [options] destination
destination is the IP address or domain name you want to check.
options modify the behavior, like how many packets to send or timeout.
bash
ping [options] destination
Example
This example shows how to ping google.com 4 times and then stop:
bash
ping -c 4 google.comOutput
PING google.com (142.250.190.78) 56(84) bytes of data.
64 bytes from lga34s10-in-f14.1e100.net (142.250.190.78): icmp_seq=1 ttl=117 time=14.2 ms
64 bytes from lga34s10-in-f14.1e100.net (142.250.190.78): icmp_seq=2 ttl=117 time=14.0 ms
64 bytes from lga34s10-in-f14.1e100.net (142.250.190.78): icmp_seq=3 ttl=117 time=13.9 ms
64 bytes from lga34s10-in-f14.1e100.net (142.250.190.78): icmp_seq=4 ttl=117 time=14.1 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 13.900/14.055/14.243/0.147 ms
Common Pitfalls
Common mistakes when using ping include:
- Not specifying
-cto limit the number of pings, causing it to run indefinitely. - Using an incorrect hostname or IP address, resulting in errors like "unknown host".
- Running
pingwithout proper permissions on some systems (rare).
Always verify the destination and use -c to control the test duration.
bash
ping google.com # runs indefinitely ping -c 3 google.com # runs 3 times and stops
Quick Reference
| Option | Description |
|---|---|
| -c | Stop after sending |
| -i | Wait |
| -t | Set the Time To Live for packets |
| -s | Specify the number of data bytes to be sent |
| -W | Time to wait for a response in seconds |
Key Takeaways
Use
ping destination to check if a host is reachable on the network.Add
-c option to limit the number of ping requests sent.The destination can be an IP address or a domain name.
Common errors include misspelling the destination or forgetting to stop the command.
Use options like
-i and -W to customize ping behavior.