GICT knowledge base
How to Test Network Connectivity in Linux with the ping Command
Published
Whether you’re a Linux beginner, a system administrator, or just someone who wants to figure out why the internet isn’t working, the ping command is one of your best friends. This simple, time-tested tool lets you check if your computer can reach another device on the network, be it a nearby server, your router, or a website like Google.
You don’t need to be a networking expert to use ping, and yet, mastering its options and understanding its output can make you feel like one. In this guide, you’ll learn not just how to use ping, but how to interpret its results, troubleshoot common issues, and get the most out of this essential Linux command.
Table of Contents
- What Is the ping Command?
- Why Use ping to Test Network Connectivity?
- How to Use ping: The Basics
- Interpreting ping Output
- Common ping Options and What They Do
- Practical Examples of Using ping
- Real-World Troubleshooting with ping
- How to Stop ping
- Tips for Using ping Effectively
- Common Issues and What They Mean
- Frequently Asked Questions (FAQ)
- Conclusion
What Is the ping Command?
The ping command is a simple way to test whether your computer can communicate with another device on a network. When you run ping, your system sends a small ICMP “echo request” packet to the target (an IP address or hostname) and waits for a reply. If the other device responds, you know it’s reachable. If not, you may have a network problem.
The name comes from the sound sonar makes—a “ping”—as it bounces off objects, just like your computer sends and waits for responses.
Why Use ping to Test Network Connectivity?
- Check if a host is up and reachable.
- Diagnose network problems quickly.
- Measure latency (how long it takes for data to travel back and forth).
- Determine if packet loss is happening.
- Test your own network interfaces (loopback).
- Verify DNS resolution (by pinging a hostname).
In short, ping is often the first step in troubleshooting any network issue, from a local connection problem to an unreachable website.
How to Use ping: The Basics
Open your terminal and type:
ping google.com
You’ll see output like:
PING google.com (142.250.72.206) 56(84) bytes of data.
64 bytes from lhr48s10-in-f14.1e100.net (142.250.72.206): icmp_seq=1 ttl=115 time=17.1 ms
64 bytes from lhr48s10-in-f14.1e100.net (142.250.72.206): icmp_seq=2 ttl=115 time=16.9 ms
On most Linux systems, ping keeps running until you stop it (see below). On some, it may send only a few packets by default.
To test if your own network stack is working, ping yourself:
ping 127.0.0.1
Or your default gateway (often your router, e.g., 192.168.1.1):
ping 192.168.1.1
Interpreting ping Output
Each line of output gives you valuable information:
- bytes from: The number of bytes in the response.
- IP address or hostname: Where the reply is coming from.
- icmp_seq: The sequence number of the packet sent.
- ttl: “Time To Live,” which decreases as the packet passes through routers.
- time: Round-trip time in milliseconds—lower is better.
At the end, you’ll see a summary like:
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 16.879/17.021/17.157/0.104 ms
- packet loss: If greater than 0%, you might have a connection problem.
- rtt min/avg/max: Lowest, average, and highest round-trip times (in ms).
Common ping Options and What They Do
Limiting the Number of Packets
Send only 4 packets (good for quick checks):
ping -c 4 google.com
Setting Packet Size
Send packets larger or smaller than default:
ping -s 128 google.com
(Default is 56 bytes.)
Changing the Interval Between Packets
Send a packet every 2 seconds:
ping -i 2 google.com
Using IPv4 or IPv6 Explicitly
Force IPv4:
ping -4 google.com
Force IPv6:
ping -6 google.com
Flooding (Advanced)
ping -f google.com
Warning: This sends packets as fast as possible and is for advanced diagnostics only. You may need root and should never use it on remote systems you don’t own.
Practical Examples of Using ping
Check if your machine sees the internet:
ping -c 4 8.8.8.8
If this works, you have connectivity. If not, check your local network.
Test DNS resolution:
ping -c 2 google.com
If IPs work but names don’t, you may have a DNS issue.
Test LAN connectivity:
ping 192.168.1.10
Test your own system:
ping 127.0.0.1
If this fails, there’s a problem with your network stack.
Real-World Troubleshooting with ping
Scenario 1: Can’t reach a website
-
- Ping the website.
ping -c 4 example.com
-
- If it fails, ping the IP directly.
ping -c 4 [IP address]
- If the IP works but the name doesn’t, check your DNS settings.
Scenario 2: No internet but local devices respond
Ping your gateway:
ping -c 4 192.168.1.1
If success, but pinging 8.8.8.8 fails, your router might have lost its internet connection.
Scenario 3: Intermittent lag or drops
ping -c 100 google.com
Look for packet loss or high/variable times.
How to Stop ping
On most Linux systems, ping runs until you stop it. To stop:
Press Ctrl + C in the terminal.
You’ll see a summary of the statistics.
Tips for Using ping Effectively
- Always start local (
127.0.0.1), then move outward (gateway, LAN, internet). - Use hostnames to test DNS.
- For scripting, use
-cto limit packet count. - For noisy or flaky networks, increase the count (
-c 50or more) to spot intermittent issues. - Respect remote servers—don’t flood with excessive pings.
Common Issues and What They Mean
- 100% packet loss: No connection; device is down, blocked, or address is wrong.
- Unknown host: DNS can’t resolve the hostname. Try the IP.
- Request timed out: Device is unreachable, busy, or blocking ICMP.
- General failure or Operation not permitted: ICMP may be blocked by your system or by a firewall.
Frequently Asked Questions (FAQ)
- Q: Is it safe to use ping?
- A: Yes, it’s a diagnostic tool. Most devices allow ICMP ping requests, but some networks block them.
- Q: Why does ping to some sites fail even though I can browse them?
- A: Some servers block ICMP pings for security or to reduce load. Try different sites or your router.
- Q: How do I ping a specific port?
- A: Ping works at the network layer and doesn’t check ports. For ports, use tools like
nc(netcat) ortelnet. - Q: Is there a GUI tool for ping?
- A: Yes! Many Linux desktops have a “Network Tools” or “Ping” utility as part of their system tools.
- Q: Can I automate ping in scripts?
- A: Absolutely. Use
ping -cand check the exit code or output.
Conclusion
The ping command is one of the simplest yet most powerful tools in your Linux networking toolbox. With just a line in the terminal, you can diagnose connectivity, check the health of your network, and pinpoint where problems start.
Remember to start with nearby devices and work your way out, interpret the output, and use options for more targeted tests. With practice, you’ll be able to troubleshoot and solve network issues faster than ever.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
