Bash Ping
Test network connectivity and measure response time
📡 What is Ping?
Ping is a network utility that tests connectivity between your computer and another host. It sends packets to check if a server is reachable and measures response time in milliseconds.
# Basic ping command
ping google.com
Common Ping Commands
Basic Ping
Test if a host is reachable
ping example.com
Limited Pings
Send specific number of packets
ping -c 4 google.com
Timed Interval
Set delay between pings
ping -i 2 example.com
Audible Ping
Beep on each response
ping -a google.com
🔹 Basic Ping Usage
The ping command is the primary tool for testing basic network connectivity and latency to a remote host. By sending ICMP Echo Request packets and waiting for Echo Replies, it confirms whether a target IP address or domain is reachable. When run without flags, ping sends packets continuously, displaying each reply's round-trip time (latency) and sequence number, providing immediate feedback. The summary (Ctrl+C) shows vital statistics: packets transmitted/received, packet loss percentage, and min/avg/max latency. This is the universal first step in any network troubleshooting workflow.
# Ping a website continuously
ping google.com
# Output shows:
# 64 bytes from google.com: icmp_seq=1 ttl=117 time=15.2 ms
# 64 bytes from google.com: icmp_seq=2 ttl=117 time=14.8 ms
🔹 Ping with Count Limit
For automated checks and scripts, use ping -c COUNT to send a specific number of packets and then stop automatically. For example, ping -c 4 google.com sends exactly four ICMP requests. This is ideal for quick, non-interactive connectivity tests—like in a startup script to verify a database is reachable before launching an application, or in a monitoring system to periodically check a server's status. It provides the same summary statistics as the continuous mode but exits cleanly without requiring manual interruption, making it perfect for automation.
# Send only 5 ping packets
ping -c 5 example.com
# Ping with count and show summary
ping -c 10 192.168.1.1
🔹 Ping IP Addresses
While ping is often used with domain names, it works directly with IP addresses, which is crucial for local network diagnostics. Pinging a local router IP (e.g., 192.168.1.1) tests your internal network hardware. Pinging another computer's IP on your LAN verifies local connectivity, bypassing potential DNS issues. You can also ping the special loopback address (127.0.0.1) to test your own machine's network stack. This direct IP testing helps isolate problems: if you can ping an IP but not its hostname, the issue is likely with DNS configuration, not network connectivity.
# Ping local router
ping 192.168.1.1
# Ping specific IP with 3 packets
ping -c 3 8.8.8.8
# Ping localhost
ping 127.0.0.1
🔹 Ping with Timeout
The ping -w SECONDS option sets an overall time limit (deadline) for the ping operation, regardless of packet count. The command will run for the specified maximum number of seconds and then stop. This is extremely useful for scripting and monitoring where you cannot afford a hanging command—for instance, a network check in an automated deployment script that must complete within a specific time window. It ensures the ping test terminates predictably, even if packets are being lost and replies are delayed, providing a bounded runtime for your automation tasks.
# Ping for 10 seconds then stop
ping -w 10 google.com
# Combine timeout with packet count
ping -c 5 -w 3 example.com
🔹 Ping with Custom Interval
Control the timing between ping packets using the ping -i INTERVAL flag, where INTERVAL is in seconds. The default is 1 second. Increasing the interval (e.g., -i 5) reduces network load and is useful for long-term, low-impact monitoring. Decreasing the interval below 0.2 seconds sends packets more aggressively for stress testing or detecting micro-bursts of packet loss, but typically requires root (sudo) privileges. This flexibility allows you to tailor the command's behavior and network impact to specific use cases, from gentle background checks to intensive diagnostics.
# Ping every 2 seconds
ping -i 2 example.com
# Fast ping every 0.5 seconds
ping -i 0.5 google.com
🔹 Quiet Ping Output
For scripts where you only care about the final result, use ping -q (quiet mode) to suppress the per-packet output. In this mode, ping only prints a single line when it starts and the final summary statistics when it finishes. This keeps logs clean and readable, especially when running ping checks from cron jobs or monitoring systems where the individual round-trip times are noise. You still get the critical information: packet loss percentage and average latency, which are the key metrics for determining overall network health in an automated context.
# Show only summary
ping -c 5 -q google.com
# Output shows only:
# 5 packets transmitted, 5 received, 0% packet loss
# rtt min/avg/max = 14.2/15.8/18.1 ms