Sub topic 13
Enable a host firewall without locking yourself out
A host firewall decides which network connections the server accepts. It is one of the last baseline steps, and it is also the easiest one to get wrong over a remote session. This chapter shows how to turn it on with SSH already allowed, how to confirm that, and how to recover when something goes wrong.
Prerequisites
This chapter assumes you have:
- A working admin user with sudo, and SSH key login that you have already tested.
- A server you can reach by SSH, and ideally a console (a cloud provider console, a hypervisor window, or a physical keyboard) as a fallback.
- Basic knowledge of TCP ports, such as SSH on port 22.
What a host firewall does
A host firewall is a set of rules on the server itself that allows or blocks network traffic. Each rule matches a packet by direction, protocol, port or source address, and then accepts or drops it.
Most baseline setups use a default-deny policy for incoming traffic. Nothing can connect unless a rule allows it. Outgoing traffic is usually allowed, so the server can still fetch updates and resolve DNS.
On Linux the kernel does the filtering through netfilter. You rarely write netfilter rules directly. Two front ends are common:
| Front end | Typical distributions | Rule style |
|---|---|---|
| ufw (Uncomplicated Firewall) | Ubuntu, Debian | Simple allow and deny commands |
| firewalld | RHEL family (Rocky, Alma, Fedora) | Zones and named services |
Pick the one your distribution ships. Running two front ends at once leads to rules that overwrite each other.
Why people lock themselves out
Enabling a default-deny firewall before allowing SSH blocks new connections to port 22. Your current session usually survives, because most rule sets accept packets that belong to an established connection. Once that session ends, you cannot log back in.
Other common causes:
- SSH listens on a non-default port, but you allowed only port 22.
- You allowed the port on IPv4 but not IPv6.
- A cloud security group or network firewall in front of the server also blocks the port. The host firewall is not the only layer.
- You restricted SSH to a source address, and your own address changed.
The rule is simple: allow SSH first, enable the firewall second, and test from a second terminal before you close the first one.
Before you start: check what is listening
Find the port SSH actually uses. Do not assume it is 22.
sudo ss -tlnp | grep sshdIf the output shows a port other than 22, use that port in every rule below. If you plan to run a web server later, allow its ports at that time, not now. A baseline should expose only what the server needs today.
Ubuntu and Debian with ufw
The ufw tool may not be installed on Debian. On Ubuntu it is present but disabled by default.
sudo apt install ufwSet the default policies, allow SSH, then enable the firewall:
# Ubuntu with ufw: allow SSH before enabling the firewall
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verboseThe OpenSSH name refers to an application profile that ufw reads from /etc/ufw/applications.d/. It maps to port 22 over TCP. If your SSH port differs, allow the number directly, for example sudo ufw allow 2222/tcp.
When you run ufw enable, ufw warns that the command may disrupt existing SSH connections. If the SSH rule is already in place, that warning is expected and safe to accept.
The status verbose output should list the default policies and an ALLOW rule for SSH, for both IPv4 and IPv6 (the IPv6 line is marked (v6)).
Limit repeated login attempts
ufw can rate-limit a port. The limit rule denies a source address that opens six or more connections within 30 seconds. It is a mild protection against password guessing and does not replace key-only SSH.
sudo ufw limit OpenSSHThis replaces the plain allow rule for the same service. Check ufw status afterward to confirm only one rule for SSH remains.
RHEL family with firewalld
On Rocky, Alma and Fedora, firewalld is usually installed and running. It groups interfaces into zones, and each zone has its own set of allowed services. The default zone is often public, and SSH is commonly already allowed. Check before you change anything.
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-allThen make the SSH rule explicit and permanent:
# RHEL family with firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-allThe --permanent flag writes the rule to disk but does not change the running configuration. The --reload command applies the saved rules. If you leave out --permanent, a rule works now and vanishes at the next reload or reboot. Leaving out --reload does the opposite: the rule is saved but not active.
For a non-default SSH port, add the port instead of the service:
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reloadSELinux must also label that port for sshd, which is a separate step covered where you change the SSH port.
Test from a second session
An open session proves nothing about new connections. After enabling the firewall, keep the first terminal open and log in from another one.
ssh [email protected]If the second login succeeds, the rules are correct. If it hangs and times out, the firewall or an upstream filter is dropping the connection. Go back to the first terminal and fix it there.
Two quick diagnoses from the open session:
- Run
sudo ufw status verboseorsudo firewall-cmd --list-alland confirm the SSH rule exists. - Run
sudo ss -tlnp | grep sshdand confirm the daemon listens on the port you allowed.
If both look right, the block is outside the host. Check the cloud security group, the provider's network firewall, or a router.
Recovery when you are locked out
If you lose access, you have three options, from least to most disruptive:
- Use the provider or hypervisor console. It does not use the network path that the firewall blocks. Log in as your admin user and fix the rule.
- Boot into rescue or single-user mode and disable the firewall service for one boot, then correct the rules.
- Mount the disk from another machine and edit the firewall configuration offline.
For ufw, the disable command is sudo ufw disable. For firewalld, use sudo systemctl stop firewalld, then correct the rules and start it again.
Confirm the firewall survives a reboot
Both tools enable their service at boot, but verify it.
# ufw
sudo systemctl is-enabled ufw
sudo systemctl is-active ufw
# firewalld
sudo systemctl is-enabled firewalld
sudo systemctl is-active firewalldEach command should print enabled and active for the tool you use. A reboot test is the strongest proof, and it belongs in the final verification checklist later in this tutorial. If you do it now, keep console access ready.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| New SSH logins time out after enabling | SSH rule missing or wrong port | From an open session, add the correct allow rule |
| Rule works now, gone after reboot | firewalld rule added without `--permanent` | Re-add with `--permanent`, then reload |
| `ufw: command not found` | Package not installed | `sudo apt install ufw` |
| Works over IPv4, fails over IPv6 | IPv6 disabled in `/etc/default/ufw` (`IPV6=no`) | Set `IPV6=yes`, then `sudo ufw disable` and `sudo ufw enable` |
| Rule exists but connection still fails | Cloud security group or upstream filter | Open the port in that layer too |
| Two firewall tools conflict | ufw and firewalld both active | Keep one and disable the other |
Summary of the safe procedure
- Confirm the SSH port with
ss -tlnp. - Set default-deny for incoming and allow for outgoing traffic.
- Allow SSH by service name or port.
- Enable the firewall.
- Test a new login from a second terminal while the first stays open.
- Confirm the service is enabled at boot.
Exercise
You enable ufw on a remote Ubuntu server with sudo ufw default deny incoming and sudo ufw enable, but you forgot to run sudo ufw allow OpenSSH. Your current SSH session still works. What do you do now, and what would you do differently next time?
Show answer
Do not close the current session. In it, run sudo ufw allow OpenSSH (or allow the actual SSH port), check sudo ufw status verbose, and open a second terminal to confirm a new login works. Next time, allow SSH before running ufw enable, and test from a second terminal before closing the first.
Exercise
On a Rocky Linux server you run sudo firewall-cmd --add-port=2222/tcp without --permanent. The rule works. After a reboot, new SSH connections to port 2222 fail. Why, and how do you fix it?
Show answer
Without --permanent, the rule exists only in the running configuration, so the reboot discarded it. Run sudo firewall-cmd --permanent --add-port=2222/tcp followed by sudo firewall-cmd --reload, then verify with sudo firewall-cmd --list-all.