Ubuntu iptables保存

Ubuntu iptables保存

摘要

Ubuntu iptables保存 ,iptables端口过滤

iptables

允许3306

sudo iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

保存还原防火墙规则

切换到root用户执行,sudo会提示无权限

/sbin/iptables-save > /etc/iptables.up.rules

还原规则

/sbin/iptables-restore < /etc/iptables.up.rules

/etc/iptables.up.rules

/etc/iptables.up.rules文件不建议手动修改,但是也是可以手动修改的.

备份/etc/iptables.up.rules规则

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
-A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT

开机还原防火墙规则

sudo vi /etc/rc.local
#在exit 0上一行添加如下内容
/sbin/iptables-restore < /etc/iptables.up.rules

iptables常用命令

#列出已经有的规则
iptables -nL --line-number

#删除指定
iptables -D INPUT 2

#删除所有
iptables -F

#阻止置顶IP访问
iptables -I INPUT -s 103.199.126.22  -j DROP