iptables防火墙
一、防火墙
1.1 防火墙种类及使用说明
- 硬件:整个企业入口
- 三层路由:H3C 华为 Cisco(思科)
- 深信服
- ~~Junipcr~~
- 软件:开源软件 网站内部 封ip
- iptables 写入到Linux内核中 以后服务docker
- firewalld C7
- 云防火墙
- 阿里云:安全组
1.2 必须熟悉的名词
-
容器: 瓶子 罐子 存放东西
-
表(table): 存放链的容器
- 链(chain): 存放规则的容器
- 规则(policy): 准许或拒绝规则
| Netfilter | 表(tables) | 链(chains) | 规则(Policy) |
|---|---|---|---|
| 一栋楼 | 楼里的房子 | 房子里的柜子 | 柜子里衣服,摆放规则 |
1.3 iptables 执行过程





二、表与链※※
2.1 简介
- iptables 是 4表五链
- 4表:filter 表 nat表 raw表 mangle表
- 五链:INPUT OUTPUT FORWARD PREROUTING POSTROUTING
2.2 每个表说明
1)filter表
- 防火墙:屏蔽或准许 端口 ip
| filter表 | 强调:主要和主机自身相关,真正负责主机防火墙功能的(过滤流入流出主机的数据包) filter表示iptables默认使用的表,这个表定义了三个链(chains) 企业工作场景:主机防火墙 |
|---|---|
| INPUT | 负责过滤所有目标地址是本机地址的数据包 通俗来说:就是过滤进入主机的数据包 |
| FORWARD | 负责转发流经主机的数据包。起转发的作用,和NAT关系很大,后面会详细介绍 LVS NAT模式,net.ipv4.ip_forward=1 |
| OUTPUT | 处理所有源地址是本机地址的数据包 通俗的讲:就是处理从主机发出去的数据包 |
2)nat表
- 实现nat功能
- 实现共享上网(内网服务器上外网)
- 端口映射和ip映射
| nat | 负责网络地址转换的,即来源与目的IP地址和port的转换。 应用:和主机本身无关,一般用于局域网共享上网或者特殊的端口转换服务相关。 工作场景: 1. 用于企业路由(zebra)或网关(iptables),共享上网(POSTROUTING) 2. 做内部外部IP地址一对一映射(dmz),硬件防火墙映射IP到内部服务器,ftp服务(PREROUTING) 3. WEB,单个端口的映射,直接映射80端口(PREROUTING) 这个表定义了3个链,nat功能相当于网络的acl控制。和网络交换机acl类似。 |
|---|---|
| OUTPUT | 和主机放出去的数据包有关,改变主机发出数据包的目的地址。 |
| PREROUTING | 在数据包到达防火墙时,进行路由判断之前执行的规则,作用是改变数据包的目的地址、目的端口等 就是收信时,根据规则重写收件人的地址。 例如:把公网IP:xxx.xxx.xxx.xxx映射到局域网的xx.xx.xx.xx服务器上。 如果是web服务,可以报80转换为局域网的服务器9000端口上 10.0.0.61 8080(目标端口) —-nat—à 10.0.0.7 22 |
| POSTROUTING | 在数据包离开防火墙时进行路由判断之后执行的规则,作用改变数据包的源地址,源端口等。 写好发件人的地址,要让家人回信时能够有地址可回。 例如。默认笔记本和虚拟机都是局域网地址,在出网的时候被路由器将源地址改为了公网地址。 生产应用:局域网共享上网。 |
三、环境准备及命令
ansible 10.0.0.61 172.16.1.61
web01 10.0.0.7 172.16.1.7
iptables iptables启动或关闭的命令
`环境准备`
#停掉firewalld防火墙,firewalld和iptables同时只能启动一个
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# systemctl disable firewalld
[root@ansible ~]# uname -r # uname=显示系统信息 -r=release专门显示内核版本
4.19.90-52.22.v2207.ky10.x86_64
#安装
[root@ansible ~]# yum -y install iptables-services
/etc/sysconfig/iptables # 防火墙的配置文件
/usr/lib/systemd/system/iptables.service # 防火墙服务配置文件
[root@ansible ~]# rpm -ql iptables # rpm -ql = list(列出**所有文件**)
[root@ansible ~]# rpm -qc iptables # rpm -qc = config(只列出**配置文件**)
(命令)
#防火墙相关模块 加载到内核中
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
#永久
cat >>/etc/rc.local<<EOF
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
EOF
[root@ansible ~]# lsmod|egrep 'filter|nat|ipt'
ip6table_nat 16384 1
ip6table_filter 16384 1
ip6_tables 32768 2 ip6table_filter,ip6table_nat
xt_nat 16384 49
iptable_nat 16384 12
nf_nat 45056 4 ip6table_nat,xt_nat,iptable_nat,xt_MASQUERADE
nf_conntrack 159744 6 xt_conntrack,nf_nat,xt_state,xt_nat,nf_conntrack_netlink,xt_MASQUERADE
iptable_filter 16384 1
iptable_raw 16384 1
ip_tables 28672 3 iptable_filter,iptable_raw,iptable_nat
libcrc32c 16384 3 nf_conntrack,nf_nat,xfs
`启动`
[root@ansible ~]# systemctl start iptables.service
[root@ansible ~]# systemctl enable iptables.service
`查看规则`
[root@ansible ~]# iptables -nL

四、iptables命令参数
| 参数 | 含义 |
|---|---|
| –L | 显示表中的所有规则 |
| –n | 不要把端口 或ip反向解析为 名字 |
| –t | 指定表 不指定默认是filter表 |
| -A | append 追加 加入准许类规则 使用-A |
| -D | delete 删除 -D INPUT 1 |
| -l | insert 拒绝类规则放在所有规则最上面 拒绝类 -I |
| line-numbers | 显示行号 |
| 参数 | 含义 |
|---|---|
| -p | 协议 protocal tcp/udp/icmp/all |
| –dport | 目标端口 dest destination 指定端口 加上协议 -p tcp |
| –sport | 源端口 source 源 |
| -s | –source 源ip |
| -d | –destination 目标ip |
| -m | 指定模块 multiport |
| -i | input 输入的时候 从哪个网卡进来 |
| -o | ouput 输出的时候 从哪个网卡出去 |
| 参数 | 含义 |
|---|---|
| -j | 满足条件后的动作 : DROP/ACCEPT/REJECT |
| DROP REJECT拒绝 DROP把数据丢掉 不会返回信息给用户 REJECT 拒绝 返回拒绝信息 |
| 参数 | 含义 |
|---|---|
| -F flush | 清除所有规则,不会处理默认的规则 |
| -X | 删除用户自定义的链 |
| -Z | 链的计数器清零(数据包计数器与数据包字节计数器) |
五、配置filter表规则※※
- 正式配置之前 先备份, 清空规则
[root@ansible ~]# iptables -F
[root@ansible ~]# iptables -X
[root@ansible ~]# iptables -Z
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
案例1:禁止访问22端口(–dport)
#禁止22端口访问
[root@ansible ~]# iptables -t filter -I INPUT -p tcp --dport 22 -j DROP
iptables -t filter -D INPUT -p tcp --dport 22 -j DROP
#重新链接xshell
Connecting to 10.0.0.61:22...
Could not connect to '10.0.0.61' (port 22): Connection failed.
#回虚拟机
删除规则
iptables -D INPUT -p tcp --dport 22 -j DROP
删除规则
添加
[root@ansible ~]# iptables -t filter -I INPUT -p tcp --dport 80 -j DROP
查看序号
[root@ansible ~]# iptables -nL --line
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
方式1:根据序号删除规则
[root@ansible ~]# iptables -D INPUT 1
方式2:
添加
[root@ansible ~]# iptables -t filter -I INPUT -p tcp --dport 80 -j DROP
删除
[root@ansible ~]# iptables -t filter -D INPUT -p tcp --dport 80 -j DROP
案例2:封ip 屏蔽某个ip(-s)
#屏蔽10.0.0.7和172.16.1.7
[root@ansible ~]# iptables -I INPUT -s 10.0.0.7 -j DROP
[root@ansible ~]# iptables -I INPUT -s 172.16.1.7 -j DROP
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 172.16.1.7 0.0.0.0/0
DROP all -- 10.0.0.7 0.0.0.0/0
#测试
[root@web01 ~]# ssh 10.0.0.61
[root@web01 ~]# ssh 172.16.1.61
案例3:禁止网段连入(禁止10.0.0.0/24网段访问 8888端口)
nc/ncat/netcat
nc -l
-l 使用监听模式,管控传入的资料
-v 显示指令执行过程。
-k 接受多个连接请求
telnet
#禁止10.0.0.0/24网段访问 8888端口
[root@ansible ~]# iptables -I INPUT -s 10.0.0.0/24 -p tcp --dport 8888 -j DROP
#测试
[root@web01 ~]# ssh 10.0.0.61 hostname
root@10.0.0.61's password:
ansible
1.ansible监听
[root@ansible ~]# nc -kl 8888
2.客户端连接
[c:\~]$ telnet 10.0.0.61 8888
Connecting to 10.0.0.61:8888...
Could not connect to '10.0.0.61' (port 8888): Connection failed.
3.清空规则再次连接
[root@ansible ~]# iptables -F
连接成功
[c:\~]$ telnet 10.0.0.61 8888
Connecting to 10.0.0.61:8888...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Ansible
监听成功
[root@ansible ~]# nc -kl 8888
Ansible
案例4:只允许指定网段连入(允许10.0.0.0网段)
实现阿里云白名单功能:默认是拒绝 开发端口 网段
allow 10.0.0.0/24;
deny all;
#方法1:利用! 进行排除
#只允许10.0.0.0/24访问 言外之意 除了10.0.0.0/24 都拒绝
[root@ansible ~]# iptables -I INPUT ! -s 10.0.0.0/24 -j DROP
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- !10.0.0.0/24 0.0.0.0/0
测试:web01用172段连接
[root@web01 ~]# ping 172.16.1.61
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
^C
--- 172.16.1.61 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1033ms
[root@web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.589 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.234 ms
#方法2:修改链默认规则 修改为拒绝 添加准许
默认准许
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
先配置好规则 准许规则
[root@ansible ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 10.0.0.0/24 0.0.0.0/0
修改默认规则
[root@ansible ~]# iptables -P INPUT DROP
[root@ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- 10.0.0.0/24 0.0.0.0/0
测试
[root@web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.426 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.193 ms
64 bytes from 10.0.0.61: icmp_seq=3 ttl=64 time=1.85 ms
^C
--- 10.0.0.61 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev = 0.193/0.822/1.849/0.731 ms
[root@web01 ~]# ping 172.16.1.61
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
测试完修改会去
[root@ansible ~]# iptables -P INPUT ACCEPT
[root@web01 ~]# ping 172.16.1.61
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
64 bytes from 172.16.1.61: icmp_seq=1 ttl=64 time=0.653 ms
64 bytes from 172.16.1.61: icmp_seq=2 ttl=64 time=2.26 ms
案例5:指定多个端口
#方式1:
[root@ansible ~]# iptables -I INPUT -p tcp --dport 8888 -j DROP
[root@ansible ~]# iptables -I INPUT -p tcp --dport 9999 -j DROP
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9999
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8888
#方式2:
[root@ansible ~]# iptables -I INPUT -p tcp -m multiport ! --dport 80,443 -j DROP
#方式3:如果是 连续的端口 可以不加上-m multiport 1:1024
[root@ansible ~]# iptables -I INPUT -p tcp --dport 1024:65535 -j DROP
案例6:匹配ICMP类型(禁ping)
- ICMP(Internet Control Message Protocol)Internet控制报文协议 ping
- 整个网站核
通过防火墙规则 控制是否可以ping
[root@ansible ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8
测试
[root@web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
^C
--- 10.0.0.61 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3064ms
[root@web01 ~]# ssh 10.0.0.61
The authenticity of host '10.0.0.61 (10.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:39F/4PC3pB6s4nQPINGX7gWuTyxxHga9tpaQP35De3s.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
通过内核参数 控制 禁止被ping
#方式:1
vim /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_all=1
sysctl -p
#方式2
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
sysctl -p
测试:
[root@ansible ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
^C
--- 10.0.0.61 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2074ms
[root@ansible ~]# ssh 10.0.0.61
Authorized users only. All activities may be monitored and reported.
root@10.0.0.61's password:
关闭
echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all
sysctl -p
测试
[root@ansible ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.034 ms
案例7:匹配网络状态(TCP/IP连接状态)
-m state –state
NEW:已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动的新连接
INVALID:非法或无法识别的
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
案例8:限制并发及速率
-m limit 限制模块
-m limit --limit 10/minute #每分钟只能有10个数据包 每6秒生成
-m limit –limit n/{second/minute/hour}:
解释:指定时间内的请求速率”n”为速率,后面为时间分别为:秒 分 时
-m limit --limit 10/minute --limit-burst 5 每6秒释放工牌 给别人使用
#10个数据包
前5个 1个1个工牌 从第6个开始 每6秒 才能释放1个工牌
–limit-burst [n]
解释:在同一时间内允许通过的请求”n”为数字,不指定默认为5
- 测试 演示
#ping icmp 协议 进行测试
iptables -F
iptables -I INPUT -p icmp -m limit --limit 10/minute --limit-burst 5 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
- web01 进行ping测试
ping 10.0.0.61
[root@web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.240 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.376 ms
64 bytes from 10.0.0.61: icmp_seq=3 ttl=64 time=0.474 ms
64 bytes from 10.0.0.61: icmp_seq=4 ttl=64 time=0.693 ms
64 bytes from 10.0.0.61: icmp_seq=5 ttl=64 time=2.43 ms
64 bytes from 10.0.0.61: icmp_seq=7 ttl=64 time=0.351 ms #7与1比 就是间隔6秒
64 bytes from 10.0.0.61: icmp_seq=13 ttl=64 time=0.869 ms #7 13 间隔6秒
64 bytes from 10.0.0.61: icmp_seq=19 ttl=64 time=0.482 ms
64 bytes from 10.0.0.61: icmp_seq=25 ttl=64 time=0.378 ms
64 bytes from 10.0.0.61: icmp_seq=31 ttl=64 time=0.329 ms
64 bytes from 10.0.0.61: icmp_seq=37 ttl=64 time=0.857 ms
64 bytes from 10.0.0.61: icmp_seq=43 ttl=64 time=0.314 ms
64 bytes from 10.0.0.61: icmp_seq=49 ttl=64 time=0.360 ms
64 bytes from 10.0.0.61: icmp_seq=55 ttl=64 time=0.349 ms
64 bytes from 10.0.0.61: icmp_seq=61 ttl=64 time=0.552 ms
64 bytes from 10.0.0.61: icmp_seq=67 ttl=64 time=0.283 ms
64 bytes from 10.0.0.61: icmp_seq=73 ttl=64 time=0.407 ms
64 bytes from 10.0.0.61: icmp_seq=79 ttl=64 time=0.297 ms
64 bytes from 10.0.0.61: icmp_seq=85 ttl=64 time=0.428 ms
64 bytes from 10.0.0.61: icmp_seq=91 ttl=64 time=0.390 ms
64 bytes from 10.0.0.61: icmp_seq=97 ttl=64 time=0.691 ms
64 bytes from 10.0.0.61: icmp_seq=103 ttl=64 time=0.537 ms
64 bytes from 10.0.0.61: icmp_seq=109 ttl=64 time=0.546 ms
64 bytes from 10.0.0.61: icmp_seq=115 ttl=64 time=0.382 ms
64 bytes from 10.0.0.61: icmp_seq=121 ttl=64 time=0.337 ms
案例9:防火墙 规则的保存与恢复
- iptables-save 默认输出到屏幕
- iptables-restore 加上文件
- 写入到/etc/sysconfig/iptables
`保存配置`
[root@ansible ~]# iptables-save >/etc/sysconfig/iptables
[root@ansible ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.8.5 on Sun May 24 04:13:20 2026
*nat
:PREROUTING ACCEPT [4:240]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [1:76]
:POSTROUTING ACCEPT [1:76]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 192.168.250.0/24 ! -o br-774e222e3f99 -j MASQUERADE
[root@ansible ~]# iptables -nL
[root@ansible ~]# iptables -D 1
iptables: Bad rule (does a matching rule exist in that chain?).
`删除规则`
[root@ansible ~]# iptables -D INPUT 1
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:1194
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:2222
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
`导入规则`
[root@ansible ~]#iptables-restore </etc/sysconfig/iptables
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:1194
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:1194
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:2222
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
`补充`
systemctl restart iptables #读取 /etc/sysconfig/iptables内容
10:filter小结
- 封ip 端口 网段
- 禁止ping
- 限制速度和并发
- iptables filter表功能 可以在 云服务器使用
六、实际生产用法
- iptables 配置方式
- 逛公园模式:默认规则是 ACCEPT
- 看电影模式:默认规则是DROP
- 默认是拒绝 去电影院
1.ssh可以连接进来
[root@ansible ~]# iptables -F
[root@ansible ~]# iptables -X
[root@ansible ~]# iptables -Z
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@ansible ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.设置允许本机lo通讯规则
#允许本机回环lo接口数据流量流出与流入
[root@ansible ~]# iptables -A INPUT -i lo -j ACCEPT
[root@ansible ~]# iptables -A OUTPUT -o lo -j ACCEPT
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
3.配置默认规则及放行80 443端口三
[root@ansible ~]# iptables -P INPUT DROP #看电影模式
[root@ansible ~]# iptables -P FORWARD DROP #看电影模式
[root@ansible ~]# iptables -P OUTPUT ACCEPT #逛公园模式
[root@ansible ~]# iptables -A INPUT -m multiport -p tcp --dport 443,80 -j ACCEPT
[root@ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 443,80
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
`网段限制`
[root@ansible ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT
[root@ansible ~]# iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT
# 此处还可以添加vpn网段 比如说10.7.1.0/24
[root@ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 443,80
ACCEPT all -- 10.0.0.0/24 0.0.0.0/0
ACCEPT all -- 172.16.1.0/24 0.0.0.0/0
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
`保存配置`
iptables-save #查看当前所有规则(不保存,只打印)
[root@ansible ~]# iptables-save
# Generated by iptables-save v1.8.5 on Sun May 24 04:46:21 2026
*nat
:PREROUTING ACCEPT [144:8978]
:INPUT ACCEPT [2:458]
:OUTPUT ACCEPT [38:2888]
:POSTROUTING ACCEPT [38:2888]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 192.168.250.0/24 ! -o br-774e222e3f99 -j MASQUERADE
...
- 汇总
[root@ansible ~]# iptables-save
# Generated by iptables-save v1.8.5 on Sun May 24 04:48:22 2026
*nat
:PREROUTING ACCEPT [167:10358]
:INPUT ACCEPT [2:458]
:OUTPUT ACCEPT [40:3040]
:POSTROUTING ACCEPT [40:3040]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 192.168.250.0/24 ! -o br-774e222e3f99 -j MASQUERADE
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.18.0.0/16 ! -o br-f404779f9695 -j MASQUERADE
-A POSTROUTING -s 10.8.0.0/24 -o ens33 -j MASQUERADE
-A POSTROUTING -s 10.8.0.0/24 -o ens36 -j MASQUERADE
-A DOCKER -i br-774e222e3f99 -j RETURN
-A DOCKER -i docker0 -j RETURN
-A DOCKER -i br-f404779f9695 -j RETURN
-A DOCKER ! -i br-f404779f9695 -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.18.0.3:8080
-A DOCKER ! -i br-f404779f9695 -p tcp -m tcp --dport 10051 -j DNAT --to-destination 172.18.0.2:10051
-A DOCKER ! -i br-f404779f9695 -p tcp -m tcp --dport 10050 -j DNAT --to-destination 172.18.0.4:10050
-A DOCKER ! -i br-774e222e3f99 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.250.6:80
-A DOCKER ! -i br-774e222e3f99 -p tcp -m tcp --dport 2222 -j DNAT --to-destination 192.168.250.7:2222
COMMIT
# Completed on Sun May 24 04:48:22 2026
# Generated by iptables-save v1.8.5 on Sun May 24 04:48:22 2026
*filter
:INPUT DROP [53:5907]
:FORWARD DROP [71:4260]
:OUTPUT ACCEPT [156:16604]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m multiport --dports 443,80 -j ACCEPT
-A INPUT -s 10.0.0.0/24 -j ACCEPT
-A INPUT -s 172.16.1.0/24 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
# Completed on Sun May 24 04:48:22 2026
# Generated by iptables-save v1.8.5 on Sun May 24 04:48:22 2026
*raw
:PREROUTING ACCEPT [59277:17230150]
:OUTPUT ACCEPT [54626:56993914]
-A PREROUTING -d 172.18.0.2/32 ! -i br-f404779f9695 -j DROP
-A PREROUTING -d 172.18.0.3/32 ! -i br-f404779f9695 -j DROP
-A PREROUTING -d 172.18.0.4/32 ! -i br-f404779f9695 -j DROP
-A PREROUTING -d 192.168.250.2/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.3/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.5/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.6/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.7/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.8/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.9/32 ! -i br-774e222e3f99 -j DROP
-A PREROUTING -d 192.168.250.4/32 ! -i br-774e222e3f99 -j DROP
COMMIT
# Completed on Sun May 24 04:48:22 2026
七、nat
- 共享上网
- 端口转发/端口映射
- ip映射
[root@ansible ~]# iptables -P INPUT ACCEPT
[root@ansible ~]# iptables -P FORWARD ACCEPT
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 443,80
ACCEPT all -- 10.0.0.0/24 0.0.0.0/0
ACCEPT all -- 172.16.1.0/24 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
#清空规则
[root@ansible ~]# iptables -F
[root@ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
案例:实现共享上网

1.防火墙配置
`服务端`
1.配置SNAT(源地址转换)
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 10.0.0.61
2.开启内核转发
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
3.立即生效
[root@ansible ~]# sysctl -p
net.ipv4.ip_forward = 1
注意事项:
公网ip不固定: iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE
2.web配置
1.关掉ens33网卡
[root@web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=no #yes改为no
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
2.给ens36配置网关172.16.1.61
[root@web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens36
TYPE=Ethernet
BOOTPROTO=none
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=172.16.1.7
PREFIX=24
GATEWAY=172.16.1.61 #配置网关
DNS1=223.5.5.5 #配置DNS
3.重启网卡
[root@web01 ~]# systemctl restart network
[root@web02 ~]# ssh 172.16.1.7
[root@web01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:a0:dc:be brd ff:ff:ff:ff:ff:ff
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:a0:dc:c8 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.7/24 brd 172.16.1.255 scope global noprefixroute ens36
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fea0:dcc8/64 scope link
valid_lft forever preferred_lft forever
4.测试
[root@web01 ~]# ping -c3 -W1 www.baidu.com
PING www.a.shifen.com (110.242.69.21) 56(84) bytes of data.
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=1 ttl=127 time=18.0 ms
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=2 ttl=127 time=18.8 ms
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=3 ttl=127 time=13.3 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 13.320/16.708/18.796/2.417 ms
[root@web01 ~]# ping -c3 -W1 1.2.4.8
PING 1.2.4.8 (1.2.4.8) 56(84) bytes of data.
64 bytes from 1.2.4.8: icmp_seq=1 ttl=127 time=11.6 ms
64 bytes from 1.2.4.8: icmp_seq=2 ttl=127 time=10.5 ms
64 bytes from 1.2.4.8: icmp_seq=3 ttl=127 time=8.89 ms
--- 1.2.4.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 8.886/10.324/11.555/1.099 ms
3.完成后 在web01 发出 ip r和ping 外网ip的结果
[root@web01 ~]# ip r
default via 172.16.1.61 dev ens36 proto static metric 100
172.16.1.0/24 dev ens36 proto kernel scope link src 172.16.1.7 metric 100
[root@web01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.1.61 0.0.0.0 UG 100 0 0 ens36
172.16.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens36
[root@web01 ~]# ping -c3 -W1 www.baidu.com
PING www.a.shifen.com (110.242.69.21) 56(84) bytes of data.
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=1 ttl=127 time=18.5 ms
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=2 ttl=127 time=18.3 ms
64 bytes from 110.242.69.21 (110.242.69.21): icmp_seq=3 ttl=127 time=13.4 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 13.445/16.736/18.495/2.329 ms
案例:实现端口转发

1.配置
[root@ansible ~]# iptables -t nat -A PREROUTING -d 10.0.0.61 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.7:22
[root@ansible ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
DNAT tcp -- 0.0.0.0/0 10.0.0.61 tcp dpt:9000 to:172.16.1.7:22
2.测试与检查
本地shell中
[c:\~]$ ssh root@10.0.0.61 9000
Connecting to 10.0.0.61:9000...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Authorized users only. All activities may be monitored and reported.
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Mon May 25 20:48:30 2026 from 172.16.1.8
[root@web01 ~]#
案例:实现ip映射
[root@ansible ~]# ip a add 10.0.0.62/24 dev ens33 label ens33:0
[root@ansible ~]# ip a |grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 10.0.0.61/24 brd 10.0.0.255 scope global noprefixroute ens33
inet 10.0.0.62/24 scope global secondary ens33:0
[root@ansible ~]# iptables -t nat -A PREROUTING -d 10.0.0.62 -j DNAT --to-destination 172.16.1.7
[root@ansible ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
DNAT tcp -- 0.0.0.0/0 10.0.0.61 tcp dpt:9000 to:172.16.1.7:22
DNAT all -- 0.0.0.0/0 10.0.0.62 to:172.16.1.7
测试
[c:\~]$ ssh 10.0.0.62
Connecting to 10.0.0.62:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Authorized users only. All activities may be monitored and reported.
Authorized users only. All activities may be monitored and reported.
Activate the web console with: systemctl enable --now cockpit.socket
最后一次失败的登录: 一 5月 25 21:08:43 CST 2026 从 10.0.0.1 ssh:notty 上
最后一次成功登录后有 1 次失败的登录尝试。
Last login: Mon May 25 21:01:38 2026 from 10.0.0.1
nat表总结
- 实现共享上网
- 端口转发
- nat功能在云服务器无法使用 替代品叫:NAT网关
八、总结
- 练习题:
【面试题】老男孩教育防火墙企业面试题iptalbes
https://www.jianshu.com/p/19422676b854
1.请写出查看iptables当前所有规则的命令。
iptables-save
iptables -nL
iptables -nL -t nat
2.禁止来自10.0.0.188 ip地址访问80端口的请求
iptables -I INPUT -s 10.0.0.188 -p tcp --dport 80 -j DROP
3.如何使在命令行执行的iptables规则永久生效?
iptables-save
/etc/sysconfig/iptables
4.实现把访问10.0.0.3:80的请求转到172.16.1.17:80
iptables -t nat -A PREROUTING -d 10.0.0.3 -p tcp --dport 80 -j DNAT --to-destination 172.16.xxxx:80
5.实现172.16.1.0/24段所有主机通过124.32.54.26外网IP共享上网。
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 123.32.54.26
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT MASQUERADE
6.案例 iptalbes 实现防止syn ddos 和ping攻击
-A FORWARD -p tcp --syn -m limit --limit 1/s --limit-burst 5 -j ACCEPT
-A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
-A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
说明:第一行:每秒中最多允许5个新连接。第二行:防止各种端口扫描。第三行:Ping洪水攻击(Ping of Death),可以根据需要调整或关闭
防火墙 笔试题
https://www.jianshu.com/p/2180face8381
正文完