How Country-Based IP Blocking Works on a Linux Server

Reduce unwanted traffic, with realistic expectations

If your service targets a local audience but receives substantial traffic from elsewhere, country-based filtering may reduce some noise. It cannot guarantee that everyone in a country is blocked: VPNs, proxies, cloud services, and inaccurate geolocation complicate the picture.

What a country list represents

Providers publish IP prefixes associated with a territory. These are maintained datasets, not permanent boundaries. They need updates, and IPv4 and IPv6 require separate attention.

The original example used a country zone file from IPdeny and appended one iptables DROP rule per prefix. That approach can produce a large rule list and may not perform or behave as expected.

Check the firewall you actually run

The command iptables --version identifies an available interface, not the complete active firewall design. A modern distribution may use an nftables backend, a firewall manager, or rules managed by containers and hosting tools.

A set-based design, such as nftables sets or an appropriate ipset integration, can be more manageable than thousands of individual rules. Follow the documentation for the installed stack rather than mixing unrelated rule managers.

Plan the change safely

  1. Record the existing rules and verify out-of-band console access.
  2. Preserve trusted administration access and arrange a rollback.
  3. Choose the services and chains you actually intend to restrict.
  4. Validate the country dataset and review rule ordering.
  5. Test allowed and denied traffic, including IPv6 and proxy behavior.
  6. Configure persistence using the distribution’s supported method.

Appending rules after an earlier accept rule may do nothing. Behind a reverse proxy, a firewall may see the proxy’s address rather than the visitor’s.

No universal restart command

The original service iptables restart and save commands aren’t portable across Debian, Ubuntu, CentOS, and Fedora. Rule changes also don’t inherently need a generic service restart. Use your system’s documented persistence and reload mechanism.

Geographic filtering complements rate limits, authentication, patching, and monitoring; it doesn’t replace them.

Images from the original article

How Country-Based IP Blocking Works on a Linux Server: original illustration 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.