配置Linux下的防火墙,也就是iptables,是大家比较头疼的问题,不是因为配置难,而是因为简单配置容易,但是想要根据自己的需求进行配置比较困难。iptables本身包含的规则以及链比较复杂,可以实现转发等各种强大的功能,在作为单机使用的时候,还是比较简单的,下面简单讲解分享一段Linux web服务器上比较使用的iptables脚本,有需要的可以根据自己需求在此基础上进行修改。在使用之前一定看好自己需要开放的端口等。
首先清空之前的规则,并且设定默认动作都为Accept
echo "clean all rules before" iptables -F iptables -X echo "setting up default rules" iptables -P FORWARD ACCEPT iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT
设定INPUT规则
echo "setting up input chain" /sbin/iptables -A INPUT -i lo -j ACCEPT #allow local address /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #allow exist connection #/sbin/iptables -A INPUT -s 1.2.3.4 -j ACCEPT #while list /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT #ssh /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT #http /sbin/iptables -A INPUT -p udp --sport 53 -j ACCEPT #DNS /sbin/iptables -A INPUT -p udp --sport 123 -j ACCEPT #ntp /sbin/iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT #ping /sbin/iptables -P INPUT DROP
其中注释掉的的ip地址1.2.3.4为默认的服务器管理白名单ip,也就是只用从1.2.3.4这个ip才能连上服务器的22端口进行ssh管理,有条件的可以注释掉下面的22端口,并且去掉白名单的注释。如果没有条件使用这个白名单的,可以不理会注释内容。
下面是是设置OUTPUT规则
/sbin/iptables -A OUTPUT -o lo -j ACCEPT #allow local address /sbin/iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #allow exist connection /sbin/iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT #yum /sbin/iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT #https /sbin/iptables -A OUTPUT -p udp --dport 53 -j ACCEPT #DNS /sbin/iptables -A OUTPUT -p udp --dport 123 -j ACCEPT #ntp /sbin/iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT #ping /sbin/iptables -P OUTPUT DROP
基本上OUTPUT也就没有什么好说的了,按自己的需要开放吧。FORWARD链我们就不需要设置了,只是简单的单机出入控制规则而已。下面加上一些其他细则,构成一个防火墙开启的脚本。
#!/bin/bash # iptables for normal web sever # you should modify it according to your purpose # by Jarett # 2012.9.3 echo "clean all rules before" iptables -F iptables -X echo "setting up default rules" iptables -P FORWARD ACCEPT iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT echo "setting up input chain" /sbin/iptables -A INPUT -i lo -j ACCEPT #allow local address /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #allow exist connection #/sbin/iptables -A INPUT -s 1.2.3.4 -j ACCEPT #while list /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT #ssh /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT #http /sbin/iptables -A INPUT -p udp --sport 53 -j ACCEPT #DNS /sbin/iptables -A INPUT -p udp --sport 123 -j ACCEPT #ntp /sbin/iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT #ping /sbin/iptables -P INPUT DROP echo "setting up output chain" /sbin/iptables -A OUTPUT -o lo -j ACCEPT #allow local address /sbin/iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #allow exist connection /sbin/iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT #yum /sbin/iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT #https /sbin/iptables -A OUTPUT -p udp --dport 53 -j ACCEPT #DNS /sbin/iptables -A OUTPUT -p udp --dport 123 -j ACCEPT #ntp /sbin/iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT #ping /sbin/iptables -P OUTPUT DROP /sbin/service iptables save iptables -vnL
下载链接:
http://pan.baidu.com/share/link?shareid=125149&uk=2905237318
你可能喜欢
本文标题:配置标准的web服务器iptables
转载请注明来源,如果喜欢本站可以Feed订阅本站。
0 条评论。