1. 程式人生 > >linux防火牆指令碼,一鍵解決防火牆規則帶來的痛苦。

linux防火牆指令碼,一鍵解決防火牆規則帶來的痛苦。

指令碼如下:

#1.
echo "1 ------------------------------------------------------------------------------------"
service iptables start
#2.
echo "2 ------------------------------------------------------------------------------------"
#3.
echo "3 ------------------------------------------------------------------------------------"
iptables -t filter -F
iptables -t filter -Z
iptables -t filter -X
#4.
echo "4 ------------------------------------------------------------------------------------"
iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT ACCEPT
iptables -t filter -P FORWARD ACCEPT
#5.
echo "5 ------------------------------------------------------------------------------------"
iptables -A     INPUT  -i lo -j ACCEPT
iptables -A     OUTPUT -o lo -j ACCEPT
#6.
echo "6 ------------------------------------------------------------------------------------"
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#7.
echo "7 ------------------------------------------------------------------------------------"
#icmp-ping
iptables -A INPUT -p icmp -j ACCEPT

#sshd
iptables -A INPUT -p tcp --dport 22   -j ACCEPT

#zabbix
iptables -A INPUT -p tcp --dport 10050 -j ACCEPT

#mysql
iptables -A INPUT -p tcp --dport 3306   -j ACCEPT


#redis
iptables -A INPUT -p tcp --dport 6379   -j ACCEPT

#8.
echo "8 ------------------------------------------------------------------------------------"
service iptables save
service iptables restart
#9.
echo "9 ------------------------------------------------------------------------------------"
cat /etc/sysconfig/iptables

新建檔案firewall_rule.sh

然後放入linux檔案目錄下

使用命令賦許可權,linux下執行shell指令碼是需要許可權的:chmod +x firewall_rule.sh

然後更改上述指令碼,比如需要ssh,我們就要開放22埠規則:

iptables -A INPUT -p tcp --dport 22   -j ACCEPT
要開放tomcat規則如下,預設8080埠:
iptables -A INPUT -p tcp --dport 8080   -j ACCEPT
把你需要的開放的埠補充上述指令碼,然後執行
./firewall_rule.sh
大功完成!