Skip to main content

Using Linux Networking Tools to Diagnose Issues

Use ip, ss, dig, curl, mtr, and tcpdump to find where a Linux network problem lives, from interfaces and DNS to ports, routes, and packets.

30 Terms

Overview and What You Will Learn

Linux networking tools are small command-line programs that each inspect one layer of the network path: interfaces and routes (ip), sockets and ports (ss), name resolution (dig), HTTP (curl), the path between hosts (ping, mtr), and raw packets (tcpdump). Picking the right one first is what turns a 20-minute guessing session into a 2-minute diagnosis.

By the end of this lab you will:

  • Inspect interfaces, addresses, and routes with ip
  • Find listening services and connection states with ss
  • Diagnose DNS failures with dig and resolvectl
  • Test HTTP endpoints and break latency into DNS, connect, TLS, and first byte with curl
  • Test ports with nc and trace the path with ping, traceroute, and mtr
  • Capture and read packets with tcpdump
  • Follow a layered checklist from "service unreachable" to root cause

Why This Matters in Production

Network incidents are stressful because the same symptom, "timeout", can come from DNS, routing, a firewall, a stopped service, or an overloaded dependency. An engineer who knows one tool per layer isolates the cause quickly. An engineer who only knows ping often escalates an hour too early.

Teams running large request volumes, such as Cloudflare and Netflix, rely on this same layered approach: confirm the name resolves, confirm the port answers, confirm the path is clean, and only then capture packets.

Core Principles

Work from the bottom of the problem upward, and test each layer separately. Each question below has one tool that answers it.

flowchart TD A([Service unreachable]) --> B{Does the name
resolve?} B -->|No| C[dig, resolvectl
DNS layer] B -->|Yes| D{Is the port
open?} D -->|No| E[ss, nc, curl -v
service or firewall] D -->|Yes| F{Is the path
clean?} F -->|No| G[mtr, ip route get
routing layer] F -->|Yes| H[tcpdump
inspect packets] classDef start fill:#475569,color:#ffffff classDef decide fill:#DBEAFE,color:#1E293B classDef tool fill:#326CE5,color:#ffffff classDef deep fill:#8B5CF6,color:#ffffff class A start class B,D,F decide class C,E,G tool class H deep
Layer Question Tool
Interface Is the NIC up with the right address? ip addr, ip -s link
Routing Where does traffic to this IP go? ip route get, mtr
DNS Does the name resolve to the right IP? dig, resolvectl
Transport Is anything listening, is the port reachable? ss, nc
Application Does the HTTP request succeed, and how fast? curl -v
Packets What is actually on the wire? tcpdump
Remember

ifconfig, netstat, and route are legacy tools. The modern replacements are ip and ss, which ship on every current distribution.

Detailed Step-by-Step Practical Lab

Milestone 1: How to Inspect Interfaces and Routes with ip
Bash
ip -br addr
# Full view of one interface
ip addr show eth0
# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP
# inet 10.0.1.50/24 brd 10.0.1.255 scope global eth0
# Routing table and default gateway
ip route show
# default via 10.0.1.1 dev eth0
# 10.0.1.0/24 dev eth0 proto kernel scope link src 10.0.1.50
# Which route and source address will this destination use?
ip route get 10.0.2.100
# Interface counters: non-zero errors or drops point to link problems
ip -s link show eth0
# Temporary changes (lost on reboot)
sudo ip addr add 10.0.1.100/24 dev eth0
sudo ip addr del 10.0.1.100/24 dev eth0
sudo ip route add 192.168.10.0/24 via 10.0.1.1
Tip

ip route get DEST answers "which interface and gateway will this packet use?" directly, which is faster than reading the whole routing table.

Changes made with ip do not survive a reboot. Make permanent ones in netplan, NetworkManager, or your cloud's network configuration.

Milestone 2: How to Find Listening Ports and Connections with ss
Bash
# Listening TCP and UDP sockets with owning processes
sudo ss -tulpn
# Netid State Local Address:Port Process
# tcp LISTEN 0.0.0.0:22 sshd
# tcp LISTEN 0.0.0.0:443 nginx
# tcp LISTEN 127.0.0.1:5432 postgres
# Is anything listening on port 4000?
sudo ss -tlpn | grep ':4000'
# Established connections to PostgreSQL
ss -tn state established '( dport = :5432 or sport = :5432 )'
# Count TCP connections by state
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn
# 2450 ESTAB
# 342 CLOSE-WAIT
# 12 TIME-WAIT

The address matters as much as the port. 127.0.0.1:5432 accepts only local connections, while 0.0.0.0:5432 accepts connections from any interface. A service "listening" on loopback is unreachable from other hosts.

A large and growing number of CLOSE-WAIT sockets usually means the application is not closing connections it received.

Milestone 3: How to Diagnose DNS with dig
Bash
# Full answer, then just the address
dig payments-api.mumbai-prod.devops.in
dig +short payments-api.mumbai-prod.devops.in
# Other record types
dig MX devops.in
dig NS devops.in
dig TXT devops.in
dig AAAA payments-api.mumbai-prod.devops.in
# Ask a specific resolver, bypassing the local one
dig @8.8.8.8 payments-api.mumbai-prod.devops.in
dig @1.1.1.1 payments-api.mumbai-prod.devops.in
# Follow resolution from the root servers
dig +trace payments-api.mumbai-prod.devops.in
# Reverse lookup
dig -x 10.0.2.100
# What resolver is this host actually using?
resolvectl status
cat /etc/resolv.conf

On many systems /etc/resolv.conf points to a local stub such as 127.0.0.53. In that case resolvectl status shows the real upstream DNS servers.

Remember

If dig @8.8.8.8 NAME works but the application still fails, the problem is the local resolver configuration, search domains, or a cached answer, not the name itself.

Milestone 4: How to Test HTTP Endpoints and Latency with curl
Bash
# Verbose request: shows DNS, TLS handshake, request and response headers
curl -v https://payments-api.mumbai-prod.devops.in/health
# Response headers only
curl -I https://payments-api.mumbai-prod.devops.in/health
# POST with a JSON body (store tokens in an environment variable)
curl -X POST https://payments-api.mumbai-prod.devops.in/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d '{"amount": 1000, "currency": "INR"}'
# Follow redirects and fail on HTTP errors
curl -fsSL https://devops.in/
# Test a new IP before DNS changes propagate
curl --resolve payments-api.mumbai-prod.devops.in:443:10.0.2.100 \
https://payments-api.mumbai-prod.devops.in/health
# Retry, with a hard timeout, for health-check scripts
curl --retry 3 --retry-delay 2 --max-time 10 -fsS https://payments-api.mumbai-prod.devops.in/health

To see where time goes, save a timing format and reuse it:

Bash
cat > ~/curl-timing.txt << 'EOF'
dns: %{time_namelookup}s
connect: %{time_connect}s
tls: %{time_appconnect}s
ttfb: %{time_starttransfer}s
total: %{time_total}s
code: %{http_code}
EOF
curl -o /dev/null -s -w "@$HOME/curl-timing.txt" https://payments-api.mumbai-prod.devops.in/health

A slow dns value points at the resolver, a slow connect at the network or a full accept queue, a slow tls at the handshake, and a slow ttfb at the application.

Milestone 5: How to Check Connectivity with ping, nc, and mtr
Bash
# Reachability and round-trip time
ping -c 5 10.0.2.100
# Is the TCP port open? (this tests the service, ping does not)
nc -zv -w 2 10.0.2.100 5432
# Connection to 10.0.2.100 5432 port [tcp/postgresql] succeeded!
# Check several ports
for port in 80 443 5432 6379; do
if nc -z -w 2 10.0.2.100 "$port"; then echo "port $port: open"; else echo "port $port: closed or filtered"; fi
done
# Hop-by-hop path, one pass
traceroute 10.0.2.100
# Continuous path analysis with loss and latency per hop
sudo apt install mtr-tiny
mtr --report --report-cycles 10 10.0.2.100

Read mtr from the destination backward. Loss that appears at one hop and continues to the end is real. Loss at a single middle hop that disappears afterward is usually a router rate-limiting its own replies and is harmless.

Common Mistake

Using ping to decide whether a service is reachable. ICMP and TCP are independent, so a firewall can allow ping while blocking port 443, or the reverse. Test the actual port with nc -zv HOST PORT or curl -v.

Milestone 6: How to Capture Packets with tcpdump
Bash
sudo apt install tcpdump
# Capture on one interface, numeric output (no DNS or port-name lookups)
sudo tcpdump -i eth0 -nn
# Filter by port, host, or both
sudo tcpdump -i eth0 -nn port 5432
sudo tcpdump -i eth0 -nn 'host 10.0.2.100 and port 5432'
# Stop after 100 packets
sudo tcpdump -i eth0 -nn -c 100 port 443
# Watch DNS queries
sudo tcpdump -i eth0 -nn port 53
# Save for Wireshark, rotating into 100 MB files and keeping five
sudo tcpdump -i eth0 -nn -w /var/tmp/capture.pcap -C 100 -W 5 'host 10.0.2.100'
# Read a saved capture
sudo tcpdump -nn -r /var/tmp/capture.pcap
# Only TCP SYN packets, useful for spotting connection attempts
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn) != 0 and port 5432'

Read the TCP flags to understand what happened. Repeated [S] packets with no [S.] reply mean the SYN is being dropped or the host is not answering. An immediate [R.] means the host is reachable but nothing listens on that port.

Security

tcpdump can capture credentials and personal data from unencrypted traffic. Capture only what you need with a narrow filter, store files in a restricted directory with mode 600, delete them after analysis, and follow your change-management rules in regulated environments such as PCI-DSS or SOC 2.

Production Best Practices & Common Pitfalls

Diagnose layer by layer, change one thing at a time, and write down what each command showed.

  1. Start with DNS and port tests before capturing packets, because they are faster and safer.
  2. Compare from two vantage points: the failing host and a healthy host.
  3. Use ss -tulpn to confirm the service listens on the right address, not only the right port.
  4. Use mtr --report for intermittent problems instead of a single ping.
  5. Keep captures short, filtered, and deleted promptly.
Problem Wrong first move Better first move
Name not resolving ping NAME dig NAME, then dig @8.8.8.8 NAME
Port not reachable Restart the service ss -tulpn to see if it listens
HTTP errors Read logs only curl -v to see the full exchange
Intermittent loss One ping mtr --report
Unknown traffic source Guess tcpdump -nn with a narrow filter

Quick Reference & Troubleshooting Commands

Task Command
Show addresses ip -br addr
Route for a destination ip route get IP
Listening ports sudo ss -tulpn
DNS lookup dig +short NAME
Test another resolver dig @8.8.8.8 NAME
HTTP with details curl -v URL
Port check nc -zv HOST PORT
Path with loss stats mtr --report HOST
Capture on a port sudo tcpdump -i eth0 -nn port PORT
Symptom First command What to look for
Host cannot reach anything ip -br addr && ip route show Missing address or default route
Name fails to resolve resolvectl status Wrong or unreachable DNS server
Connection refused sudo ss -tlpn | grep PORT Nothing listening, or loopback only
Connection times out nc -zv -w 2 HOST PORT Filtered port, firewall drop
Slow responses curl -w "@$HOME/curl-timing.txt" -o /dev/null -s URL Which phase is slow
Intermittent failures mtr --report HOST Loss that persists to the destination
Tip

mtr combines ping and traceroute and updates continuously. If loss starts at one hop and continues through every hop after it, that hop or the link just after it is the problem.

Remember

Refused and timed out are different answers. Refused means the host replied that nothing listens there. A timeout means the packets were dropped or never answered, which usually points to a firewall or a routing problem.

Resources

Explore More in Linux Networking and Security

All 4 Topics

Frequently Asked Questions

Is Using Linux Networking Tools to Diagnose Issues free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Using Linux Networking Tools to Diagnose Issues topic cover?

Use ip, ss, dig, curl, mtr, and tcpdump to find where a Linux network problem lives, from interfaces and DNS to ports, routes, and packets.

Found a mistake, an outdated step or a broken link?