Warning: This version is in development and not intended for use in production environments. Use this package at your own risk. It changes firewall rules and can affect network access. For interactive changes, prefer
sudo country-block editso new rules are tested temporarily before they become persistent.
Scripts to block incoming and/or outgoing network traffic from specified countries on Linux systems.
This system uses ipset for handling large IP lists and iptables to enforce the firewall rules. The scripts are designed to be persistent across reboots, configurable, and easy to manage.
IP lists are sourced from IPdeny:
- IPv4: https://www.ipdeny.com/ipblocks/data/countries
- IPv6: https://www.ipdeny.com/ipv6/ipaddresses/blocks
- Rules-Driven: Easily manage which countries to block in a simple text file.
- Directional Rules: Block traffic on a per-country basis for
input,output, orboth. - Efficient IP Management: Uses
ipsetto handle thousands of IP ranges without slowing downiptables. - Optional Persistence: Includes a systemd service to restore confirmed rules after reboot.
- Optional Automatic Updates: Includes a systemd timer to refresh IP lists and apply updated rules weekly.
- Safe Editing: Test rules temporarily before making them persistent.
For supported Debian and Ubuntu releases, install with:
curl -fsSL https://raw.githubusercontent.com/newfullname/country-block/master/install-deb.sh | sudo bashThe installer adds the repository signing key to /usr/share/keyrings, configures
the APT source, and installs the package.
Supported Debian releases: bookworm, trixie.
Supported Ubuntu releases: jammy, noble, resolute.
Debian package installation instructions are available at: https://newfullname.github.io/debian/country-block.html
Ubuntu package installation instructions are available at: https://newfullname.github.io/debian/country-block-ubuntu.html
The following prerequisites and installation steps are for installing from a local clone of this repository.
Ensure the following packages are installed on your system.
bashipsetiptablescurlcoreutils(fordate,rm, etc.)sed
Install them using your distribution's package manager, for example:
- Debian/Ubuntu:
sudo apt-get install ipset iptables curl - Fedora/RHEL:
sudo dnf install ipset iptables curl - Arch:
sudo pacman -S ipset iptables curl
git clone https://github.com/newfullname/country-block.git
cd country-block
sudo make installThis will:
- Create the necessary directories (
/etc/country-block,/var/cache/country-block). - Copy the
country-blockcommand to/usr/local/sbin/. - Copy a default rules file to
/etc/country-block/rules.conf(if it doesn't already exist). - Install
country-block.service,country-block-update.service, andcountry-block.timer. - Leave all systemd units disabled and stopped until you opt in.
After installation, configure which countries you want to block. For remote machines, prefer the safe editor because it tests the new rules before replacing the persistent rules file.
-
Edit the rules file:
sudo country-block edit
-
Add your rules. The format is
<country-codes> <chains> <ip-versions>.
<country-codes>: Comma-separated list of two-letter country codes (e.g.,ru,ir,cn)<chains>: Comma-separated list of chains:input,output,forward, or*for all<ip-versions>: Comma-separated list:v4,v6, or*for both
Example rules.conf:
# Block all traffic (all chains) for Russia and Iran, both IPv4 and IPv6
ru,ir * *
# Block only incoming traffic from China, IPv4 only
cn input v4
Once you have added your rules, test them with rollback before making persistence decisions:
sudo country-block tryIf you are confident the rules are safe and want a direct non-interactive refresh and apply, use sync:
sudo country-block syncsync runs update and then apply. Use the commands separately when you want only one half of that workflow:
sudo country-block update # refresh downloaded lists and /var/cache/country-block/ipsets.save only
sudo country-block apply # restore cached ipsets and apply firewall rulesRerun sudo country-block sync if you change the rules file and need to refresh lists and push the new rules immediately.
Check the current rules, cache, live firewall rules, and systemd units with:
sudo country-block statusAfter you have confirmed the rules work, enable boot-time restore if you want rules to persist across reboots:
sudo systemctl enable country-block.serviceEnable weekly list refreshes and live rule updates if you want updates to run automatically:
sudo systemctl enable --now country-block.timerUse edit for interactive rules changes:
sudo country-block editThe command opens a temporary copy of /etc/country-block/rules.conf with ${EDITOR:-nano}, prepares any missing country ipsets, applies the edited rules temporarily, and asks you to type yes within 120 seconds. If you confirm, the temporary file is installed as the persistent rules file. If you disconnect, press Ctrl-C, or do not confirm in time, the previous firewall state is restored and the persistent rules file is left unchanged.
You can change the confirmation window:
sudo country-block edit --timeout 300To test the current persistent rules with rollback, run try directly:
sudo country-block try --timeout 120For generated rules files or automation, pass a candidate file. Unlike plain apply, try may prepare or download missing candidate ipsets before temporarily applying rules. It does not install the candidate file; use edit or replace /etc/country-block/rules.conf explicitly when you want to save rules.
sudo country-block try --config ./candidate.conf --timeout 120apply remains a direct non-interactive command. It is intended for systemd and for administrators who explicitly want to apply the current persistent rules immediately.
To remove live country-block firewall rules without uninstalling the package:
sudo country-block clearclear removes only country-block managed iptables and ip6tables rules. It does not delete downloaded lists or the saved ipset cache.
country-block.servicerunscountry-block applyduring boot to restore cached ipsets and firewall rules. It is installed disabled by default so package installation and upgrades do not apply firewall rules automatically.country-block.timerruns weekly and triggerscountry-block-update.service. It is installed disabled and stopped by default so package installation and upgrades do not refresh or apply rules automatically.country-block-update.serviceis a oneshot helper used by the timer. It runscountry-block sync, which refreshes the saved ipset cache and applies the updated firewall rules.