Sub topic 13
Enable a Host Firewall Without Locking Yourself Out
What a Host Firewall Controls
A host firewall filters network traffic on the server itself. It decides whether packets are accepted, rejected, or dropped before an application receives them. This adds protection even when the server is exposed to an untrusted network.
Assume you already have:
- A working network connection.
- A confirmed SSH login.
- Administrative access through
sudo. - Knowledge of which services must be reachable.
A firewall does not replace application security or a cloud provider's network firewall. Both layers must allow traffic for a connection to work.
Plan Before Enabling Rules
List the services that need incoming connections. A typical administration server needs SSH on TCP port 22. A web server might also need HTTP on port 80 and HTTPS on port 443. Do not open a port merely because a program is installed.
The safest order is:
- Identify the SSH service and its port.
- Add the SSH allow rule.
- Set the default policy for new incoming traffic.
- Enable or reload the firewall.
- Test a new connection from another terminal.
- Add only the application ports you actually need.
Keep the current SSH session open while testing. An existing session may remain connected even when new connections would be blocked.
Configure a Firewall
Ubuntu commonly uses ufw, which manages firewall rules with simple commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verboseOpenSSH is an application profile that normally permits TCP port 22. If SSH uses another port, specify it directly, such as sudo ufw allow 2222/tcp. To limit administration to a management network, use a source restriction:
sudo ufw allow from 192.0.2.0/24 to any port 22 proto tcpOn RHEL-family systems, firewalld manages named services and zones:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-allThe --permanent option saves the rule for future reloads. The reload applies saved rules to the running firewall.
Verify and Recover
From a second terminal, start a fresh login:
ssh [email protected]Then confirm that expected services are listening:
ss -tlnpIf a required service is blocked, add a narrow rule rather than allowing all traffic. If SSH becomes unreachable, use the provider console or local terminal to inspect the firewall status and restore the SSH rule.
| Check | What it confirms |
|---|---|
| Firewall status | The firewall is running with the intended policy |
| New SSH login | Administration still works for new connections |
| `ss -tlnp` | Listening services match the planned exposure |
Exercise
Why should you test SSH from a second terminal instead of relying on the session that enabled the firewall?
Show answer
The existing session may remain established even if the new firewall policy blocks all new SSH connections.