[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedFebruary 21, 2023

Using ipset to block IP addresses – firewall

Using ipset to block IP addresses – firewall

ipset is an excellent tool that can be used to efficiently block a list of IP addresses at the firewall level (it uses iptables).  It really helps in easily adding IP addresses and maintaining IP address blocklists.

The below guide sets up ipset to block a list of IP addresses and includes several commands to save/restore said IP lists.

This guide assumes you on are on form of Linux (Ubuntu 16.10 Server is used below).

 

Installing ipset

Let’s first install ipset.  On Debian/Ubuntu we can do:

sudo apt-get install ipset

On CentOS/Amazon-Linux you would do:

sudo yum install ipset

 

Creating our “blacklist”

Let’s first start by creating a list – we’ll call our list ‘blacklist’ .

Note you can create several types of ipset lists, with the type defining the IP address input formats.  The two we’re interested in here is ip and nethash.  ip simply allows defining IP addresses (in their full form) and nethash allows (and requires) CIDR format – which allows us to block entire IP ranges (which if you tried to add to an ipset using hash:ip, could very easily fill the ipset list up to it’s maximum).

To create an ip type list:

sudo ipset create blacklist hash:ip hashsize 4096

To create a nethash (CIDR) type list:

sudo ipset create blacklist nethash

 

Enabling (and deleting) the list in iptables

Now that we’ve created out list, we need to tell iptables to use it:

sudo iptables -I INPUT -m set –match-set blacklist src -j DROP

sudo iptables -I FORWARD -m set –match-set blacklist src -j DROP

Note, we can always remove these rules (to get iptables to not use the list) by:

sudo iptables -D INPUT -m set –match-set blacklist src -j DROP

sudo iptables -D FORWARD -m set –match-set blacklist src -j DROP

See Make ip-tables (firewall) rules persistent for saving the above ip-table rules permanently.

Alternatively, you can list all iptables rules (of which ipset will be in there) and then select a rule to delete.  For example, the below commands will list, and then delete the listed rule 1:

sudo iptables -L –list-numbers

sudo iptables -D INPUT 1

 

Setting ipset to be persistent (not lose lists on reboot)

If your distro is using systemd (Ubuntu, Arch, RHEL 7+, etc.) then you create a service to load ipset tables at boot (as well as save the table/s when the service is stopped, e.g. at shutdown etc.).

The below is for a Ubuntu (16.04) server, so if you are using another distro you might need to modify the following to suit.

For the service file (example) below, I’m only going to be saving a single ipset: blacklist, but you can modify it to save all ipsets (if you have multiple).

Let’s start by creating our service file based off this excellent post by selivan:

/etc/systemd/system/ipset-persistent.service

 

[Unit]

Description=ipset persistancy service

DefaultDependencies=no

Requires=netfilter-persistent.service

Requires=ufw.service

Before=network.target

Before=netfilter-persistent.service

Before=ufw.service

ConditionFileNotEmpty=/etc/ipsets.conf

[Service]

Type=oneshot

RemainAfterExit=yes

ExecStart=/sbin/ipset restore -f -! /etc/ipsets.conf

# save on service stop, system shutdown etc.

ExecStop=/sbin/ipset save blacklist -f /etc/ipsets.conf

[Install]

WantedBy=multi-user.target

RequiredBy=netfilter-persistent.service

RequiredBy=ufw.service

Note: the above requires both ufw  and netfilter-persistent packages.  See  Make ip-tables (firewall) rules persistent for more information on configuring persistent ip-tables.

Now all that’s left is to start and enable the service:

sudo systemctl daemon-reload

sudo systemctl start ipset-persistent

sudo systemctl enable ipset-persistent

Now, on shutdown (or stopping the service) our ipset-persistent service should backup the current blacklist ipset and restore it on reboot.

 

Listing / adding / removing IP addresses from our blacklist

To list current IP addresses in our list, do:

sudo ipset list

To add an IP address, simply do:

sudo ipset add blacklist <IP-ADDRESS_OR_CIDR-IF-NETHASH>

To remove an IP address, do:

sudo ipset del blacklist <IP-ADDRESS_OR_CIDR-IF-NETHASH>

To remove ALL IP addresses in a list, use the flush command

sudo ipset flush blacklist

 

Querying to see if IP address is in list

You can use the “test” command to check if an IP address is in a specific list:

sudo ipset test blacklist <IP-ADDRESS>

 

Saving / restoring IP lists

To save the current list to a file:

sudo ipset save blacklist -f ipset-blacklist.backup

To restore said file:

sudo ipset restore -! < ipset-blacklist.backup

 

Deleting a list

You might want to delete a list.  To do so you can use the ‘destroy’ command:

sudo ipset destroy blacklist

Note1, that you first need to remove any lists that are in use from iptables.  See above section Enabling the list in iptables for details on how to do this.

Note2: use the flush command to simply delete all ip addreses in the list without deleting the list itself.

Keep reading
How to use bash exit command.
September 4, 2023

How to use bash exit command.

How To Use Bash Exit Command. In this article, we will cover the Bash exit built-in command and the exit statuses of the executed commands. Exit Status Each shell command returns an exit code when it terminates, either successfully or unsuccessfully. By convention, an exit code of zero indicates…

Read article
How to create a todo list in Bash.
September 1, 2023

How to create a todo list in Bash.

How To Create A Todo List In Bash. Bash is a powerful scripting language that can handle text manipulation and file operations quite well. Here are the basic steps to create a simple to-do list application in Bash: Create a Bash Script File: Start by creating a new Bash script file using a text…

Read article
How to install SolusVM on Ubuntu 20.4
August 31, 2023

How to install SolusVM on Ubuntu 20.4

How To Install SolusVm On Ubuntu 20.4 The management server is a physical dedicated server that meets the following requirements: OS AlmaLinux 8,CentOS 7, CentOS Stream,Debian 10, Debian 11, Debian 12,Ubuntu 18, Ubuntu 20, Ubuntu 22 CPU Minimum 4 cores/8 threads RAM Minimum 4 GiBHighly recommended…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00