一键配置CentOS iptables防火墙的Shell脚本分享,centosiptables


一键配置CentOS iptables防火墙Shell脚本分享
转自:http://www.jbxue.com/article/23208.html
手里几台VPS配置iptables太繁琐,看到了朱哥的LNMP脚本里有一个自动配置iptables防火墙的脚本,借来改了一下,给需要的人用;

只提供常用端口的设置,如果你有特殊需求只需自行添加或减少相应的端口即可;
使用方法:
chmod +x iptables.sh
./iptables.sh

设置iptables开机自动启动:
chkconfig --level 345 iptables on

完整Shell:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function support_distro(){
if [ -z "`egrep -i "centos" /etc/issue`" ];then
echo "Sorry,iptables script only support centos system now."
exit 1
fi
}
support_distro
echo "============================iptables configure============================================"
# Only support CentOS system
# 获取SSH端口
if grep "^Port" /etc/ssh/sshd_config>/dev/null;then
sshdport=`grep "^Port" /etc/ssh/sshd_config | sed "s/Port\s//g" `
else
sshdport=22
fi
# 获取DNS服务器IP
if [ -s /etc/resolv.conf ];then
nameserver1=`cat /etc/resolv.conf |grep nameserver |awk 'NR==1{print $2 }'`
nameserver2=`cat /etc/resolv.conf |grep nameserver |awk 'NR==2{print $2 }'`
fi
IPT="/sbin/iptables"
# 删除已有规则 www.jbxue.com
$IPT --delete-chain
$IPT --flush
# 禁止进,允许出,允许回环网卡
$IPT -P INPUT DROP 
$IPT -P FORWARD DROP 
$IPT -P OUTPUT ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关连接的通行
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 限制80端口单个IP的最大连接数为10
$IPT -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP
# 允许80(HTTP)/873(RSYNC)/443(HTTPS)/20,21(FTP)/25(SMTP)端口的连接
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
# 允许SSH端口的连接,脚本自动侦测目前的SSH端口,否则默认为22端口
$IPT -A INPUT -p tcp -m tcp --dport $sshdport -j ACCEPT
# 允许ping
$IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT 
$IPT -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
# 允许DNS
[ ! -z "$nameserver1" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver1 --dport 53 -j ACCEPT
[ ! -z "$nameserver2" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver2 --dport 53 -j ACCEPT
# 保存规则并重启IPTABLES
service iptables save
service iptables restart
echo "==========iptables configure completed================"

:linux系统上iptables防火墙的IP段访问限制的脚本

大致为
iptables -A INPUT -s 210.22.23.0/24 -p tcp --dport 80 -j ACCEP
iptables-save
iptables -L

至于太整体的防火墙规则你可以访问
www.ha97.com/4096.html
 

教对于用 iptables 配置LINUX服务器防火墙的脚本

1. vi iptables.sh
2. 在iptables.sh文件中写以下内容
#!/bin/bash
iptables -P INPUT DROP
IP=192.168.11.0/24
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT (这三条满足入站要求1)
iptables -A INPUT -s $IP -p tcp --dport 23 -j ACCEPT 允许telnet
iptables -A INPUT -s $IP -p tcp --dport 22 -j ACCEPT 允许ssh
iptables -A INPUT -s $IP -p icmp -j ACCEPT 允许ping

我觉得这就可以了,因为output链我们没有关闭,所以是允许本主机访问其他主机的服务的,关键是其他主机是否会返回信息。
 

相关内容