DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Linux & BashLesson 15 of 16
Your progress · 0%
On this page
  1. A method, not guesses
  2. ping: is the host there?
  3. traceroute and mtr: where does it stop?
  4. ss: what is listening?
  5. Refused or timeout?
  6. tcpdump: see the real packets
  7. Firewalls: nftables and ufw
↖ Course roadmap
Lesson 15 · Troubleshooting

Troubleshooting the network: from "it is down" to the cause

A step-by-step method for network problems: ping and ICMP, traceroute, ss to see what listens, connection refused versus timeout, tcpdump to see real packets, and firewalls with nftables and ufw.

40 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 7 complete

A method, not guesses

A typical incident. It's 11 p.m. The app logs say "could not connect to database". The app team says the database is down. The database team says it's up and they can connect fine. Someone restarts the app, which changes nothing. Someone else restarts the database, which takes the other apps down too. An hour later, someone finds a firewall rule that was changed that afternoon.

All of that guessing could have been five minutes of checking. "The app can't reach the database" has a dozen possible causes. Checking them in a fixed order, from the bottom up, finds the cause quickly and proves it, which matters when you tell another team the problem is on their side.

Bottom to top. The first rung that fails is where the problem is.© Diagram: DevOps Academy

Bottom to top. The first rung that fails is where the problem is.© Diagram: DevOps Academy

The first three rungs are in the Networking lesson. This lesson is about the rest: what's reachable, what's listening, and what's in the way.

ping: is the host there?

ping tests whether a host answers ICMP echo requests and reports round-trip time and packet loss. It is a quick reachability signal, but it does not test the application's TCP port or HTTP health.

$ ping -c 3 1.1.1.1
64 bytes from 1.1.1.1: icmp_seq=1 ttl=57 time=41.8 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=57 time=33.6 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=57 time=33.6 ms
--- 1.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 33.625/36.358/41.818/3.860 ms

ping sends ICMP echo requests. Replies prove the host is reachable; the times show latency and any packet loss points to a bad link. A failed ping proves much less: AWS security groups and many firewalls block ICMP by default, so a perfectly healthy server may not answer. Always go on to test the real port.

Use a short count such as -c 3 during an investigation so the command stops on its own. Compare loss and latency across several destinations, then test the actual service port. A common mistake is declaring a server down because ping is blocked, or declaring an application healthy because ping succeeds.

Linux Crash Course - The ping CommandLearn Linux TV · Watch on YouTube ↗

traceroute and mtr: where does it stop?

When the destination does not respond, a route trace shows which network hops reply along the path. traceroute sends probes with increasing hop limits; mtr repeats the test and summarizes loss and latency over time.

$ traceroute -n 1.1.1.1           # each router (hop) on the way, and its delay
$ mtr -rwc 10 1.1.1.1             # traceroute and ping combined: loss per hop

Each line is one router. Where the hops stop answering is roughly where the path breaks, inside your network, your provider's, or theirs. * * * on one hop in the middle is usually a router that ignores these probes, not a failure: only loss that continues to the last hop is real. Install them with sudo apt install traceroute mtr-tiny; tracepath needs no root.

Use traces to collect evidence about where replies stop, not as proof that the non-responding router is broken. Routers often forward traffic while ignoring diagnostic probes. If later hops and the destination respond, the path is working despite asterisks in the middle. A common mistake is reporting the first silent hop as the root cause without checking the final destination.

ss: what is listening?

ss shows sockets: what listens and what's connected. It replaced the older netstat. Use it to check whether the expected process has opened a local port before blaming a firewall or remote network.

$ sudo ss -tlnp                   # t TCP, l listening, n numbers, p process
State  Recv-Q Send-Q  Local Address:Port  Peer Address:Port Process
LISTEN 0      4096    127.0.0.53%lo:53         0.0.0.0:*     users:(("systemd-resolve",…))
LISTEN 0      5             0.0.0.0:8099       0.0.0.0:*     users:(("python3",pid=100308,fd=3))
$ ss -tn state established        # current connections
$ ss -s                           # totals: how many connections of each state
$ sudo ss -tlnp 'sport = :443'    # just one port
sudo ss -tlnp
  • sudorun the next command as root

Read the Local Address column: 0.0.0.0 or * accepts connections on every address, 127.0.0.1 only from this machine. The single most common "works on the server, not from outside" bug is an app bound to 127.0.0.1.

Use -l for listeners, -t for TCP, -n to avoid name lookups and -p to show the process when permitted. Check the bind address as well as the port: a listener on loopback is reachable only from that host. A common mistake is looking at a UDP socket while troubleshooting TCP, or assuming ss shows cloud security-group rules; it only reports local kernel sockets.

How to Use the ss Command (Linux Crash Course Series)Learn Linux TV · Watch on YouTube ↗

Refused or timeout?

The connection error often tells you whether the packet reached a host. A refusal usually means the host sent back a reset because nothing accepted the port. A timeout means the client received no answer before its deadline, which can happen when packets are dropped or the path is broken.

$ nc -zv -w 2 127.0.0.1 8099
Connection to 127.0.0.1 8099 port [tcp/*] succeeded!
$ nc -zv -w 2 127.0.0.1 8098
nc: connect to 127.0.0.1 port 8098 (tcp) failed: Connection refused
$ nc -zv -w 3 10.0.2.20 5432
nc: connect to 10.0.2.20 port 5432 (tcp) timed out: Operation now in progress

Why the two failures look different. When you connect to a port, your machine sends a "can I connect?" packet (a TCP SYN). If it reaches the server and nothing is listening on that port, the server's kernel immediately replies "no" (a TCP reset), and you see Connection refused in a fraction of a second. If a firewall or security group sits in between and is set to drop traffic, nobody replies at all. Your machine waits, retries, and gives up: timed out. So a refusal proves you reached the host, and a timeout tells you something in the path is swallowing your packets.

Refused: you reached the host, look at the service. Timed out: something in the path dropped it, look at firewalls.© Diagram: DevOps Academy

Refused: you reached the host, look at the service. Timed out: something in the path dropped it, look at firewalls.© Diagram: DevOps Academy
ResultMeansLook at
succeededthe port is open and something answeredthe app itself: logs, curl -v
refused, instantlyyou reached the host; nothing listens theress -tlnp and the service on that host
timed outno answer at all: packets are dropped on the wayfirewalls, security groups, routes
No route to hostno path, or a firewall rejectingip route, the firewall's reject rules

This one distinction halves most investigations. A timeout is almost never the application; a refusal almost never the network.

Treat those as clues rather than absolute rules: a firewall can actively reject a connection, and a service can fail while handling it. Compare tests from the client and server, inspect ss, and check each firewall and route. A common mistake is repeatedly restarting the application after a timeout without first proving that the request reached its host.

tcpdump: see the real packets

When two teams each say "it's not us", tcpdump captures packets seen by a network interface. It can show whether a SYN left the client, whether a reply returned, or whether the connection reached the server at all. Capture only the host, port and duration you need because packet files can contain sensitive data.

$ sudo tcpdump -ni any port 5432                  # all traffic on port 5432
$ sudo tcpdump -ni eth0 host 10.0.2.20 and port 5432 -c 20
$ sudo tcpdump -ni any -w capture.pcap port 443   # save to a file to open in Wireshark
sudo tcpdump -ni any port 5432
  • sudorun the next command as root
10:41:02.113 IP 10.0.1.15.51544 > 10.0.2.20.5432: Flags [S], seq 381…
10:41:03.140 IP 10.0.1.15.51544 > 10.0.2.20.5432: Flags [S], seq 381…
10:41:05.188 IP 10.0.1.15.51544 > 10.0.2.20.5432: Flags [S], seq 381…

[S] is a SYN, the start of a TCP connection. Here it leaves three times and nothing comes back: proof that the packets leave this server and are dropped further on. A healthy connection shows [S], then [S.] (SYN-ACK) from the other side, then [.]. A refused one shows [R.] (reset).

Use -n to avoid DNS lookups during capture, choose the right interface, and set a packet count or narrow filter for a short investigation. Store captures with restricted permissions and delete them when no longer needed. A common mistake is interpreting no packets on one interface as proof that no packets exist; verify the route and capture on the correct host and interface.

Firewalls: nftables and ufw

A firewall is a list of rules the kernel checks for every packet: is this one allowed in? The usual safe setup is default drop: block everything, then allow only what the server is for (SSH, the web ports, the database from the app subnet). Anything you forgot to allow shows up as a timeout, which is why this is the last rung of the method.

Firewalls decide which packets may enter, leave or pass through a host. Linux uses the kernel's netfilter framework; nftables is its current rule interface. Ubuntu offers ufw as a simpler frontend, while many RHEL-family systems use firewalld. Cloud security groups filter traffic before it reaches the host, so the required port must be allowed at both layers.

$ sudo ufw status verbose                 # Ubuntu: rules and default policy
$ sudo ufw allow OpenSSH                  # ALWAYS first, on a remote server
$ sudo ufw allow 443/tcp
$ sudo ufw allow from 10.0.1.0/24 to any port 5432 proto tcp   # database: app subnet only
$ sudo ufw enable
$ sudo nft list ruleset                   # what the kernel actually enforces
sudo ufw status verbose
  • sudorun the next command as root

A minimal nftables policy, /etc/nftables.conf, that drops everything not allowed:

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept      # replies to our own connections
    iif lo accept                            # this machine talking to itself
    tcp dport 22 accept                      # SSH
    tcp dport { 80, 443 } accept             # web
    icmp type echo-request accept            # allow ping
  }
}
$ sudo nft -c -f /etc/nftables.conf       # check the file without applying it
$ sudo systemctl enable --now nftables    # load it now and at boot
sudo nft -c -f /etc/nftables.conf
  • sudorun the next command as root
sudo systemctl enable --now nftables
  • systemctlcontrol systemd services
Don't lock yourself out

Add the SSH rule before turning on a default-drop policy, keep a second session open, and know how to reach the console (the Booting lesson). Use either ufw or a hand-written nftables file on a server, not both: they'll fight over the same rules.

Keep rules narrow: a protocol, a port and a source. Two mistakes cause most firewall incidents:

  • Too wide. Opening the database port to everyone because one app needs it. Allow only the app's subnet or host, as in the ufw example above.
  • Only one layer opened. In the cloud, traffic passes the security group and then the host firewall. Opening the port in the security group but not in ufw (or the other way round) gives you a timeout, and each team swears their layer is open. Check both.
In production

Write down what you saw at each rung as you go: the exact error, the time, which host. "Port 5432 times out from 10.0.1.15 at 10:41, SYNs leave with no reply (tcpdump attached)" gets fixed in minutes. "The database is down" starts an argument.

⌘
Mini mission

Prove where a connection fails

Write port-check.sh: given a host and a port, test DNS, then the TCP port, and say in plain words what failed and where to look next. Test it on a port that's open, one that refuses, and a name that doesn't exist.

Input: Any Linux machineOutput: A one-line verdict for each check
Reveal one safe solution
$ cat > ~/port-check.sh <<'EOF'
#!/usr/bin/env bash
host=${1:?usage: port-check.sh <host> <port>}; port=${2:?usage: port-check.sh <host> <port>}
ip=$(getent ahostsv4 "$host" | awk 'NR==1 {print $1}')
[[ -n $ip ]] || { echo "FAIL dns: $host does not resolve. Check /etc/hosts and resolvectl."; exit 1; }
out=$(nc -zv -w 3 "$ip" "$port" 2>&1)
case "$out" in
  *succeeded*) echo "OK   $host ($ip) port $port is open. If the app still fails, read its logs." ;;
  *refused*)   echo "FAIL refused: $ip answered, nothing listens on $port. Run ss -tlnp there."; exit 1 ;;
  *)           echo "FAIL timeout: no answer from $ip:$port. Check firewalls and security groups."; exit 1 ;;
esac
EOF
$ python3 -m http.server 8099 > /dev/null 2>&1 & sleep 1   # a test server on 8099
$ bash ~/port-check.sh localhost 8099
OK   localhost (127.0.0.1) port 8099 is open. If the app still fails, read its logs.
$ bash ~/port-check.sh localhost 8098
FAIL refused: 127.0.0.1 answered, nothing listens on 8098. Run ss -tlnp there.
$ bash ~/port-check.sh no-such-host.invalid 80
FAIL dns: no-such-host.invalid does not resolve. Check /etc/hosts and resolvectl.
$ kill %1
cat > ~/port-check.sh <<'EOF'
  • catprint a file
  • >write output to a file, replacing it
python3 -m http.server 8099 > /dev/null 2>&1 & sleep 1
  • 2>&1send errors to the same place as output
kill %1
  • killsend a signal to a process

The exit code is 1 on every failure, so this works in a deploy pipeline or a cron health check. To see a timeout, point it at an address that drops packets, such as 10.255.255.1 80 on most networks.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

curl to port 5432 says "Connection refused" straight away. What does that tell you?

Go deeper

Official reading for this lesson

  • Docsss(8) manualUbuntu manpages ↗
  • Docstcpdump(8) manualUbuntu manpages ↗
  • Guidenftables wiki: quick referencenftables ↗
  • GuideUbuntu Server: firewall (ufw)Ubuntu ↗
  • GuideLPIC-1 objective 109.3: basic network troubleshootingLinux Professional Institute ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousShell scripting: automate it once, run it foreverNext lesson →Containers are Linux: namespaces, cgroups and Docker