Network Troubleshooting บน Linux — ping, traceroute, ss และ tcpdump

เมื่อเครือข่ายมีปัญหา — SSH เชื่อมต่อไม่ได้, เว็บโหลดช้า, Packet หาย หรือ DNS ไม่ตอบสนอง — ผู้ดูแลระบบต้องมีเครื่องมือสำหรับวิเคราะห์หาสาเหตุได้อย่างเป็นระบบ Linux มีชุดเครื่องมือ Network Troubleshooting ครบครัน ตั้งแต่ ping ที่ทดสอบ Connectivity, traceroute ที่แสดง Path ของ Packet, netstat/ss ที่ดู Connection และ Port ที่เปิดอยู่ ไปจนถึง tcpdump ที่ Capture Packet ได้โดยตรง

บทความนี้อธิบายการใช้งานเครื่องมือ Network Troubleshooting บน Linux ตั้งแต่การทดสอบ Connectivity เบื้องต้น, การตรวจสอบ Route, การดู Open Ports และ Active Connections, การวิเคราะห์ DNS รวมถึง Workflow การ Troubleshoot แบบเป็นขั้นตอน

ping — ทดสอบ Connectivity

ping ส่ง ICMP Echo Request ไปยัง Host ปลายทาง ใช้ทดสอบว่า Host ตอบสนองได้และวัด Round-Trip Time (RTT)

# ping พื้นฐาน
ping google.com
ping 8.8.8.8

# ส่งจำนวนที่กำหนด (-c)
ping -c 4 google.com

# กำหนด Interval ระหว่าง Packet (-i วินาที)
ping -i 0.2 -c 10 google.com    # ส่งทุก 0.2 วินาที

# กำหนดขนาด Packet (-s bytes)
ping -s 1400 -c 4 google.com    # ทดสอบ MTU

# ระบุ Interface ที่ใช้ส่ง
ping -I eth0 -c 4 8.8.8.8

# Flood ping (root เท่านั้น) — ทดสอบ bandwidth
sudo ping -f -c 1000 192.168.1.1

# ดู Statistics
ping -c 10 google.com
# Output: 10 packets transmitted, 9 received, 10% packet loss
# rtt min/avg/max/mdev = 1.234/5.678/12.345/3.456 ms

อ่านผลลัพธ์ ping

# ผลลัพธ์ที่ดี:
# 64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=1.23 ms

# Packet Loss สูง → Network ไม่เสถียร หรือ ICMP ถูก Block
# RTT สูง → Network ช้า, Latency สูง
# "Destination Host Unreachable" → ไม่มี Route ไปถึง Host นั้น
# "Request timeout" → Host ไม่ตอบสนอง (อาจ Block ICMP หรือ Down)

# ตีความ RTT:
# < 1ms    → Local Network
# 1-10ms   → ภายใน Data Center เดียวกัน
# 10-50ms  → ต่างประเทศ ใกล้เคียง
# 50-200ms → ต่างประเทศ ระยะไกล
# > 200ms  → ช้ามาก ต้องตรวจสอบ

traceroute / tracepath — ดู Path ของ Packet

traceroute แสดง Router ทุกตัวที่ Packet ผ่านจาก Source ไปถึง Destination ช่วยหาว่าปัญหาเกิดที่จุดไหนของเส้นทาง

# ติดตั้ง traceroute (ถ้ายังไม่มี)
sudo apt install traceroute    # Ubuntu/Debian
sudo dnf install traceroute    # RHEL/Rocky

# traceroute พื้นฐาน
traceroute google.com

# ใช้ ICMP แทน UDP (เหมาะกับ Firewall บางตัว)
traceroute -I google.com

# ใช้ TCP SYN (เหมาะกับ Host ที่ Block ICMP/UDP)
traceroute -T -p 80 google.com

# กำหนด Max Hops
traceroute -m 20 google.com

# tracepath — ไม่ต้อง root
tracepath google.com

# mtr — Real-time traceroute (แนะนำ)
sudo apt install mtr
mtr google.com
mtr --report -n -c 10 google.com    # Report mode ไม่ Resolve DNS
# อ่านผลลัพธ์ traceroute:
# traceroute to google.com, 30 hops max
#  1  192.168.1.1 (Gateway)    0.5 ms   0.4 ms   0.5 ms
#  2  10.0.0.1    (ISP Router)  5.1 ms   5.0 ms   5.2 ms
#  3  * * *                    (No response — Firewall block ICMP)
#  4  172.217.x.x (Google)    10.2 ms  10.1 ms  10.3 ms

# * * * = Router ไม่ตอบ ICMP — ไม่ได้หมายความว่า Down
# RTT เพิ่มขึ้นกะทันที = Bottleneck ที่ Hop นั้น
# Hop หยุดที่ตรงกลางและไม่ไปต่อ = Routing Problem

netstat และ ss — ดู Connection และ Ports

ss เป็นเครื่องมือรุ่นใหม่ที่เร็วกว่า netstat ใช้สำหรับดู Socket Statistics, Open Ports และ Active Connections บน Linux รุ่นใหม่

# ดู Connection ทั้งหมดที่ LISTEN
ss -tlnp
# -t = TCP, -l = LISTEN, -n = Numeric port, -p = Show Process

# ดู UDP ที่เปิดอยู่
ss -ulnp

# ดู Connection ทั้งหมด (ESTABLISHED + LISTEN)
ss -tnp

# ดู Connection ของ Port เฉพาะ
ss -tnp | grep :80
ss -tnp | grep :443

# ดู Connection ไปยัง IP เฉพาะ
ss -tnp dst 8.8.8.8

# นับ Connection ที่ ESTABLISHED
ss -tn | grep ESTABLISHED | wc -l

# ดู Connection ที่ TIME_WAIT (มากเกินไปอาจเป็นปัญหา)
ss -tn | grep TIME_WAIT | wc -l

# netstat (รุ่นเก่า แต่ยังใช้ได้)
netstat -tlnp    # TCP LISTEN ports
netstat -an | grep :80
# ตัวอย่าง Output ของ 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))
LISTEN  0      511     0.0.0.0:80         0.0.0.0:*          users:(("nginx",pid=5678))
LISTEN  0      511     0.0.0.0:443        0.0.0.0:*          users:(("nginx",pid=5678))

# อ่านผลลัพธ์:
# 0.0.0.0:22  = Listen บนทุก IP, Port 22 (SSH)
# :::80       = Listen บน IPv6 ทุก IP, Port 80
# users:sshd  = Process ที่เปิด Port นั้น

nslookup และ dig — ทดสอบ DNS

# nslookup พื้นฐาน
nslookup google.com
nslookup google.com 8.8.8.8    # ระบุ DNS Server

# dig — ละเอียดกว่า nslookup
dig google.com
dig google.com @8.8.8.8        # Query ผ่าน DNS Server เฉพาะ
dig google.com A               # เฉพาะ A record (IPv4)
dig google.com AAAA            # IPv6
dig google.com MX              # Mail Server
dig google.com TXT             # TXT record
dig -x 8.8.8.8                 # Reverse DNS (PTR)

# ดูเฉพาะผลลัพธ์ (Short)
dig +short google.com
dig +short google.com MX

# ทดสอบว่า DNS ทุกตัวใน /etc/resolv.conf ตอบเหมือนกัน
dig google.com @1.1.1.1 +short
dig google.com @8.8.8.8 +short

# ดู DNS Resolution Time
dig google.com | grep "Query time"

# ตรวจสอบ DNSSEC
dig google.com +dnssec

curl และ wget — ทดสอบ HTTP/HTTPS

# ทดสอบ HTTP Response
curl -I https://google.com     # Header only
curl -v https://google.com     # Verbose (ดู TLS handshake)
curl -o /dev/null -w "%{http_code} %{time_total}s\n" https://google.com

# ทดสอบเฉพาะ IP (ไม่ผ่าน DNS)
curl -v --resolve "google.com:443:142.250.x.x" https://google.com

# ดู Certificate ของ HTTPS
curl -v https://google.com 2>&1 | grep -E "SSL|TLS|certificate"

# ทดสอบ Timeout
curl --connect-timeout 5 --max-time 10 https://google.com

# wget ทดสอบ Download
wget -O /dev/null https://speed.cloudflare.com/__down?bytes=10000000

tcpdump — Capture Packets

tcpdump เป็นเครื่องมือ Packet Capture ระดับ Low-level ที่ให้ข้อมูลละเอียดที่สุด เหมาะสำหรับ Debug ปัญหาที่ซับซ้อน

# ติดตั้ง tcpdump
sudo apt install tcpdump       # Ubuntu/Debian
sudo dnf install tcpdump       # RHEL/Rocky

# Capture ทุก Packet บน Interface eth0
sudo tcpdump -i eth0

# Capture เฉพาะ Port 80
sudo tcpdump -i eth0 port 80

# Capture เฉพาะ IP ปลายทาง
sudo tcpdump -i eth0 host 8.8.8.8

# Capture HTTP Traffic
sudo tcpdump -i eth0 -A port 80    # -A แสดงเนื้อหา ASCII

# บันทึก Capture ลงไฟล์ (เปิดด้วย Wireshark ได้)
sudo tcpdump -i eth0 -w /tmp/capture.pcap

# อ่านไฟล์ Capture
tcpdump -r /tmp/capture.pcap

# Capture เฉพาะ SYN Packets (การเปิด Connection ใหม่)
sudo tcpdump -i eth0 'tcp[tcpflags] & tcp-syn != 0'

# Capture DNS Queries
sudo tcpdump -i eth0 udp port 53

Workflow การ Troubleshoot เครือข่าย

เมื่อพบปัญหาเครือข่าย ให้ตรวจสอบเป็นลำดับจาก Layer ล่างขึ้นบน:

# Step 1: ตรวจสอบ Physical/Link Layer
ip link show                  # Interface UP หรือ DOWN?
cat /sys/class/net/eth0/operstate    # "up" หรือ "down"

# Step 2: ตรวจสอบ IP Address และ Route
ip addr show                  # มี IP หรือไม่?
ip route show                 # มี Default Route หรือไม่?

# Step 3: ทดสอบ Local Network
ping -c 3 192.168.1.1         # Gateway ตอบสนองหรือไม่?

# Step 4: ทดสอบ Internet (IP)
ping -c 3 8.8.8.8             # ถ้าไม่ได้ — ปัญหา Routing/ISP
                              # ถ้าได้ — ปัญหาอาจเป็น DNS

# Step 5: ทดสอบ DNS
ping -c 3 google.com          # ถ้าไม่ได้ — ปัญหา DNS Resolution
nslookup google.com

# Step 6: ทดสอบ Application Layer
curl -I https://google.com    # HTTP/HTTPS ทำงานหรือไม่?

# Step 7: ตรวจสอบ Firewall
sudo iptables -L -n           # iptables rules
sudo firewall-cmd --list-all  # firewalld (RHEL)
sudo ufw status               # ufw (Ubuntu)

เครื่องมือเสริม

# nmap — Port Scanner
sudo apt install nmap
nmap -p 22,80,443 192.168.1.1      # Scan Port เฉพาะ
nmap -sV 192.168.1.1               # Detect Service Version
nmap -p 1-65535 192.168.1.1        # Scan ทุก Port (ช้า)

# iperf3 — ทดสอบ Network Bandwidth
sudo apt install iperf3
# บน Server: รัน Server
iperf3 -s
# บน Client: ทดสอบ
iperf3 -c 192.168.1.100 -t 10     # ทดสอบ 10 วินาที

# lsof — ดู File/Socket ที่ Process เปิดอยู่
lsof -i :80                        # Process ที่ใช้ Port 80
lsof -i TCP -n                     # TCP Connections ทั้งหมด
lsof -p 1234 -i                    # Network Files ของ PID 1234

# ip neigh show — ดู ARP Table
ip neigh show

สรุป

การ Troubleshoot เครือข่ายบน Linux ควรทำเป็นลำดับจาก Physical Layer ขึ้นไปถึง Application Layer — ตรวจ Interface → IP/Route → Gateway → DNS → Application ping ใช้ทดสอบ Connectivity และวัด Latency, traceroute/mtr ใช้หา Bottleneck บน Path, ss -tlnp ใช้ดู Open Ports และ Process ที่เปิดอยู่, dig ใช้ทดสอบ DNS Resolution แบบละเอียด และ tcpdump ใช้ Capture Packet สำหรับวิเคราะห์เชิงลึก

แนะนำบริการ DE

การ Troubleshoot เครือข่ายบน Server ต้องการสิทธิ์ Root เพื่อรัน tcpdump, iptables และเครื่องมืออื่น Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบพร้อม Monitoring Dashboard ที่แสดง Network Traffic แบบ Real-time ช่วยให้ตรวจสอบปัญหาเครือข่ายได้สะดวก

หากต้องการโฮสต์เว็บโดยไม่ต้องจัดการ Network เอง Cloud Hosting ของ DE มีทีม Support พร้อมช่วยแก้ปัญหาเครือข่ายและมี Uptime SLA รับประกัน