iptables常用示例
0x01 安装iptables
# 没有安装iptables可以使用下面的命令
sudo apt-get install iptables
0x02 停用firewall
需要停用ubuntu默认使用的firewall3
# 将会停用firewall并且关闭开机自启
sudo service firewall stop
0x03 安装iptables-persistent
在Ubuntu系统中,所编写的规则重启之后就会失效所以需要软件持久化iptables的规则
# 安装iptables-persistent持久化规则
sudo apt-get iptables-persistent
# 持久化保存设定的规则
sudo netfilter-persistent save
0x04 常用实例
# 防火墙默认状态
Chain INPUT (policy ACCEPT)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT)
pkts bytes target prot opt in out source destination
# 设定INPUT链为DROP
# OUTPUT链默认为ACCEPT,一般不需要设置为DROP
# 这个设置默认丢弃所有入站数据包,即关闭了所有的端口
iptables -P INPUT DROP
下方的命令最大程度的限制了端口的开放
# 允许源端口为22,53,80,443的TCP数据包进入
iptables -A INPUT -p tcp -m multiport --sports 22,53,80,443 -j ACCEPT
# 开放SSH,DNS,HTTP,HTTPS服务相对应的22,53,80,443端口
iptables -A INPUT -p tcp -m multiport --dports 22,53,80,443 -j ACCEPT
# 允许源端口为53的UDP数据包进入
iptables -A INPUT -p udp --sport 53 -j ACCEPT
# 开放基于UDP的DNS服务的53端口
iptables -A INPUT -p udp --dport 53 -j ACCEPT
# 允许外部主机ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
其他文章