Sub topic 12
Verifying from the server and from outside
This chapter assumes you can use sudo on a Linux server, know what TCP and UDP ports are, can manage services with systemctl, and have already opened or closed a port with firewall-cmd. Changing a rule is only half the job. You also have to confirm that the rule does what you intended, from the server and from another machine.
Why one check is not enough
A port can look open in one place and be unreachable in another. Three separate things must all be true before a client can connect:
- A process on the server is listening on the port, on an address the client can reach.
- The firewall accepts the traffic in the zone that applies to the client.
- Nothing between the client and the server blocks it. This includes cloud security groups, network ACLs, and routers.
firewall-cmd only tells you about the second point. It reports what firewalld is configured to do. It does not tell you whether anything is listening, and it cannot see firewalls outside the host. A complete verification therefore uses three kinds of evidence:
- Firewall configuration, read with
firewall-cmd. - Listening sockets, read on the server with
ss. - A real connection attempt, made from a different host.
Step 1: Check the firewall configuration
Start with what firewalld believes. The query commands return an exit status, which makes them useful in scripts, and they also print yes or no.
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --query-service=http
echo $?An exit status of 0 means the rule is present and 1 means it is not. The same commands accept --zone=NAME to check a zone other than the default.
Remember the two configuration sets. Without --permanent, a query reads the runtime configuration. With --permanent, it reads what is saved on disk and will load at the next reload or reboot. Both should agree after a change.
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --permanent --query-port=8080/tcpIf the first answers yes and the second answers no, the port will vanish at the next reload. If the answers are reversed, you saved the rule but did not reload.
You can also compare the whole list of ports in each set:
sudo firewall-cmd --list-ports
sudo firewall-cmd --permanent --list-portsStep 2: Check that something is listening
A firewall rule that allows port 8080 does nothing if no process has opened port 8080. Use ss on the server to list listening TCP sockets.
sudo ss -tlnp | grep ':8080'
nc -zv server.example.com 8080The first command runs on the server. The second runs from another host. The ss flags mean:
| Flag | Meaning |
|---|---|
| `-t` | TCP sockets only |
| `-l` | Listening sockets only |
| `-n` | Show numeric ports and addresses instead of resolving names |
| `-p` | Show the owning process (needs root to see other users' processes) |
A typical output line looks like this:
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* users:(("python3",pid=2314,fd=3))The local address column matters as much as the port:
| Local address | What it means |
|---|---|
| `0.0.0.0:8080` | Listening on all IPv4 addresses. Reachable remotely if the firewall allows it. |
| `[::]:8080` | Listening on all IPv6 addresses. On most systems this also accepts IPv4. |
| `127.0.0.1:8080` | Loopback only. No remote client can connect, whatever the firewall says. |
| `192.0.2.10:8080` | Bound to one address. Only traffic sent to that address is accepted. |
A service bound to 127.0.0.1 is a very common reason for a port that "is open but unreachable". The fix is in the application's configuration, not in firewalld. For UDP services use -u in place of -t, and note that UDP sockets have no LISTEN state, so you will see UNCONN instead.
Step 3: Test from outside
Testing from the server itself proves very little. Traffic to a local address usually travels over the loopback interface, which firewalld accepts in the trusted handling of loopback and which never reaches your zone rules in the same way. A connection from localhost can succeed while remote clients are blocked.
Run the test from a different host on the same path your real clients use. nc (netcat) with -z scans without sending data, and -v prints the result.
nc -zv server.example.com 8080The result tells you which layer failed:
| Result | Likely meaning |
|---|---|
| `Connection to ... succeeded!` | Firewall allows it and a process is listening. |
| `Connection refused` | The packet reached the server and was actively rejected. Either nothing is listening, or a firewalld `reject` rule or the zone's default reject sent a reply. |
| Hangs, then `timed out` | Packets are being dropped silently. Suspect a `drop` rule or zone, or a cloud security group or router. |
Distinguishing "refused" from "timed out" is the most useful single observation in troubleshooting. A firewalld zone that simply does not list the port usually answers with an ICMP reject, so you see refused. Traffic assigned to the drop zone or matched by a drop rule produces a timeout.
For UDP, nc -zvu can only tell you that no ICMP "port unreachable" came back. UDP has no handshake, so a quiet port could be open or filtered. For UDP services, test with the real client or a protocol-aware tool.
You can test a web service end to end with curl, which also confirms the application answers correctly:
curl -sS -o /dev/null -w "%{http_code}\n" --connect-timeout 5 http://server.example.com:8080/The --connect-timeout option keeps a blocked port from hanging your terminal for minutes.
Step 4: Verify a block
Blocking needs the opposite proof. You must show that the blocked client fails and that everyone else still succeeds. After adding a source to the drop zone or a rich rule that rejects one address:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-rich-rulesThen test from the blocked address and from an unblocked one:
nc -zv -w 5 server.example.com 22From the blocked client you expect Connection refused for a reject rule and a timeout (here after five seconds, set by -w 5) for drop. From any other client you expect success. If both fail, your rule is too broad or you have locked out the wrong party.
Step 5: Watch the firewall decide
When results are unclear, ask firewalld to log what it denies. This records rejected or dropped packets in the kernel log, which shows whether your test packet was refused by the firewall at all.
sudo firewall-cmd --set-log-denied=all
sudo journalctl -k --since "10 minutes ago" | grep -i reject
sudo firewall-cmd --set-log-denied=offRun the test from the remote host between the first and second commands. A log line contains the source address (SRC=), the destination port (DPT=), and the interface (IN=). If your test produces a line, the firewall is denying it, and the interface tells you which zone to inspect. If it produces no line and the connection still fails, the cause is somewhere else: the application, the bind address, or a device outside the host.
Turn logging off when you finish. On an internet-facing server, all can fill the log with scanner noise.
A repeatable verification routine
After any change, run through the same short sequence. It takes about a minute and catches most mistakes.
- On the server, confirm the rule is in both configurations:
--query-portor--query-service, with and without--permanent. - On the server, confirm the process is listening on the intended address with
ss -tlnp. - From a remote host, connect with
nc -zvor the real client. - For a block, repeat the remote test from a client that should still work.
- Read the result as
succeeded,refused, ortimed out, and map it to a layer using the table above. - Save or discard: if the runtime change works, make it permanent with
--runtime-to-permanent. If not, reload to return to the saved state.
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --permanent --query-port=8080/tcp
sudo ss -tlnp | grep ':8080'Finally, after a reload or reboot window, repeat the remote test once more. Only a check made after the rules have been reloaded proves the permanent configuration is correct.
Common errors
- Testing only from localhost. It succeeds because loopback traffic bypasses the zone rules you meant to test. Always test remotely.
- Querying the wrong zone.
--query-portwithout--zonechecks the default zone, which may not be the one that handles the client. - Trusting
firewall-cmdalone. It shows configuration, not whether a service listens or whether a cloud firewall allows the traffic. - Forgetting the reload or the permanent flag. A runtime-only rule is lost at reload, and a permanent-only rule is not active until reload.
- Reading a timeout as an open port. A silent drop looks like a hang, and some tools report it ambiguously. Use a connect timeout and read the exact message.
- Leaving log-denied on. It is a diagnostic aid, not a permanent setting.
Exercise
You opened port 8080/tcp permanently and reloaded. firewall-cmd --query-port=8080/tcp prints yes, and ss -tlnp shows 127.0.0.1:8080 listening. From another host, nc -zv server.example.com 8080 reports Connection refused. What is wrong, and what do you change?
Show answer
The firewall is not the problem. The application is bound to the loopback address 127.0.0.1, so it only accepts connections made on the server itself, and the kernel answers remote connections with a refusal because no socket is listening on the external address. Change the application's configuration so it listens on 0.0.0.0 or the server's external address, restart it, and confirm with ss -tlnp that the local address is no longer 127.0.0.1. Then repeat the nc test from the remote host.
Exercise
After you add a rich rule that drops traffic from 203.0.113.10 to port 22, a test from that address ends with timed out, and a test from another address succeeds. Would you expect the blocked address to see Connection refused instead if you had used reject? How would you confirm which action the rule uses?
Show answer
Yes. A reject rule sends an ICMP error back, so the client sees Connection refused immediately, while drop discards the packet silently, which produces a timeout. Run sudo firewall-cmd --list-rich-rules and read the action at the end of the rule (drop or reject). The successful test from the other address confirms the rule is not blocking everyone.