正确认识 netfilter 和 iptables 的关系,有助于理解 Linux 防火墙的工作方式。
2. 规则链
3. 规则链内部各条防火墙规则之间的顺序
[root@Linux01 ~]# systemctl stop firewalld.service
[root@Linux01 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@Linux01 ~]#
[root@Linux01 ~]# yum -y install iptables iptables-services
[root@Linux01 ~]# systemctl start iptables.service
[root@Linux01 ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@Linux01 ~]#
iptables [-t 表名] 管理选项 [链名] [匹配条件] [-j 控制类型]
[root@Linux01 ~]# iptables -t filter -I INPUT -p icmp -j REJECT
1. 添加新的规则
[root@Linux01 ~]# iptables -t filter -A INPUT -p tcp -j ACCEPT
[root@Linux01 ~]# iptables -I INPUT -p udp -j ACCEPT
[root@Linux01 ~]# iptables -I INPUT 2 -p icmp -j ACCEPT
2. 查看规则表
[root@Linux01 ~]# iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT udp -- anywhere anywhere
2 ACCEPT icmp -- anywhere anywhere
3 REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
5 ACCEPT icmp -- anywhere anywhere
6 ACCEPT all -- anywhere anywhere
7 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
8 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
9 ACCEPT tcp -- anywhere anywhere
[root@Linux01 ~]#
[root@Linux01 ~]# iptables -n -L INPUT //"-n -L"可合写为"-nL"
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
[root@Linux01 ~]#
3. 删除、清空规则
[root@Linux01 ~]# iptables -D INPUT 3
[root@Linux01 ~]# iptables -n -L INPUT //确认删除效果
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
[root@Linux01 ~]#
[root@Linux01 ~]# iptables -F INPUT
[root@Linux01 ~]# iptables -n -L INPUT //确认删除效果
Chain INPUT (policy ACCEPT)
target prot opt source destination
[root@Linux01 ~]#
[root@Linux01 ~]# iptables -F
[root@Linux01 ~]# iptables -t nat -F
[root@Linux01 ~]# iptables -t mangle -F
4. 设置默认策略
[root@Linux01 ~]# iptables -t filter -P FORWARD DROP
[root@Linux01 ~]# iptables -P OUTPUT ACCEPT
[root@Linux01 ~]# iptables -I INPUT -p icmp -j DROP
[root@Linux01 ~]# iptables -A FORWARD ! -p icmp -j ACCEPT
(2)地址匹配
编写 iptables 规则时使用“-s 源地址”或“-d 目标地址”的形式指定,用来检查数据包的源 地址(--source)或目标地址(--destination)。IP 地址、网段地址等都是可以接受的,但不 建议使用主机名、域名地址(解析过程会影响效率)。例如,若要拒绝转发源地址为192.168.1.11 的数据,允许转发源地址位于 192.168.7.0/24 网段的数据,可以执行以下操作。
[root@Linux01 ~]# iptables -A FORWARD -s 192.168.1.11 -j REJECT
[root@Linux01 ~]# iptables -A FORWARD -s 192.168.7.0/24 -j ACCEPT
[root@Linux01 ~]# iptables -I INPUT -s 10.20.30.0/24 -j DROP
[root@Linux01 ~]# iptables -I FORWARD -s 10.20.30.0/24 -j DROP
(3)网络接口匹配
[root@Linux01 ~]# iptables -A INPUT -i ens33 -s 10.0.0.0/8 -j DROP
[root@Linux01 ~]# iptables -A INPUT -i ens33 -s 172.16.0.0/16 -j DROP
[root@Linux01 ~]# iptables -A INPUT -i ens33 -s 192.168.0.0/16 -j DROP
2. 隐含匹配
[root@Linux01 ~]# iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCEPT
[root@Linux01 ~]# iptables -A FORWARD -d 192.168.4.0/24 -p udp --dport 53 -j ACCEPT
[root@Linux01 ~]# iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
[root@Linux01 ~]# iptables -A INPUT -p tcp --dport 24500:24600 -j ACCEPT
(2)ICMP类型匹配
[root@Linux01 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
[root@Linux01 ~]# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
[root@Linux01 ~]# iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
[root@Linux01 ~]# iptables -A INPUT -p icmp -j DROP
[root@Linux01 ~]# iptables -p icmp -h
3. 显示匹配
[root@Linux01 ~]# iptables -A INPUT -p tcp -m multiport --dport 25,80,110,143 -j ACCEPT
(2)IP范围匹配
[root@Linux01 ~]# iptables -A FORWARD -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j DROP
(3)MAC地址匹配
[root@Linux01 ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:c0:55:3f -j DROP
(4)状态匹配
[root@Linux01 ~]# iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP
[root@Linux01 ~]# iptables -I INPUT -p tcp -m multiport --dport 80 -j ACCEPT
[root@Linux01 ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT
[root@Linux01 ~]# iptables -P INPUT DROP