文章目录
-
-
- 查看当前的iptables规则
- 快速清除所有的iptables规则
- 添加一条限制固定目的ip、端口的iptables规则
- 添加一条限制固定目的ip、多个端口的iptables规则
- 添加一条限制对固定目的ip、端口来源的ip地址范围的iptables规则
- 添加一条目的ip固定、源mac为确定值的iptables规则
- 添加一条源ip已知抓取关键字符串的iptables规则
- 限制时间相关的iptables规则对源ip为172.16.0.0/16目的ip为172.16.100.10时间为星期六星期日的23点至24点之间的tcp数据包通过80端口时丢弃
- 保存规则方式
- iptables规则中的nat地址转换
- iptables规则中nat进行转发功能
-
查看当前的iptables规则
命令:iptables -vnL
[root@centos ~]# iptables -vnL
Chain INPUT (policy ACCEPT 4629K packets, 1620M bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 172.16.0.0/16 172.16.100.10 multiport dports 20,22,80
0 0 DROP tcp -- * * 0.0.0.0/0 172.16.100.10 tcp dpt:80 source IP range 172.16.100.5-172.16.100.10
0 0 DROP all -- * * 0.0.0.0/0 172.16.100.10 source IP range 172.16.100.5-172.16.100.10
0 0 ACCEPT all -- * * 172.16.0.100 0.0.0.0/0 MAC 00:50:56:12:34:56
0 0 REJECT all -- * * 172.16.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
0 0 DROP all -- * * 172.16.0.0/16 172.16.100.10 TIME from 23:00:00 to 23:59:59 on Sat,Sun UTC
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 TIME from 23:00:00 to 23:59:59 on Sat,Sun UTC
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 420 packets, 37668 bytes)
pkts bytes target prot opt in out source destination
相关字段 | 含义 |
---|---|
pkts | 发送的包数量 |
bytes | 发送的包总共的大小 |
target | 目标,即想要效果是drop还是accept或者其他 |
prot | 协议 |
in | 进来通过哪个网卡 |
out | 出去通过哪个网卡 |
source | 源ip地址 |
destination | 目的ip地址 |
快速清除所有的iptables规则
命令:iptables -F
通过命令先清除所有的iptables规则限制
[root@centos ~]# iptables -F
[root@centos ~]# iptables -vnL
Chain INPUT (policy ACCEPT 2567 packets, 890K bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 15 packets, 1428 bytes)
pkts bytes target prot opt in out source destination
添加一条限制固定目的ip、端口的iptables规则
命令:iptables -A INPUT -d 192.168.1.1 -p tcp --dport 22 -j DROP
命令解释:-A INPUT意思为对input链添加规则;-d 192.168.1.1意思为对目的ip为192.168.1.1的所有ip进行添加的规则;-p tcp意思为对tcp数据包进行添加规则;–dport 22意思为对端口22进行添加规则;-j DROP意思为满足这个规则的使之drop掉,即丢弃。