ping - Linux
Overview
The ping
command is a network utility used to test the availability of a host on an IP network. It operates by sending Internet Control Message Protocol (ICMP) Echo Request messages to the target host and listens for Echo Reply messages. Its primary use is for troubleshooting network connectivity issues and for performance measurement.
Syntax
ping [options] destination
- destination: This can be either an IP address or a hostname to which the ICMP Echo Requests are sent.
Options/Flags
-a
: Audible ping. Play a beep sound for each response received.-c count
: Stop after sending (and receiving)count
ECHO_RESPONSE packets.-i interval
: Waitinterval
seconds between sending each packet. The default is 1 second.-s packetsize
: Specifies the number of data bytes to be sent.-t ttl
: Set the IP Time to Live.-q
: Quiet output. Only display the summary lines at startup time and when finished.-l preload
: Sendpreload
number of packets as fast as possible before falling into the normal rate of sending (only as superuser).-f
: Flood ping. Outputs packets rapidly (only as superuser). Very high rate ping, useful for stress tests.-w deadline
: Specify adeadline
in seconds by which the command will terminate regardless of how many packets have been sent or received.
Examples
-
Basic ping:
ping google.com
Sends ICMP packets to
google.com
until interrupted. -
Ping with count:
ping -c 4 google.com
Send exactly 4 packets to
google.com
and then exit. -
Specifying packet size:
ping -s 100 google.com
Sends packets of 100 bytes to
google.com
. -
Ping with deadline:
ping -w 10 google.com
Continue to send packets to
google.com
for 10 seconds.
Common Issues
- Permission denied: Without superuser rights, the
-f
(flood) option might fail. - Network is unreachable: Indicates a problem with the local network configuration; check your network settings and interfaces.
- Unknown host: The DNS resolution for the destination failed; verify the hostname and DNS settings.
Integration
Combining ping
with grep
:
ping -c 10 example.com | grep 'bytes from'
This command pings example.com
10 times, but only outputs the lines indicating responses.
Scripting:
for ip in {1..10}; do
ping -c 1 "192.168.0.$ip"
done
Pings 10 hosts in a local subnet and checks their availability.
Related Commands
traceroute
: Trace the route packets take to a network host.netstat
: Display network connections, routing tables, interface statistics, and more.nc
(netcat): Utility for reading from and writing to network connections.
Further reading:
- Visit Ping Man Page for more in-depth details on usage and options.