Network Troubleshooting Tools

Level: Intermediate Module: TCP/IP & Networking 7 min read Lesson 20 of 66

Overview

  • What you’ll learn: A systematic approach to network troubleshooting, and how to use ping, traceroute, mtr, ss, netstat, curl, tcpdump, nmap, and tshark/Wireshark to diagnose connectivity, performance, and security issues.
  • Prerequisites: Lesson 19 – Firewall with ufw and iptables
  • Estimated reading time: 25 minutes

Introduction

Network problems are among the most common and frustrating issues in system administration. A server that cannot reach the Internet, a service that times out, or a mysterious packet loss can bring entire systems to a halt. The key to effective troubleshooting is a systematic, layer-by-layer approach combined with the right tools.

Linux provides an exceptionally rich set of network diagnostic tools, from simple utilities like ping to powerful packet analyzers like tcpdump. Mastering these tools allows you to quickly identify whether a problem is at the physical layer, the network layer, the transport layer, or the application layer.

In this lesson, you will build a comprehensive troubleshooting methodology and learn to use each tool effectively with real-world examples.

Systematic Troubleshooting Approach

When diagnosing network issues, work from the bottom of the OSI model upward. This layer-by-layer approach ensures you do not waste time on application-layer debugging when the problem is actually a broken cable or a misconfigured route.

  • Layer 1 (Physical): Is the cable connected? Is the interface up? Check ip link show.
  • Layer 2 (Data Link): Can you see the MAC address of the gateway? Check ip neigh show.
  • Layer 3 (Network): Can you ping the gateway? Can you ping external IPs? Check ping and ip route.
  • Layer 4 (Transport): Is the port open? Is the service listening? Check ss and nc.
  • Layer 7 (Application): Is the application responding correctly? Check curl and application logs.
# Quick connectivity check script
#!/bin/bash
echo "=== Interface Status ==="
ip link show ens33 | grep -E "state (UP|DOWN)"

echo "=== IP Address ==="
ip addr show ens33 | grep "inet "

echo "=== Default Gateway ==="
ip route show default

echo "=== Gateway Reachability ==="
ping -c 2 -W 2 $(ip route show default | awk '{print $3}') 2>&1 | tail -3

echo "=== DNS Resolution ==="
dig +short +time=2 www.example.com

echo "=== External Connectivity ==="
ping -c 2 -W 2 8.8.8.8 2>&1 | tail -3

Ping, Traceroute, and MTR

ping tests basic IP connectivity by sending ICMP echo requests. traceroute reveals the path packets take to a destination, showing each hop. mtr combines both into a continuous, real-time display that is ideal for identifying intermittent packet loss.

# Basic ping — test connectivity and measure latency
$ ping -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=4.23 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=4.15 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 4.123/4.189/4.234/0.041 ms

# Ping with specific packet size (test MTU issues)
$ ping -c 3 -s 1472 -M do 8.8.8.8

# Traceroute — show the path to destination
$ traceroute -n 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  192.168.1.1     1.234 ms  1.123 ms  1.089 ms
 2  10.0.0.1        5.678 ms  5.432 ms  5.321 ms
 3  * * *           (no response — firewall blocking ICMP)
 4  8.8.8.8        15.678 ms 15.432 ms 15.321 ms

# MTR — continuous traceroute with statistics
$ mtr -n -c 20 8.8.8.8
                         Packets               Pings
 Host                  Loss%   Snt   Last   Avg  Best  Wrst StDev
 1. 192.168.1.1         0.0%    20    1.2   1.3   1.0   2.1   0.3
 2. 10.0.0.1            0.0%    20    5.4   5.6   5.1   7.2   0.5
 3. 172.16.0.1          5.0%    20   12.3  12.8  11.9  15.4   0.9
 4. 8.8.8.8             0.0%    20   15.2  15.5  14.8  17.1   0.6

# Install mtr if not available
$ sudo apt install -y mtr-tiny

Socket and Connection Analysis

The ss command (socket statistics) is the modern replacement for netstat. It displays detailed information about TCP connections, listening ports, UDP sockets, and Unix domain sockets.

# Show all listening TCP ports
$ ss -tlnp
State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port  Process
LISTEN  0       128     0.0.0.0:22           0.0.0.0:*          users:(("sshd",pid=1234,fd=3))
LISTEN  0       511     0.0.0.0:80           0.0.0.0:*          users:(("nginx",pid=5678,fd=6))
LISTEN  0       128     0.0.0.0:443          0.0.0.0:*          users:(("nginx",pid=5678,fd=7))

# Show all established connections
$ ss -tnp
State   Recv-Q  Send-Q  Local Address:Port   Peer Address:Port   Process
ESTAB   0       0       192.168.1.10:22      192.168.1.5:54312   users:(("sshd",pid=9012,fd=4))

# Show socket summary statistics
$ ss -s
Total: 187
TCP:   12 (estab 3, closed 2, orphaned 0, timewait 1)

# Show connections to a specific port
$ ss -tnp dst :443

# Count connections per state
$ ss -tan | awk '{print $1}' | sort | uniq -c | sort -rn
     45 ESTAB
     12 TIME-WAIT
      6 LISTEN
      3 CLOSE-WAIT

# Check if a specific port is reachable (with nc)
$ nc -zv 192.168.1.20 80
Connection to 192.168.1.20 80 port [tcp/http] succeeded!

# Test HTTP with curl
$ curl -I -s -o /dev/null -w "%{http_code} %{time_total}sn" https://example.com
200 0.234567s

Packet Capture with tcpdump

tcpdump is a powerful command-line packet analyzer. It captures packets on a network interface and displays them in real time or saves them to a file for later analysis.

# Capture all traffic on an interface
$ sudo tcpdump -i ens33 -c 10

# Capture traffic for a specific host
$ sudo tcpdump -i ens33 host 192.168.1.20

# Capture only TCP traffic on port 80
$ sudo tcpdump -i ens33 tcp port 80 -nn

# Capture DNS queries
$ sudo tcpdump -i ens33 udp port 53 -nn
10:15:01 IP 192.168.1.10.45678 > 8.8.8.8.53: 12345+ A? www.example.com.
10:15:01 IP 8.8.8.8.53 > 192.168.1.10.45678: 12345 1/0/0 A 93.184.216.34

# Save capture to a file (for Wireshark analysis)
$ sudo tcpdump -i ens33 -w /tmp/capture.pcap -c 1000

# Read a saved capture file
$ tcpdump -r /tmp/capture.pcap -nn | head -20

# Advanced filters — capture SYN packets only (new connections)
$ sudo tcpdump -i ens33 'tcp[tcpflags] & tcp-syn != 0' -nn

# Capture packets larger than 1000 bytes
$ sudo tcpdump -i ens33 greater 1000 -nn

Port Scanning with nmap

nmap (Network Mapper) is a versatile tool for network discovery and security auditing. It can scan for open ports, detect running services, and identify operating systems.

# Install nmap
$ sudo apt install -y nmap

# Basic TCP port scan
$ nmap 192.168.1.20
PORT    STATE SERVICE
22/tcp  open  ssh
80/tcp  open  http
443/tcp open  https

# Scan specific ports
$ nmap -p 22,80,443,3306 192.168.1.20

# Scan a range of ports
$ nmap -p 1-1024 192.168.1.20

# Service version detection
$ nmap -sV 192.168.1.20
PORT    STATE SERVICE VERSION
22/tcp  open  ssh     OpenSSH 8.9p1 Ubuntu 3ubuntu0.6
80/tcp  open  http    nginx 1.18.0
443/tcp open  ssl     nginx 1.18.0

# UDP scan (slower but important)
$ sudo nmap -sU -p 53,67,68,123 192.168.1.1

# Scan an entire subnet
$ nmap -sn 192.168.1.0/24
Nmap scan report for 192.168.1.1
Host is up (0.0012s latency).
Nmap scan report for 192.168.1.10
Host is up (0.00045s latency).

# OS detection (requires root)
$ sudo nmap -O 192.168.1.20

Key Takeaways

  • Always troubleshoot systematically, working from the physical layer upward: interface status, IP configuration, gateway reachability, DNS resolution, then application connectivity.
  • Use ping for basic reachability, traceroute for path analysis, and mtr for identifying intermittent packet loss at specific hops.
  • The ss command is the modern tool for inspecting sockets and connections; combine it with nc and curl for port and service testing.
  • tcpdump captures packets at the wire level — invaluable for diagnosing protocol-level issues; save captures to .pcap files for detailed Wireshark analysis.
  • nmap discovers open ports, running services, and operating systems — essential for security auditing and verifying firewall configurations.

What’s Next

Congratulations on completing Module 2 — TCP/IP and Networking! You now have a solid foundation in network protocols, configuration, DNS, DHCP, firewalls, and troubleshooting. In Module 3, you will advance to Storage and File Systems, where you will learn about disk partitioning, LVM, file system types, and RAID configurations.

繁體中文

概述

  • 學習目標:系統化的網路排除問題方法,以及如何使用 ping、traceroute、mtr、ss、netstat、curl、tcpdump、nmap 和 tshark/Wireshark 來診斷連線、效能和安全問題。
  • 先決條件:第 19 課 – 使用 ufw 和 iptables 的防火牆
  • 預計閱讀時間:25 分鐘

簡介

網路問題是系統管理中最常見且最令人沮喪的問題之一。無法連接網際網路的伺服器、超時的服務或神秘的封包遺失可能使整個系統停擺。有效排除問題的關鍵是結合正確工具的系統化、逐層方法。

Linux 提供了極其豐富的網路診斷工具集,從簡單的 ping 工具到強大的封包分析器 tcpdump。掌握這些工具可以讓您快速識別問題是在實體層、網路層、傳輸層還是應用層。

在本課程中,您將建立全面的排除問題方法論,並學習如何透過實際範例有效地使用每個工具。

系統化排除問題方法

診斷網路問題時,從 OSI 模型的底部向上工作。這種逐層方法確保您不會在問題實際上是斷線或路由設定錯誤時浪費時間在應用層除錯上。

  • 第 1 層(實體層):纜線是否連接?介面是否啟動?檢查 ip link show
  • 第 2 層(資料鏈結層):能看到閘道的 MAC 位址嗎?檢查 ip neigh show
  • 第 3 層(網路層):能 ping 通閘道嗎?能 ping 通外部 IP 嗎?檢查 pingip route
  • 第 4 層(傳輸層):連接埠是否開放?服務是否在監聽?檢查 ssnc
  • 第 7 層(應用層):應用程式是否正確回應?檢查 curl 和應用程式日誌。

Ping、Traceroute 和 MTR

ping 透過發送 ICMP 回應請求來測試基本 IP 連線。traceroute 顯示封包到達目的地的路徑,展示每一跳。mtr 將兩者結合為連續的即時顯示,非常適合識別間歇性封包遺失。

Socket 和連線分析

ss 命令(socket 統計)是 netstat 的現代替代品。它顯示有關 TCP 連線、監聽連接埠、UDP socket 和 Unix 域 socket 的詳細資訊。

使用 tcpdump 封包擷取

tcpdump 是一個強大的命令列封包分析器。它在網路介面上擷取封包並即時顯示或儲存到檔案以供後續分析。

使用 nmap 連接埠掃描

nmap(Network Mapper)是一個多功能的網路探索和安全稽核工具。它可以掃描開放的連接埠、偵測運行的服務並識別作業系統。

重要觀念

  • 始終系統化地排除問題,從實體層向上工作:介面狀態、IP 設定、閘道可達性、DNS 解析,然後是應用程式連線。
  • 使用 ping 進行基本可達性測試,traceroute 進行路徑分析,mtr 識別特定跳點的間歇性封包遺失。
  • ss 命令是檢查 socket 和連線的現代工具;結合 nccurl 進行連接埠和服務測試。
  • tcpdump 在線路級別擷取封包——對診斷協定級別問題非常寶貴;將擷取儲存為 .pcap 檔案以進行詳細的 Wireshark 分析。
  • nmap 發現開放連接埠、運行的服務和作業系統——對安全稽核和驗證防火牆設定至關重要。

下一步

恭喜您完成模組 2 — TCP/IP 與網路!您現在擁有網路協定、設定、DNS、DHCP、防火牆和排除問題的堅實基礎。在模組 3 中,您將進入儲存與檔案系統,學習磁碟分割、LVM、檔案系統類型和 RAID 設定。

日本語

概要

  • 学習内容:体系的なネットワークトラブルシューティングアプローチ、ping、traceroute、mtr、ss、netstat、curl、tcpdump、nmap、tshark/Wiresharkを使用した接続性、パフォーマンス、セキュリティの問題の診断方法。
  • 前提条件:レッスン19 – ufwとiptablesによるファイアウォール
  • 推定読了時間:25分

はじめに

ネットワークの問題は、システム管理で最も一般的でフラストレーションの多い問題です。インターネットに接続できないサーバー、タイムアウトするサービス、不可解なパケットロスは、システム全体を停止させる可能性があります。効果的なトラブルシューティングの鍵は、適切なツールと組み合わせた体系的なレイヤーごとのアプローチです。

Linuxは、シンプルなpingユーティリティから強力なパケットアナライザーtcpdumpまで、非常に豊富なネットワーク診断ツールを提供しています。これらのツールをマスターすることで、問題が物理層、ネットワーク層、トランスポート層、アプリケーション層のどこにあるかを迅速に特定できます。

このレッスンでは、包括的なトラブルシューティング方法論を構築し、実世界の例を使って各ツールを効果的に使用する方法を学びます。

体系的トラブルシューティングアプローチ

ネットワーク問題を診断する際は、OSIモデルの下位層から上位層に向かって作業します。このレイヤーごとのアプローチにより、問題が実際にはケーブルの断線やルートの設定ミスである場合に、アプリケーション層のデバッグに時間を無駄にすることがありません。

  • レイヤー1(物理層):ケーブルは接続されていますか?インターフェースは起動していますか?ip link showで確認。
  • レイヤー2(データリンク層):ゲートウェイのMACアドレスが見えますか?ip neigh showで確認。
  • レイヤー3(ネットワーク層):ゲートウェイにpingできますか?外部IPにpingできますか?pingip routeで確認。
  • レイヤー4(トランスポート層):ポートは開いていますか?サービスはリッスンしていますか?ssncで確認。
  • レイヤー7(アプリケーション層):アプリケーションは正しく応答していますか?curlとアプリケーションログで確認。

Ping、Traceroute、MTR

pingはICMPエコーリクエストを送信して基本的なIP接続性をテストします。tracerouteはパケットが宛先に到達するまでのパスを表示し、各ホップを示します。mtrは両方を連続的なリアルタイム表示に統合し、間欠的なパケットロスの特定に最適です。

ソケットと接続の分析

ssコマンド(ソケット統計)はnetstatの現代的な代替です。TCP接続、リスニングポート、UDPソケット、Unixドメインソケットに関する詳細な情報を表示します。

tcpdumpによるパケットキャプチャ

tcpdumpは強力なコマンドラインパケットアナライザーです。ネットワークインターフェース上のパケットをキャプチャし、リアルタイムで表示するか、後で分析するためにファイルに保存します。

nmapによるポートスキャン

nmap(Network Mapper)はネットワーク探索とセキュリティ監査のための多目的ツールです。開いているポートのスキャン、実行中のサービスの検出、オペレーティングシステムの識別ができます。

重要ポイント

  • 常に体系的にトラブルシューティングし、物理層から上位層に向かって作業します:インターフェース状態、IP設定、ゲートウェイの到達可能性、DNS解決、そしてアプリケーション接続性。
  • 基本的な到達可能性にはping、パス分析にはtraceroute、特定ホップでの間欠的なパケットロスの特定にはmtrを使用します。
  • ssコマンドはソケットと接続を検査するための現代的なツールです。ポートとサービスのテストにはnccurlを組み合わせて使用します。
  • tcpdumpはワイヤレベルでパケットをキャプチャします——プロトコルレベルの問題の診断に非常に貴重です。詳細なWireshark分析のためにキャプチャを.pcapファイルに保存します。
  • nmapは開いているポート、実行中のサービス、オペレーティングシステムを検出します——セキュリティ監査とファイアウォール設定の検証に不可欠です。

次のステップ

モジュール2 — TCP/IPとネットワーキングの完了おめでとうございます!ネットワークプロトコル、設定、DNS、DHCP、ファイアウォール、トラブルシューティングの確固たる基盤ができました。モジュール3では、ストレージとファイルシステムに進み、ディスクパーティショニング、LVM、ファイルシステムタイプ、RAID構成について学びます。

You Missed