Sub topic 5
Changing rules without cutting off SSH
This chapter assumes you can run firewall-cmd with sudo, that you know the difference between runtime and permanent configuration, and that you can read firewall-cmd --list-all output. You will learn which changes can lock you out of a remote server and how to make changes so you can undo a mistake.
Why lockouts happen
A firewall change on a remote server is risky because the connection you use to make the change is also subject to the firewall. If you remove the rule that allows your SSH session and the firewall stops accepting new SSH connections, you cannot log in again. If the server is a cloud instance or sits in a remote data centre, you may have no other way in.
Two details of firewalld's behavior affect how you plan for this.
- Established connections usually survive. firewalld's rules accept packets that belong to a connection that is already tracked. If you remove the
sshservice, your current session often keeps working. The failure appears later, when you open a new session or your current one drops and reconnects. A change that looks harmless can therefore hide a lockout until it is too late to fix it. - Runtime changes vanish on reload or reboot. Runtime configuration is held in memory. If the permanent configuration still allows SSH,
firewall-cmd --reloador a reboot restores access. Your plan can use this.
Common ways to lock yourself out
Most lockouts come from a short list of actions.
| Action | What goes wrong | Safer approach |
|---|---|---|
| `--remove-service=ssh` in the zone that serves your connection | New SSH connections are refused or time out | Check which zone handles your connection first, and keep `ssh` allowed there |
| Moving your interface to another zone, or changing the default zone | The new zone may not allow SSH | Run `--list-all --zone=<new zone>` first and confirm `ssh` is listed |
| Adding a source network to the `drop` zone that includes your own address | All traffic from your address is silently discarded | Check your own address with `echo $SSH_CLIENT` before choosing the network |
| A rich rule that rejects or drops port 22 from a range that includes you | Same as above | Test the rule against a different address, or use `--timeout` |
| Running sshd on a non-standard port without allowing that port | The `ssh` service only covers port 22 | Open the new port before changing sshd, and keep port 22 open until you have tested |
| `firewall-cmd --panic-on` | Drops all packets, including established connections, so your session dies at once | Do not use panic mode on a remote server unless you have console access |
| `--permanent` change followed by `--reload` with a mistake | The mistake takes effect immediately and survives a reboot | Make the change in runtime first, verify it, then save it |
Rules that match on source address take priority over rules that match on interface in the way zones are chosen. If you add your own address to drop while connected from it, the next packet you send is discarded. You then have no way to run the command that would remove the rule.
Know your starting point
Before any change, record where your SSH session comes in. Check the zone, the interface, and whether SSH is currently allowed.
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all
sudo firewall-cmd --query-service=ssh
echo "$SSH_CLIENT"
sudo ss -tlnp | grep sshd--query-service=ssh prints yes or no for the default zone. $SSH_CLIENT shows your client address, the client port, and the server port, so you can check that your own address will not fall inside a network you plan to block. The ss line confirms which port sshd listens on. If it is not 22, the predefined ssh service does not cover it, and you must treat that port as essential.
Strategy 1: make changes in runtime first
The runtime configuration is your safety net. When you add a rule without --permanent, it applies at once but is lost on reload or reboot. A mistake that locks you out can then be cleared by a reload, if you can reach the server, or by a reboot from the provider's console.
Adding --timeout goes further. firewalld removes the rule by itself when the time expires. The timeout works only on runtime changes, and firewalld rejects it when combined with --permanent.
sudo firewall-cmd --add-service=http --timeout=5m
sudo firewall-cmd --list-services
sudo firewall-cmd --runtime-to-permanentThe first command opens HTTP for five minutes. You test it. The third command saves the whole runtime configuration to disk, but run it only after the test shows that everything works.
--runtime-to-permanent copies all of the runtime configuration, not only the rule you just added. If an earlier runtime experiment is still in place, it becomes permanent too. Check --list-all before you run it.
A timeout is a good fit for rules that add access. It does not protect you from a rule that removes access, because the timer would undo only a rule you added. If you add a blocking rule with --timeout, the block expires. That is the behavior you want for a risky block, and it is why a timed block is safer than a permanent one.
Strategy 2: a dead-man's switch
For a change that could cut off SSH, schedule a rollback before you make it. Because a reload replaces the runtime configuration with the permanent one, a scheduled --reload undoes any runtime-only change.
sudo systemd-run --on-active=5m --unit=fw-rollback firewall-cmd --reload
sudo firewall-cmd --zone=drop --add-source=203.0.113.0/24Now open a new SSH session and check that you can still log in. If it works, cancel the rollback and decide whether to keep the rule:
sudo systemctl stop fw-rollback.timer
sudo firewall-cmd --runtime-to-permanentIf you are locked out, wait five minutes. The timer runs firewall-cmd --reload, the runtime rule disappears, and the permanent configuration, which still allows SSH, takes over again.
This only works under two conditions. The permanent configuration must still allow your SSH access, so never use this method after you have changed the permanent configuration. The server must also stay up for the timer to fire.
Strategy 3: always test from a second session
Keep your original SSH session open while you make the change. After the change, open a second session from another terminal. Only close the first session after the second one is working.
Your original session is valuable because of the established-connection behavior described earlier. Even if the firewall now refuses new connections, the old session may still let you run sudo firewall-cmd --reload or remove the bad rule. Closing it too early throws that away.
A safe workflow
The steps below combine these strategies.
Validating permanent changes
Sometimes you must edit the permanent configuration directly, for example when you add a service definition or change the default zone. These changes take effect only after a reload, so you cannot test them in runtime first. Reduce the risk in two ways.
- Run
sudo firewall-cmd --check-configbefore you reload. It reports syntax errors in the permanent configuration files. A file with an error can leave firewalld unable to load a zone. - Schedule the rollback as before. A reload loads the new permanent configuration, so a rollback reload would not help. In this case, use a different recovery plan: take a copy of
/etc/firewalldfirst and keep console access ready.
sudo cp -a /etc/firewalld /root/firewalld-backup
sudo firewall-cmd --check-config
sudo firewall-cmd --reloadIf the new configuration locks you out, restore the copy from the console with cp -a /root/firewalld-backup/. /etc/firewalld/ and then reload.
Summary of habits
- Check the zone, the interface, and
sshbefore you change anything. - Prefer runtime changes and
--timeoutfor experiments, and save to permanent only after a test. - Schedule a reload as a rollback when you apply a risky runtime change.
- Test from a second SSH session before you close the first.
- Never use
--panic-onover a remote connection. - If sshd uses a non-standard port, open that port explicitly.
Exercise
You are connected over SSH from 198.51.100.7. You want to block the network 198.51.100.0/24 from reaching the server, but you are not sure the rule is right. Describe a sequence of steps that lets you try the rule without risking permanent lockout, and say why the order matters.
Show answer
First check your address with echo $SSH_CLIENT and note that 198.51.100.7 falls inside 198.51.100.0/24, so a block would cut you off. Keep your first session open. Schedule a rollback with sudo systemd-run --on-active=5m --unit=fw-rollback firewall-cmd --reload, or add the rule with --timeout so it expires. Apply the rule to runtime only, without --permanent. Test from another host, and not from the blocked network. If the rule does what you want, cancel the rollback and run --runtime-to-permanent. The order matters because the rollback must exist before the risky change, and because a permanent rule would survive a reload and a reboot and could not be undone by waiting.
Exercise
You run sudo firewall-cmd --permanent --remove-service=ssh and then sudo firewall-cmd --reload. Your current session keeps working, but a colleague cannot log in. Why does your session survive, and how do you restore access?
Show answer
firewalld accepts packets that belong to a connection it already tracks, so the established session continues even though new SSH connections are no longer allowed. To restore access, use the still-open session to run sudo firewall-cmd --permanent --add-service=ssh and then sudo firewall-cmd --reload, and confirm with sudo firewall-cmd --query-service=ssh that it prints yes.