The following describes how to define a simple iptables rule: Step 1: clear all defined rules and user-defined rules # iptables-F # iptables-X # iptables-Z Step 2: set the default policy for security and aspect control, set all default policies to DROP # iptables-PINPUTDROP # iptables-PF
The following is a simple example:IptablesSteps and methods for rule definition:
Step 1: Understand all defined rules and user-defined rules
# Iptables-F
# Iptables-X
# Iptables-Z
Step 2: set the default policy. here, all the default policies are set to DROP for security and aspect control)
# Iptables-P INPUT DROP
# Iptables-P FORWARD DROP
# Iptables-P OUTPUT DROP
Step 3: disable Server ping
#/Bin/echo "1">/proc/sys/net/ipv4/icmp_echo_ignore_all
Step 4: disable ip forwarding
#/Bin/echo "0">/proc/sys/net/ipv4/ip_forward
Step 5: allow local loops
# Iptables-a input-I lo-j ACCEPT
# Iptables-a output-o lo-j ACCEPT
Step 6: ensure that the tcp connection is a valid syn packet
# Iptables-a input-I eth0-p tcp! -- Syn-m state -- state NEW-j DROP
Step 7: Open the ssh port (port 22)
# Iptables-a input-I eth0-p tcp -- sport 22-m state -- stateESTABLISHED-j ACCEPT
# Iptables-a output-o eth0-p tcp -- dport 22-m state -- stateNEW, ESTABLISHED-j ACCEPT
Step 8: Open the ftp port (Port 21)
# Iptables-a input-I eth0-p tcp -- sport 21-m state -- stateESTABLISHED-j ACCEPT
# Iptables-a output-o eth0-p tcp -- dport 21-m state -- stateNEW, ESTABLISHED-j ACCEPT
Step 9: Open the www port (port 80)
# Iptables-a input-I eth0-p tcp -- sport 80-m state -- stateESTABLISHED-j ACCEPT
# Iptables-a output-o eth0-p tcp -- dport 80-m state -- stateNEW, ESTABLISHED-j ACCEPT
Step 10: Save
# Iptables-save> iptables-script
# Iptables-restore iptables-script
Or
# Service iptables save