Rocky Linux 10 Tutorial / Restrict SSH to management IP addresses
To allow only management IPs to access SSH in Rocky Linux 10, you should first create an allow rule specifying the source address and SSH service together , and then remove SSH allowance that was open to all addresses. Do not judge success just because an existing SSH session remains; test new connections from both allowed and disallowed addresses before deciding on permanent settings.
Check from the addresses to be restricted and the actual connection path.
SSH (Secure Shell) is a communication protocol for remote login to a server. Firewalls restrict the source addresses allowed for connection, while SSH server key authentication and user permissions are checked separately. Allowing an IP does not automatically allow login. Proceed with this on a practice VM where SSH key authentication and server configuration verification have already been completed.
The basic example in this article applies to the case where the service ssh on basic port 22/tcp in the public zone is generally allowed. If the zone, port, or other allowed rules are different, do not apply as-is. Before changing the remote policy, check whether you can actually log in to the console and run sudo.
printf '%s\n' "$SSH_CONNECTION"
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --permanent --zone=public --list-all
sudo firewall-cmd --info-service=ssh
sudo ss -lntp '( sport = :22 )'
The value of SSH_CONNECTION is in the order of client IP, client port, server IP, and server port. It is read from a normal user shell connected via SSH. It may be empty in the console. It is used as a starting point to check whether the source seen by the server is the same as the address shown on the management PC.
Especially if it goes through NAT (Network Address Translation) or VPN (Virtual Private Network), the server may see the address of the intermediate device. In this VirtualBox NAT exercise, the connection from Windows to 127.0.0.1:22222 was observed at the guest as originating from 10.0.2.2. If you allow this address, you cannot distinguish other clients that are converted to the same address. If multiple users need to be managed separately, VPN address design or authentication policies should also be reviewed.
The /32 after the address refers to a single IPv4 address. It differs in scope from /24, which refers to the entire management range. Since addresses automatically assigned via DHCP can change or VPN connections can vary, administrators originally allowed may be blocked, so prepare fixed management paths and recovery methods.
Record the existing allowed and recovery standards.
As explained in the firewalld runtime and permanent rule lesson, these two configurations are separate. First, back up both settings and relevant files. If the following path already exists, stop and choose a separate path. Do not overwrite the backup.
sudo bash <<'BASH'
set -eu
backup=/root/ssh-access-before
test ! -e "$backup"
mkdir -m 700 "$backup"
cp -a /etc/firewalld "$backup/"
firewall-cmd --list-all-zones > "$backup/runtime-zones.txt"
firewall-cmd --permanent --list-all-zones > "$backup/permanent-zones.txt"
BASH
Read all of the services, ports, and rich rules zones. Even if you remove the general ssh allowance, if 22/tcp remains open separately, or if other rules, zones, or policies allow it, your restriction objectives may not be met. Do not apply this example to configurations that allow broadly like trusted zones or ACCEPT targets. Do not assume defaults are the same; verify which zone your access device and origin belong to.
The following check is an example to confirm the allowance of a simple existing SSH service in both runtime and permanent settings, yes. If no or an error occurs, stop and first understand the currently allowed path. Do not duplicate the same operation on servers that already have existing restrictive policies.
sudo firewall-cmd --zone=public --query-service=ssh
sudo firewall-cmd --permanent --zone=public --query-service=ssh
Allow the management address first and schedule automatic recovery.
Below, 203.0.113.10/32 is an example for documentation purposes. Replace it with the actual management source confirmed on your own server. The variables in this example are used continuously in the same shell. Variables are not passed to a separate window or console.
ZONE=public
ADMIN_CIDR=203.0.113.10/32
RULE="rule family=\"ipv4\" source address=\"$ADMIN_CIDR\" service name=\"ssh\" accept"
printf '%s\n' "$ZONE" "$RULE"
sudo firewall-cmd --zone="$ZONE" --add-rich-rule="$RULE"
sudo firewall-cmd --zone="$ZONE" --query-rich-rule="$RULE"
Check not only the source but also whether service name="ssh" has been specified. Creating a comprehensive accept for the source may allow more communication than intended. The address for this rule is IPv4, so family="ipv4" has been specified. The official firewalld rich language documentation explains rule syntax and ordering.
Before restricting the rule, secure time using a one-time systemd timer to restore general SSH allowance. The following example uses the name lf-ssh-rollback. If the same name already exists, choose another unique name and use that name in subsequent queries and stop commands.
sudo systemctl list-units --all 'lf-ssh-rollback.*'
sudo systemd-run --unit=lf-ssh-rollback --on-active=120s \
--timer-property=AccuracySec=1s \
/usr/bin/firewall-cmd --zone=public --add-service=ssh
sudo systemctl status lf-ssh-rollback.timer --no-pager
Proceed only after confirming that the timer is in standby. --on-active is the option to schedule execution relative to time, and it is defined in the original official documentation of systemd-run in systemd 257 . This recovery is limited to the practice where the general SSH service was originally allowed. It is not a mechanism to revert all other services, ports, or zone changes, and it does not guarantee recovery from reboot or firewall daemon failure.
In this VM's firewalld 2.4.3-4.el10_2, a problem was observed in the previous lecture with the combination of timed rules and reloads; therefore, the source practice used a separate systemd timer instead of a firewalld rule with --timeout. Recovery that turns off the firewall or changes host security settings was not used.
Remove general SSH allowance and compare new connections.
Now, remove only the general allowance in the runtime settings. Keep the management address rules and do not change the permanent settings yet. After the following command succeeds, reconnect to SSH from a new terminal of the allowed management PC while keeping the existing window.
sudo firewall-cmd --zone="$ZONE" --remove-service=ssh
sudo firewall-cmd --zone="$ZONE" --query-service=ssh
sudo firewall-cmd --zone="$ZONE" --list-rich-rules
The expected result of a general service query is no, and the termination status may be 1. Administrative address rules should still be output. Existing connections may be maintained due to connection tracking, so be sure to verify new TCP connections and new key-authenticated logins . Check not only connection success but also necessary administrative privileges such as sudo -v in a new window.
Next, test a new connection from a client whose actual source differs and is not on the allow list. Two PCs behind the same NAT appearing with the same address cannot be used as a comparison target. In the remote verification window, use SSH commands and host key verification appropriate for the environment, and do not skip the server host key verification even if it fails.
| Observation phase | Allowed test addresses | Non-allowed test addresses | Existing administrative paths |
|---|---|---|---|
| General SSH allowed status | New SSH banner reception | New SSH banner reception | New key-authenticated login success |
| Runtime settings retaining only source rules | New SSH banner reception | New connection rejected | New key authentication login successful with separate allow rule |
| Restore scheduled recovery or normal SSH allowance from the console | New SSH banner reception | New SSH banner reception | New key-authenticated login success |
The source lab compared Rocky Linux 10.2, firewalld 2.4.3-4.el10_2, OpenSSH server 9.9p1-29, systemd 257. Inside the source VM, two network namespaces with separate network stacks provided test paths. The policy allowed 10.203.16.2/32 and blocked 10.203.17.2/32. The existing management path 10.0.2.2/32 had a separate allow rule.
The inspection of the two test addresses is only up to the banner sent by the server SSH-2.0-OpenSSH_9.9 and is not a user authentication test. The test of logging in to the VM from a Windows OpenSSH client with new key authentication was separately successful. From non-allowed addresses, No route to host appeared, but the actual route existed and connections were possible before the policy change. Do not conclude it is a routing problem only based on this error message and investigate the firewall's rejection response as well.
Check timer recovery and console recovery separately
In the source recovery test, the timer was deliberately allowed to expire. The 40-second timer tested restored normal SSH access, and new connections to disallowed test addresses also received the SSH banner again. The execution results are confirmed with the unit's journal and policy, as well as actual new connections. Since a transient unit can disappear from memory after execution, do not judge failure based solely on the absence of the unit later.
sudo journalctl -u lf-ssh-rollback.service --no-pager -n 20
sudo firewall-cmd --zone=public --query-service=ssh
Manual recovery is carried out from the VM console, independent of the timer. If the actual device uses a different zone, adjust the zone below according to the pre-change record. This command also restores the current configuration where normal SSH service was originally allowed.
sudo firewall-cmd --zone=public --add-service=ssh
sudo firewall-cmd --zone=public --query-service=ssh
This time, with a 120-second backup timer in place, normal allowance was removed again and recovery was performed via the console. The console displayed success on 2026-11-12T14:17:08Z, and the timer journal executed on 14:18:42Z recorded that the SSH service was already allowed. New connections were also recovered. By comparing the console screen with the policy and journal, the source lab distinguished automatic recovery from manual recovery.
If the rule syntax is incorrect or the daemon is abnormal, a simple allow command may fail. Do not repeatedly reload or restart without checking existing permanent settings; examine errors and original backups on the console. Do not make firewall deactivation the default recovery procedure.
Decide whether to continue applying it or to revert.
If the test failed or you do not plan to maintain the restrictions, first restore the general SSH allowance and remove only the source rules you added. Check that the RULE value in the same shell is correct by printing it. Do not delete rules that existed before the practice.
sudo firewall-cmd --zone="$ZONE" --add-service=ssh
sudo firewall-cmd --zone="$ZONE" --query-service=ssh
printf '%s\n' "$RULE"
sudo firewall-cmd --zone="$ZONE" --remove-rich-rule="$RULE"
The source lab chose this restoration path. After cleaning up the two address rules added, the virtual interface, and the namespace, the entire runtime and permanent zone output were compared with the original, and the /etc/firewalld file also matched byte for byte. The original network profile, SSH service, and SELinux Enforcing status were maintained.
If you continue to use the verified restrictions, first check all necessary management addresses and IPv6 routes. If IPv6 SSH is open while only IPv4 rules have been created, you cannot say that overall access is restricted by IPv4 restrictions alone. If IPv6 management is needed, design the actual IPv6 address ranges and family="ipv6" rules separately and test both paths.
An example of making it permanent is as follows. First, put the verified allowed rules into the permanent settings and check them, then remove the original general SSH allowance from the permanent settings. If multiple management addresses were allowed, you must save rules for each address. In this practice, the application of the following permanent restrictions was not performed.
sudo firewall-cmd --permanent --zone="$ZONE" --add-rich-rule="$RULE"
sudo firewall-cmd --permanent --zone="$ZONE" --query-rich-rule="$RULE"
sudo firewall-cmd --permanent --zone="$ZONE" --remove-service=ssh
sudo firewall-cmd --permanent --zone="$ZONE" --list-all
sudo firewall-cmd --check-config
The official description of firewall-cmd --check-config checks the XML syntax and meaning of permanent settings and does not guarantee successful connections. Reloading brings the permanent settings into the running configuration, so other unsaved running changes may be lost. Before applying remotely, recheck the console, backup, recovery time, and overall differences, and after applying, re-examine new connections on both allowed and disallowed sides. Plan to perform the same check after reload and reboot. Do not replace review with --runtime-to-permanent that indiscriminately saves all running changes.
sudo firewall-cmd --reload
sudo firewall-cmd --zone="$ZONE" --list-all
sudo firewall-cmd --permanent --zone="$ZONE" --list-all
When returning to the original general SSH allowance after making permanent changes, first allow ssh on both running and permanent settings in the console, then query, and only remove the address rules added by this operation on both settings. If you only restore the running configuration, it will be restricted again on the next reload or reboot, so be sure to compare with the permanent settings as well.
sudo firewall-cmd --zone="$ZONE" --add-service=ssh
sudo firewall-cmd --permanent --zone="$ZONE" --add-service=ssh
sudo firewall-cmd --zone="$ZONE" --query-service=ssh
sudo firewall-cmd --permanent --zone="$ZONE" --query-service=ssh
sudo firewall-cmd --zone="$ZONE" --remove-rich-rule="$RULE"
sudo firewall-cmd --permanent --zone="$ZONE" --remove-rich-rule="$RULE"
If the restriction has been successfully maintained and the backup timer is still waiting, stop only the timer you created and check its status. If it has already run, first check whether the general SSH allowance has been added again. The timer stop command does not revert firewall changes that have already been executed.
sudo systemctl stop lf-ssh-rollback.timer
sudo systemctl is-active lf-ssh-rollback.timer
sudo firewall-cmd --zone=public --list-all
Completion requires new connections from the intended sources, blocked connections from other sources, successful authentication and required administrative privileges. Verify permanent settings and recovery measures as well; one surviving administrator session does not establish that the policy works.




