Basic Iptables - Debian/RedHat
SummaryYou can find an easier to read version here: 5dollarwhitebox.org
The SystemDebian Sarge 3.1 Vanilla 2.6.12.4 kernel from mirrors.kernel.org iptables administration utility version 1.2.11-10
PreparationThis How-To is performed on a Debian Sarge 3.1 box, though the commands and syntax should work for any linux distro. Before you can configure iptables, you first must ensure that it has been compiled into the kernel, and that you have the proper userland utilities installed.
CONFIG_IP_NF_CONNTRACK=m CONFIG_IP_NF_FTP=m CONFIG_IP_NF_AMANDA=m CONFIG_IP_NF_TFTP=m CONFIG_IP_NF_IRC=m CONFIG_IP_NF_QUEUE=m CONFIG_IP_NF_IPTABLES=m CONFIG_IP_NF_MATCH_LIMIT=m CONFIG_IP_NF_MATCH_MAC=m CONFIG_IP_NF_MATCH_PKTTYPE=m CONFIG_IP_NF_MATCH_MARK=m CONFIG_IP_NF_MATCH_MULTIPORT=m CONFIG_IP_NF_MATCH_TOS=m CONFIG_IP_NF_MATCH_RECENT=m CONFIG_IP_NF_MATCH_ECN=m CONFIG_IP_NF_MATCH_DSCP=m CONFIG_IP_NF_MATCH_AH_ESP=m CONFIG_IP_NF_MATCH_LENGTH=m CONFIG_IP_NF_MATCH_TTL=m CONFIG_IP_NF_MATCH_TCPMSS=m CONFIG_IP_NF_MATCH_HELPER=m CONFIG_IP_NF_MATCH_STATE=m CONFIG_IP_NF_MATCH_CONNTRACK=m CONFIG_IP_NF_MATCH_UNCLEAN=m CONFIG_IP_NF_MATCH_OWNER=m CONFIG_IP_NF_FILTER=m CONFIG_IP_NF_TARGET_REJECT=m CONFIG_IP_NF_TARGET_MIRROR=m CONFIG_IP_NF_NAT=m CONFIG_IP_NF_NAT_NEEDED=y CONFIG_IP_NF_TARGET_MASQUERADE=m CONFIG_IP_NF_TARGET_REDIRECT=m CONFIG_IP_NF_NAT_AMANDA=m CONFIG_IP_NF_NAT_SNMP_BASIC=m CONFIG_IP_NF_NAT_IRC=m CONFIG_IP_NF_NAT_FTP=m CONFIG_IP_NF_NAT_TFTP=m CONFIG_IP_NF_MANGLE=m CONFIG_IP_NF_TARGET_TOS=m CONFIG_IP_NF_TARGET_ECN=m CONFIG_IP_NF_TARGET_DSCP=m CONFIG_IP_NF_TARGET_MARK=m CONFIG_IP_NF_TARGET_LOG=m CONFIG_IP_NF_TARGET_ULOG=m CONFIG_IP_NF_TARGET_TCPMSS=m CONFIG_IP_NF_ARPTABLES=m CONFIG_IP_NF_ARPFILTER=m CONFIG_IP_NF_ARP_MANGLE=m CONFIG_IP_NF_COMPAT_IPCHAINS=m CONFIG_IP_NF_NAT_NEEDED=y CONFIG_IP_NF_COMPAT_IPFWADM=m CONFIG_IP_NF_NAT_NEEDED=y This isn't all that necessary, since you'll find out real quick whether iptables works or not once we try to add some rules.
iptables 1.2.11-10 Linux kernel 2.4+ iptables administration to
/sbin/iptables
APT # apt-get update && apt-get install iptables
Preparing ################################# [100%]
The Main Files
Debian
RedHat
A Little About IPTables
Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
Configuring Rule Sets
Lets break that down:
Next, we will want to use some standard rules for general network traffic. This goes a bit beyond the basic stuff, however iptables can determine the 'state' that a packet is in. This has to do with standard TCP communication. For example, the 3 way handshake between two hosts when transmitting data.
# iptables -A FORWARD -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT # iptables -A OUTPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT The last rule obviously allows any traffic the leave the server.
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 192.168.1.10 10.1.15.1 tcp dpt:ssh ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED Chain FORWARD (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state NEW,RELATED,ESTABLISHED
INIVIDUAL REJECTS FIRST: ----------------------------------------------------------------------- BAD GUYS (Block Source IP Address): # iptables -A INPUT -s 172.34.5.8 -j DROP NO SPAMMERS (notice the use of FQDN): # iptables -A INPUT -s mail.spammer.org -d 10.1.15.1 -p tcp --dport 25 -j REJECT -----------------------------------------------------------------------
# iptables -A INPUT -d 10.1.15.1 -p tcp --dport 22 -j ACCEPT Sendmail/Postfix: # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 25 -j ACCEPT FTP: (Notice how you can specify a range of ports 20-21) # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 20:21 -j ACCEPT Passive FTP Ports Maybe: (Again, specifying ports 50000 through 50050 in one rule) # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 50000:50050 -j ACCEPT HTTP/Apache # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 80 -j ACCEPT SSL/Apache # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 443 -j ACCEPT IMAP # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 143 -j ACCEPT IMAPS # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 993 -j ACCEPT POP3 # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 110 -j ACCEPT POP3S # iptables -A INPUT -d 10.1.15.1 -p tcp --dport 995 -j ACCEPT Any Traffic From Localhost: # iptables -A INPUT -d 10.1.15.1 -s 127.0.0.1 -j ACCEPT ICMP/Ping: # iptables -A INPUT -d 10.1.15.1 -p icmp -j ACCEPT-----------------------------------------------------------------------
Or, reject everything else coming through to any IP: # iptables -A INPUT -j REJECT # iptables -A FORWARD -j REJECT----------------------------------------------------------------------- Notice the we do the global REJECT lines last! These must be last.
Saving Rule Sets
The Debian WayThe old style init script is no longer in Sarge by default, but it is still around for legacy use. I believe the new way is to use ' /etc/network/if-up.d' and '/etc/network/if-down.d' for iptables scripts (but I don't like that).
# chmod +x /etc/init.d/iptables # mkdir /var/lib/iptables # chmod 700 /var/lib/iptables Now that you have the script in place you can do the needful.
Active RulesThe Active rules are those loaded when starting iptables: # /etc/init.d/iptables save activeSaving iptables ruleset: save "active" with counters. This saves your rules in /var/lib/iptables/active
Inactive RulesYou can also configure a second set of rules for when you stop
iptables called 'inactive'. Iptables doesn't actually “stop�, it just
flushes out the rule sets that are in place and then loads the
'inactive' rules. Loading iptables ruleset: load "inactive"
Saving iptables ruleset: save "inactive" with counters.
The RedHat WayThe RedHat INIT script is very similar. You can use it to start and stop iptables, as well as save rule sets.
This will save your rules to '/etc/sysconfig/iptables'.
Starting iptables [OK]
Stopping iptables [OK]
Manual Save and RestoreYou can also manually use the iptables-save and iptables-restore utilities like so:
Save the rules to a files# iptables-save > /root/iptables-save.out
Restore the rules# iptables-restore -c /root/iptables-save.outThe -c tells iptables-restore that this is file was created using iptables-save, which outputs the rules as “counters�.
ConclusionAnd there you go, iptables at its very basic. The uses of iptables are too numerous to even start truly doing a howto on them. However, for basic security and understanding of IPTables, I hope this might have helped you. If there is anything I could add, please feel free to email me.
---
Resources
|
www.seamlessenterprise.com
One number. One voicemail. Seize the lead. Sprint Mobile Integration.
www.seamlessenterprise.com
One Number. One Voicemail.
Make it easier for clients to reach you. Turn your desk phone and mobile phone into one with Sprint Mobile Integration.
www.seamlessenterprise.com
One number. One voicemail. Sprint Mobile Integration.
www.seamlessenterprise.com
One number. one voicemail. Seize the lead with Sprint. Learn more
AT&T Synaptic Compute as a Service. Boost your power on demand.
Trial: IBM Cognos Express Reporting, Analysis & Planning
Learn benefits of Simpana software.
View the Gartner Video
Sprint 4G - The Ultimate Mobile Broadband
Click here
SAP-Business Objects Crystal Reports Server
Complete reporting without hidden costs. Free Trial



![Creative Commons Attribution License [Creative Commons Attribution License]](http://creativecommons.org/images/public/somerights20.gif)




Recent comments
9 hours 54 min ago
11 hours 36 sec ago
11 hours 35 min ago
15 hours 28 min ago
16 hours 28 min ago
18 hours 24 min ago
19 hours 48 min ago
22 hours 23 min ago
1 day 4 hours ago
1 day 11 hours ago