How to Block IPs From a Country on AlmaLinux 9 or Rocky Linux 9 with firewalld
Load a country's IPv4 and IPv6 networks into firewalld IP sets on AlmaLinux 9 or Rocky Linux 9, bind them to the drop zone and refresh them weekly.
This guide shows you how to drop all incoming connections from one or more countries on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9. You will load a published list of a country's IP networks into a firewalld IP set, bind that set to the drop zone, and schedule a job that keeps the list current. The example blocks Iceland (country code is); replace it with the codes you need.
CentOS 6 reached end of life in November 2020, and its service iptables save workflow does not exist on current releases. On the RHEL 9 family, firewalld manages the packet filter with nftables underneath and has built-in support for IP sets, so you need neither hand-written iptables rules nor the separate ipset command.
Before you start
- You need a user with
sudorights and firewalld running. Check withsudo firewall-cmd --state; if it is not installed, runsudo dnf install -y firewalldandsudo systemctl enable --now firewalld. - Do not block the country you connect from. Keep your provider's web console at hand in case you lock yourself out of SSH.
- Country lists are derived from registry data and are never perfectly accurate. Visitors on VPNs, mobile roaming or cloud networks may appear under a different country. Treat this as a way to cut background noise, not as access control.
Step 1: Download the country's network list
Several services publish per-country lists of IP networks in CIDR notation, one network per line. This guide uses the aggregated zone files from IPdeny, which are named after the lower-case two-letter ISO country code. Download the IPv4 and IPv6 lists for Iceland:
curl -fsS -o /tmp/is-v4.zone https://www.ipdeny.com/ipblocks/data/aggregated/is-aggregated.zone
curl -fsS -o /tmp/is-v6.zone https://www.ipdeny.com/ipv6/ipaddresses/aggregated/is-aggregated.zone
Look at the result before you load it into the firewall:
head -n 5 /tmp/is-v4.zone
wc -l /tmp/is-v4.zone /tmp/is-v6.zone
Each line should be a network such as 198.51.100.0/24. To block several countries, download each file and append them into one list per address family. Read the provider's usage policy before you automate downloads.
Step 2: Create the IP sets
An IP set holds either IPv4 or IPv6 networks, so create one of each. The type hash:net stores networks rather than single addresses, and maxelem raises the capacity so that large countries fit:
sudo firewall-cmd --permanent --new-ipset=countryblock4 --type=hash:net --family=inet --option=maxelem=131072
sudo firewall-cmd --permanent --new-ipset=countryblock6 --type=hash:net --family=inet6 --option=maxelem=131072
Load the downloaded lists. firewalld reads one entry per line and ignores empty lines and lines that start with # or ;:
sudo firewall-cmd --permanent --ipset=countryblock4 --add-entries-from-file=/tmp/is-v4.zone
sudo firewall-cmd --permanent --ipset=countryblock6 --add-entries-from-file=/tmp/is-v6.zone
Step 3: Send the sets to the drop zone
firewalld ships with a zone named drop that silently discards every incoming packet. When you bind a source to a zone, traffic from that source is handled by that zone instead of the zone of the network interface. Bind both sets and activate the permanent configuration:
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:countryblock4
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:countryblock6
sudo firewall-cmd --reload
Connections that your server opens to an address in the blocked country still work, because replies to established connections are accepted before zone rules apply. Only new incoming connections are dropped.
Step 4: Verify
List the active zones. The drop zone should appear with both sets as sources:
sudo firewall-cmd --get-active-zones
Check that the sets are loaded and count their entries:
sudo firewall-cmd --info-ipset=countryblock4
sudo firewall-cmd --ipset=countryblock4 --get-entries | wc -l
Open a second SSH session to confirm that you can still log in before you close the first one. For a real test, connect from a host inside the blocked country, for example through a VPN exit there; the connection should time out rather than be refused.
Step 5: Keep the list up to date
Address allocations change, so refresh the sets regularly. Create a script:
sudo vi /usr/local/sbin/update-countryblock.sh
Paste the following and set COUNTRIES to a space-separated list of country codes:
#!/bin/bash
set -euo pipefail
COUNTRIES="is"
V4=$(mktemp)
V6=$(mktemp)
OLD=$(mktemp)
trap 'rm -f "$V4" "$V6" "$OLD"' EXIT
for cc in $COUNTRIES; do
curl -fsS "https://www.ipdeny.com/ipblocks/data/aggregated/${cc}-aggregated.zone" >> "$V4"
curl -fsS "https://www.ipdeny.com/ipv6/ipaddresses/aggregated/${cc}-aggregated.zone" >> "$V6"
done
# Stop here if a download produced nothing
[ -s "$V4" ] || exit 1
[ -s "$V6" ] || exit 1
refresh() {
local set="$1" file="$2"
firewall-cmd --permanent --ipset="$set" --get-entries > "$OLD"
if [ -s "$OLD" ]; then
firewall-cmd --permanent --ipset="$set" --remove-entries-from-file="$OLD"
fi
firewall-cmd --permanent --ipset="$set" --add-entries-from-file="$file"
}
refresh countryblock4 "$V4"
refresh countryblock6 "$V6"
firewall-cmd --reload
Because of set -e and curl -f, a failed download aborts the script before the existing sets are touched. Make the script executable and run it once by hand:
sudo chmod 750 /usr/local/sbin/update-countryblock.sh
sudo /usr/local/sbin/update-countryblock.sh
Schedule it weekly with a cron file:
echo '15 4 * * 1 root /usr/local/sbin/update-countryblock.sh' | sudo tee /etc/cron.d/countryblock
Removing the block
Unbind the sources, delete the sets and reload:
sudo firewall-cmd --permanent --zone=drop --remove-source=ipset:countryblock4
sudo firewall-cmd --permanent --zone=drop --remove-source=ipset:countryblock6
sudo firewall-cmd --permanent --delete-ipset=countryblock4
sudo firewall-cmd --permanent --delete-ipset=countryblock6
sudo firewall-cmd --reload
Remove /etc/cron.d/countryblock and the script as well. The firewalld IP set options are described in the firewall-cmd manual page.
Frequently asked questions
Can I allow only one country instead of blocking others?
Yes, and for a service such as SSH it is often the better approach. Load the allowed country into an IP set, remove the service from your public zone, and add a rich rule that accepts the service only when the source is in the set. Everything else then falls through to the zone's default handling. Test from a second session before you close the first.
Does blocking a country slow the server down?
Not noticeably. A set lookup is a single hash operation regardless of how many networks it contains, which is why sets are used instead of one firewall rule per network. Using the aggregated lists also keeps the number of entries down.
Does this stop traffic that arrives through a CDN or reverse proxy?
No. If visitors reach your site through a CDN or a load balancer, the firewall only sees the proxy's address. Country rules then have to be applied at the proxy or CDN, or in the web server using the forwarded client address.
Why not use iptables with the ipset command as on CentOS 6?
You can install both on AlmaLinux 9, but they would sit beside firewalld and compete with it. Keeping the sets inside firewalld means one tool owns the rules, the configuration is stored in /etc/firewalld/ipsets/ and it is restored automatically at boot.