Linux software firewall¶
iptables is the name of the software firewall bundled with most UKFast Linux servers.
Whilst we recommend that you use your hardware firewall for most things, it may still be helpful / necessary to be able to use iptables for quickly blocking IP addresses or closing ports.
As with most Linux packages, the man pages are a good place to start to learn more about the package and its usage, but if you’re just looking to get up and running quickly, here are a few examples:
Command Structure¶
iptables -I OUTPUT -p tcp -s 192.168.0.1 --dport 2020 -j ACCEPT
The individual elements of the above commands are explained below
iptablesThe command itself.
-IInsert the new rule at the top of the chain.
IPTables rules are read in sequentially, so the order the rules are in is very important. If the rule you’re adding doesn’t need to be the first, can you alternatively use
-Ato append it to the bottom of the chain or-I {CHAIN} {LINE NUMBER}to insert it at a particular line number.OUTPUTThe chain name.
There are 2 main chains that you’ll likely be interested in,
INPUTandOUTPUT. Simply put, rules inINPUTaffect inbound traffic and rules inOUTPUTaffect outbound.-pThe protocol that the rule relates to.
Valid protocols are
tcp,udporicmp-sSource IP address that the rule pertains to.
Can also be written longhand as
--sourceor--src.Inversely, destination IP addresses can be specified with
-d,--destinationor--dst.Can also be given a range such as
192.168.0.0/24or192.168.0.0/255.255.255.0.--dportDestination port that the rule relates to.
Can also be written as
--destination-port.Alternatively, source port can be specified with
--sportor--source-port.Ranges of ports can be specified with a
:, for example1096:2999.-jChain to jump to when previous elements have been matched.
Basic chains that can be used here are
ACCEPT,DROPandREJECT.ACCEPTwill accept packet,REJECTwill deny packet andDROPwill result in it being silently dropped.
List current rules¶
iptables -vnL
Blocking a port¶
iptables -I INPUT -p tcp --dport 6667 -j REJECT
Blocking an IP¶
iptables -I INPUT -p tcp -s 1.2.3.4 -j DROP
Blocking a port for a particular IP¶
iptables -I OUTPUT -p tcp -s 192.168.0.1 --dport 25 -j DROP
Note
The site http://iptabl.es can be used to generate IPTables rules without having to memorise the above syntax. Usage is explained on the frontpage, but a few examples are below:
curl iptabl.es/drop/2020/8.8.8.8