前言

为了服务器安全,我们经常会使用 iptables 来实现访问控制。

常用命令

1、查看当前 iptables 状态

iptables -nL #默认查看 filter 表的状态,如果需要查看其他表的状态加上 -t 表名

iptables -nL --line-numbers #可以列出序列号,在插入或者删除的时候就不用自己去数了

iptables -nL --line-numbers --verbose #可以查看到包过滤的流量统计,访问次数等

2、插入一条记录

iptables -I INPUT 1 -i lo -j ACCPET #在第一条的位置插入一条记录,接受所有来自 lo 网口的访问

iptables -I INPUT 2 -s 192.168.1.0/24 -j ACCPET # 如果在 INPUT 中不指明在第几条插入,默认就是在第一条插入

3、追加一条记录

iptables -A INPUT -s 192.168.2.0/24 -j ACCEPT #在 INPUT 最后追加一条记录。

4、删除一条记录

iptables -D INPUT 7 #删除第 7 条记录

5、针对协议开放

iptables -I INPUT -p imcp -j ACCEPT

6、针对端口开放(需要指明协议)

iptables -I INPUT -p tcp --dport 22 -j ACCEPT

7、限制 ip 端口访问

iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCPET

8、拒绝所有访问

iptables -A INPUT -j DROP #这个一般放到最后,不然会对前面的规则造成影响。

9、根据时段限制访问

iptables -A INPUT -p tcp -m time --timestart 00:00 --timestop 02:00 -j DROP #这里的时间是指 UTC 时间记得换算

10、限制单个 IP 一分钟内建立的连接数

iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 25 -j REJECT

11、保存 iptables 规则

iptables-save > /etc/sysconfig/iptables

12、从文件里面恢复 iptables 规则

iptables-restore < /etc/sysconfig/iptables

13、对外建立的连接经过 INPUT 不拦截

iptables -I INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

14、端口转发(本机 8080 转发到远程 192.168.1.22:80)

iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 8080 -j DNAT --to 192.168.1.22:80

iptables -t nat -A POSTROUTING -j MASQUERADE

echo 1 > /proc/sys/net/ipv4/ip_forward #需要打开网转发

iptables -t filter FORWARD -d 192.168.1.22/32 -j ACCEPT #转发的 FROWARD 要允许双方的数据传输

iptables -t filter FORWARD -s 192.168.1.22/32 -j ACCEPT

总结

iptables 命令直接操作 iptables 还是比较简单的,真的用不惯 firewalld 之类的工具。