Centos / Redhat Iptables Firewall Configuration Tutorial
Centos / Redhat Iptables Firewall Configuration Tutorial
Tutorial
by nixCraft on November 10, 2009 · 31 comments· LAST UPDATED June 17, 2010
in CentOS, Linux, RedHat and Friends <http://www.cyberciti.biz/faq/rhel-fedorta-linux-iptables-firewall-
configuration-tutorial/>
How do I configure a host-based firewall called Netfilter (iptables) under CentOS / RHEL / Fedora / Redhat
Enterprise Linux?
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is
activated by default. This firewall is controlled by the program called iptables. Netfilter filtering take place at the
kernel level, before a program can even process the data from the network packet.
The default config files for RHEL / CentOS / Fedora Linux are:
/etc/sysconfig/iptables - The system scripts that activate the firewall by reading this file.
iptables --line-numbers -n –L
Sample outputs:
chkconfig iptables on
service iptables start
# restart the firewall
service iptables restart
# stop the firewall
service iptables stop
Understanding Firewall
1. INPUT - The default chain is used for packets addressed to the system. Use this to open or close incoming
ports (such as 80,25, and 110 etc) and ip addresses / subnet (such as 202.54.1.20/29).
2. OUTPUT - The default chain is used when packets are generating from the system. Use this open or close
outgoing ports and ip addresses / subnets.
3. FORWARD - The default chains is used when packets send through another interface. Usually used when you
setup Linux as router. For example, eth0 connected to ADSL/Cable modem and eth1 is connected to local
LAN. Use FORWARD chain to send and receive traffic from LAN to the Internet.
4. RH-Firewall-1-INPUT - This is a user-defined custom chain. It is used by the INPUT, OUTPUT and FORWARD
chains.
Target Meanings
/etc/sysconfig/iptables
# vi /etc/sysconfig/iptables
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
Find lines:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
Update as follows to change the default policy to DROP from ACCEPT for the INPUT and FORWARD
built-in chains:
Update it as follows:
-A RH-Firewall-1-INPUT -j LOG
-A RH-Firewall-1-INPUT -j DROP
COMMIT
Open Port
To open port 80 (Http server) add the following before COMMIT line:
To open port 53 (DNS Server) add the following before COMMIT line:
To open port 443 (Https server) add the following before COMMIT line:
To open port 25 (smtp server) add the following before COMMIT line:
IPTABLES_MODULES="ip_conntrack_ftp"
Edit /etc/sysctl.conf to defend against certain types of attacks and append / update as follows:
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
#net.ipv4.icmp_ignore_bogus_error_messages = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
See previous FAQ, "Linux Kernel /etc/sysctl.conf Security Hardening" for more details.
You can skip /etc/sysconfig/iptables file and create a shell script from scratch as follows:
#!/bin/bash
# A sample firewall shell script
IPT="/sbin/iptables"
SPAMLIST="blockedip"
SPAMDROPMSG="BLOCKED IP DROP"
SYSCTL="/sbin/sysctl"
BLOCKEDIPS="/root/scripts/blocked.ips.txt"
# load modules
modprobe ip_conntrack
if [ -f "${BLOCKEDIPS}" ];
then
# create a new iptables list
$IPT -N $SPAMLIST
# Block sync
$IPT -A INPUT -i ${PUB_IF} -p tcp ! --syn -m state --state NEW -m limit --limit 5/m --
limit-burst 7 -j LOG --log-level 4 --log-prefix "Drop Sync"
$IPT -A INPUT -i ${PUB_IF} -p tcp ! --syn -m state --state NEW -j DROP
# Block Fragments
$IPT -A INPUT -i ${PUB_IF} -f -m limit --limit 5/m --limit-burst 7 -j LOG --log-level
4 --log-prefix "Fragments Packets"
$IPT -A INPUT -i ${PUB_IF} -f -j DROP
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 5/m --
limit-burst 7 -j LOG --log-level 4 --log-prefix "XMAS Packets"
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP #XMAS
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -m limit --limit 5/m --
limit-burst 7 -j LOG --log-level 4 --log-prefix "Fin Packets Scan"
$IPT -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -j DROP # FIN packet scans
# Allow ssh
$IPT -A INPUT -i ${PUB_IF} -p tcp --destination-port 22 -j ACCEPT
exit 0
Recommend readings:
See all our iptables related FAQs, tutorials, and shell scripts.
Ip6tables (IPv6) firewall configurations.
Read iptables and sysctl man pages.