Sub topic 5
Changing Rules Without Cutting Off SSH
Assume you know basic Linux commands, sudo, TCP and UDP ports, systemctl, and IPv4 CIDR notation. You should also have a second way to reach the server, such as a console provided by your cloud platform. A firewall mistake can prevent a new SSH connection, so do not test a risky change with your only access path.
Use the Correct Zone
A firewalld zone is a named rule set. Firewalld assigns incoming traffic to a zone by matching the traffic's source address or network interface. The default zone is used only when no more specific assignment matches.
Start by recording the current state:
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all--list-all describes the default zone. If the server has several active zones, inspect the zone attached to the interface carrying your SSH connection:
sudo firewall-cmd --get-zone-of-interface=eth0
sudo firewall-cmd --zone=public --list-allReplace eth0 and public with the interface and zone reported by your system. Do not assume that the default zone is the zone handling your connection.
Keep SSH Allowed Before Testing
Before changing any other rule, confirm that SSH is allowed in the relevant zone:
sudo firewall-cmd --zone=public --query-service=sshA result of yes means the zone permits the predefined ssh service. If it returns no, add SSH to the runtime configuration before continuing:
sudo firewall-cmd --zone=public --add-service=sshThis change is temporary. It protects the current test without immediately changing the permanent configuration. If SSH was already permitted permanently, a later reload will restore it. If you need to keep this correction, save it after verifying access.
Test New Rules in Runtime First
The runtime configuration is the rule set currently used by firewalld. A runtime change takes effect immediately but normally disappears after a reload or reboot. This makes it safer for testing.
For example, suppose a web service is listening on port 8080. Open that port temporarily with a five-minute timeout:
sudo firewall-cmd --zone=public --add-port=8080/tcp --timeout=5m
sudo firewall-cmd --zone=public --query-port=8080/tcpThe timeout automatically removes the rule if you do not confirm the change. From another host, test the port:
nc -zv server.example.com 8080Keep your existing SSH session open while testing. Open a second SSH session to confirm that SSH still works. If the application and SSH tests succeed, save the tested runtime rules:
sudo firewall-cmd --runtime-to-permanentThis copies the current runtime configuration into the permanent configuration.
Understand Reloads and Recovery
A reload replaces the active runtime rules with the saved permanent rules. It can remove a temporary rule or restore a rule that you accidentally deleted from runtime:
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-allIf a new SSH connection fails after a change, use the existing session or an out-of-band console. Restore SSH in the correct zone:
sudo firewall-cmd --zone=public --add-service=ssh
sudo firewall-cmd --permanent --zone=public --add-service=ssh
sudo firewall-cmd --reloadThe first command restores the current runtime state. The second stores the fix for future reloads.
Exercise
Exercise
Why should a new port usually be added to the runtime configuration with a timeout before it is made permanent?
Show answer
A timeout limits the risk of an incorrect rule. It gives you time to test the port and SSH access, and removes the temporary rule automatically if you do not confirm it.