5.13. Setting and Controlling IP sets using iptables
The essential differences between
firewalld
and the iptables (and ip6tables) services are:
- The iptables service stores configuration in
/etc/sysconfig/iptables
and/etc/sysconfig/ip6tables
, whilefirewalld
stores it in various XML files in/usr/lib/firewalld/
and/etc/firewalld/
. Note that the/etc/sysconfig/iptables
file does not exist asfirewalld
is installed by default on Red Hat Enterprise Linux. - With the iptables service, every single change means flushing all the old rules and reading all the new rules from
/etc/sysconfig/iptables
, while withfirewalld
there is no recreating of all the rules. Only the differences are applied. Consequently,firewalld
can change the settings during runtime without existing connections being lost.
Both use iptables tool to talk to the kernel packet filter.
To use the
Copy to Clipboard
Copied!
iptables
and ip6tables
services instead of firewalld
, first disable firewalld
by running the following command as root
:
systemctl disable firewalld systemctl stop firewalld
~]# systemctl disable firewalld
~]# systemctl stop firewalld
Then install the iptables-services package by entering the following command as
Copy to Clipboard
Copied!
The iptables-services package contains the
root
:
yum install iptables-services
~]# yum install iptables-services
iptables
service and the ip6tables
service.
Then, to start the
Copy to Clipboard
Copied!
To enable the services to start on every system start, enter the following commands:
Copy to Clipboard
Copied!
iptables
and ip6tables
services, enter the following commands as root
:
systemctl start iptables systemctl start ip6tables
~]# systemctl start iptables
~]# systemctl start ip6tables
systemctl enable iptables systemctl enable ip6tables
~]# systemctl enable iptables
~]# systemctl enable ip6tables
The ipset utility is used to administer IP sets in the Linux kernel. An IP set is a framework for storing IP addresses, port numbers, IP and MAC address pairs, or IP address and port number pairs. The sets are indexed in such a way that very fast matching can be made against a set even when the sets are very large. IP sets enable simpler and more manageable configurations as well as providing performance advantages when using iptables. The iptables matches and targets referring to sets create references which protect the given sets in the kernel. A set cannot be destroyed while there is a single reference pointing to it.
The use of ipset enables iptables commands, such as those below, to be replaced by a set:
Copy to Clipboard
Copied!
The set is created as follows:
Copy to Clipboard
Copied!
The set is then referenced in an iptables command as follows:
Copy to Clipboard
Copied!
If the set is used more than once a saving in configuration time is made. If the set contains many entries a saving in processing time is made.
iptables -A INPUT -s 10.0.0.0/8 -j DROP iptables -A INPUT -s 172.16.0.0/12 -j DROP iptables -A INPUT -s 192.168.0.0/16 -j DROP
~]# iptables -A INPUT -s 10.0.0.0/8 -j DROP
~]# iptables -A INPUT -s 172.16.0.0/12 -j DROP
~]# iptables -A INPUT -s 192.168.0.0/16 -j DROP
ipset create my-block-set hash:net ipset add my-block-set 10.0.0.0/8 ipset add my-block-set 172.16.0.0/12 ipset add my-block-set 192.168.0.0/16
~]# ipset create my-block-set hash:net
~]# ipset add my-block-set 10.0.0.0/8
~]# ipset add my-block-set 172.16.0.0/12
~]# ipset add my-block-set 192.168.0.0/16
iptables -A INPUT -m set --set my-block-set src -j DROP
~]# iptables -A INPUT -m set --set my-block-set src -j DROP