Debian/Ubuntu-shell脚本来管理iptables安全策略

时间:2022-04-27
本文章向大家介绍Debian/Ubuntu-shell脚本来管理iptables安全策略,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

在Centos上都有iptables-services或者firewalld等iptables管理工具。那在Debian系列用什么管理工具呢?

使用Debian

Debian安装完以后,要设置安全策略,发现没有iptables-services管理工具,What?好吧!只能找找有什么工具,发现有一个ufw。不是没有管理工具,只是我不会用。

shell脚本管理

ufw用不惯就不强求,自己写个脚本来解决:

#!/bin/bash
source /etc/profile
function save(){
iptables-save  > /etc/iptables.rules
}
function restart(){
iptables-restore 
}
function stop(){
iptables -t nat -F
iptables -t nat -X
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -P PREROUTING ACCEPT
iptables -t mangle -P INPUT ACCEPT
iptables -t mangle -P FORWARD ACCEPT
iptables -t mangle -P OUTPUT ACCEPT
iptables -t mangle -P POSTROUTING ACCEPT
iptables -F
iptables -X
iptables -P FORWARD ACCEPT
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t raw -F
iptables -t raw -P PREROUTING ACCEPT
iptables -t raw -P OUTPUT ACCEPT
}
if [ "$1" == "save" ]
then
save
elif [[ "$1" == "reload" || "$1" == "restart" ]]
then
restart
elif [[ "$1" == "stop" ]]
then
stop
else
echo "$0 [save|restart|reload|stop]"
fi

总结

只要知道iptables这个命令自己用脚本多方便啊!