Back to Blog
Lesson 33 of the Linux: Linux Command Line for Developers course
LinuxAugust 20, 20264 min read

Testing Connectivity: Mastering Ping and Traceroute in Linux

Learn how to use ping and traceroute to diagnose network issues in Linux. Master these essential networking diagnostics to ensure your server stays online.

linuxnetworkingdiagnosticscommand-lineconnectivity
Close-up of green network cables plugged into server ports, showcasing technology setup.

Previously in this course, we explored Mastering Linux IP Configuration: A Developer's Guide to understand how to assign and view IP addresses on your machine. Now that you know how your server is addressed, this lesson adds the ability to verify that your server can actually "talk" to the rest of the world.

Understanding Network Reachability

When a server fails to communicate with a remote service or your local machine, the first step is to determine if the destination is reachable at all. Networking diagnostics rely on two primary tools: ping and traceroute.

Testing Reachability with Ping

The ping command uses the ICMP (Internet Control Message Protocol) "Echo Request" packet. When you send a ping to a host, that host sends back an "Echo Reply" if it is functioning and configured to respond.

To test if your web server can reach the internet, run:

Bash
ping -c 4 google.com
  • -c 4: This flag tells ping to send exactly 4 packets and then stop. Without it, ping will run indefinitely until you press Ctrl+C.

Interpreting the output:

  • Time: Represents the round-trip time (RTT). Lower is better.
  • Packet Loss: If this is 0%, your connection is healthy. If you see packet loss, you have an unstable connection or a congested network.

Tracing Paths with Traceroute

Sometimes a host is unreachable, but the problem isn't the destination itself—it's one of the "hops" (routers) in between. traceroute maps the path your packets take to reach a destination.

Install it if you don't have it (on Debian/Ubuntu):

Bash
sudo apt update && sudo apt install traceroute -y

Run a trace to see how your request travels:

Bash
traceroute google.com

This will display a list of every router your packet passes through before reaching the final destination. If a specific hop displays * * *, it means that router is either dropping the packet or blocking ICMP requests.

Worked Example: Diagnosing a "Black Hole"

Imagine your web server cannot reach its update repository. You suspect a network path issue.

  1. Check local connectivity: ping -c 3 8.8.8.8 (Check if you can reach a reliable public DNS).
  2. Check DNS resolution: ping -c 3 google.com (If IP works but domain doesn't, you have a DNS issue).
  3. Map the failure: If the ping fails, run traceroute 8.8.8.8.

If the trace stops abruptly at the second or third hop, you know the issue is within your local ISP or the immediate upstream provider, rather than the destination server.

Hands-on Exercise

  1. Open your terminal.
  2. Run ping -c 5 127.0.0.1. This is your "loopback" address—the server talking to itself. It should always work.
  3. Run a traceroute to a major site like github.com. Count how many hops it takes to reach their infrastructure.

Common Pitfalls

  • ICMP Blocking: Many modern firewalls and cloud providers (like AWS or GCP) block ICMP by default for security. A server might be perfectly fine but appear "down" because it ignores your ping requests. Always verify if the service is running (e.g., via a web browser) before assuming the server is offline.
  • Confusing Latency with Downtime: High RTT (e.g., 500ms) indicates a slow network, not necessarily a broken one. Don't confuse sluggishness with a complete break in connectivity.
  • Missing Dependencies: traceroute is not always pre-installed on minimal Linux images. If the command isn't found, use your package manager to install it.

FAQ

Q: Why does ping work but traceroute show stars? A: Some routers are configured to ignore traceroute packets to hide their internal network topology, even if they allow standard traffic to pass through.

Q: Can I use ping to test a specific port, like 80 or 443? A: No. ping operates at the network layer (Layer 3). To test specific ports, you will need tools like telnet or nc (Netcat), which we will cover in the next lesson.

Recap

We've covered the basics of connectivity testing:

  • Use ping to verify if a destination is alive and measure latency.
  • Use traceroute to visualize the path and identify where a connection is being dropped.
  • Remember that ICMP filtering is common; always cross-reference ping results with actual application availability.

Up next: Network Ports and Services — We will move from verifying general reachability to checking if specific applications are listening on your server's ports.

Similar Posts