0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Linux系统中iptables与firewalld防火墙的区别

马哥Linux运维 ? 来源:马哥Linux运维 ? 2025-07-14 11:24 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

iptables与firewalld防火墙配置完全指南

1. 防火墙基础概念

1.1 什么是防火墙

防火墙是一种网络安全设备,用于监控和控制网络流量,根据预定义的安全规则来允许或阻止数据包通过。Linux系统中主要有两种防火墙解决方案:iptables和firewalld。

1.2 iptables vs firewalld

?iptables:传统的Linux防火墙工具,直接操作内核的netfilter框架

?firewalld:动态防火墙管理器,提供更高级的抽象和动态配置能力

2. iptables详解

2.1 iptables基本概念

2.1.1 表(Tables)

?filter表:默认表,用于过滤数据包

?nat表:用于网络地址转换

?mangle表:用于修改数据包头部信息

?raw表:用于配置连接跟踪

2.1.2 链(Chains)

?INPUT:处理入站数据包

?OUTPUT:处理出站数据包

?FORWARD:处理转发数据包

?PREROUTING:在路由决策前处理数据包

?POSTROUTING:在路由决策后处理数据包

2.1.3 目标(Targets)

?ACCEPT:接受数据包

?DROP:丢弃数据包

?REJECT:拒绝数据包并返回错误信息

?LOG:记录日志

?MASQUERADE:IP伪装

2.2 iptables基本语法

iptables [-t table] -[ADI] chain rule-specification
iptables [-t table] -[FLZ] [chain]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target

2.3 iptables常用命令

2.3.1 查看规则

# 查看所有规则
iptables -L -n -v

# 查看特定表的规则
iptables -t nat -L -n -v

# 查看规则编号
iptables -L INPUT --line-numbers

2.3.2 添加规则

# 允许SSH连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许特定IP访问
iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

2.3.3 删除规则

# 删除特定规则
iptables -D INPUT -p tcp --dport 80 -j ACCEPT

# 按行号删除
iptables -D INPUT 3

# 清空所有规则
iptables -F
iptables -X
iptables -Z

2.3.4 设置默认策略

# 设置默认拒绝策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

2.4 iptables高级配置

2.4.1 端口范围和多端口

# 端口范围
iptables -A INPUT -p tcp --dport 3000:3010 -j ACCEPT

# 多端口
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

2.4.2 时间限制

# 只在工作时间允许访问
iptables -A INPUT -p tcp --dport 22 -mtime--timestart 09:00 --timestop 18:00 -j ACCEPT

2.4.3 连接限制

# 限制并发连接数
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT

# 限制连接速率
iptables -A INPUT -p tcp --dport 22 -mlimit--limit5/min --limit-burst 10 -j ACCEPT

2.4.4 NAT配置

# SNAT(源地址转换)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

# DNAT(目标地址转换)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:8080

2.5 iptables规则持久化

2.5.1 CentOS/RHEL系统

# 保存规则
service iptables save

# 或者手动保存
iptables-save > /etc/sysconfig/iptables

# 恢复规则
iptables-restore < /etc/sysconfig/iptables

2.5.2 Ubuntu/Debian系统

# 安装iptables-persistent
apt-get install iptables-persistent

# 保存规则
netfilter-persistent save

# 恢复规则
netfilter-persistent reload

3. firewalld详解

3.1 firewalld基本概念

3.1.1 区域(Zones)

?drop:丢弃所有传入连接

?block:拒绝所有传入连接

?public:公共区域,默认区域

?external:外部区域,用于NAT

?dmz:DMZ区域

?work:工作区域

?home:家庭区域

?internal:内部区域

?trusted:信任区域,允许所有连接

3.1.2 服务(Services)

预定义的服务配置,包含端口、协议等信息。

3.1.3 富规则(Rich Rules)

提供更复杂的规则配置语法。

3.2 firewalld基本命令

3.2.1 服务管理

# 启动firewalld
systemctl start firewalld

# 停止firewalld
systemctl stop firewalld

# 重启firewalld
systemctl restart firewalld

# 查看状态
systemctl status firewalld
firewall-cmd --state

3.2.2 区域管理

# 查看默认区域
firewall-cmd --get-default-zone

# 设置默认区域
firewall-cmd --set-default-zone=public

# 查看活动区域
firewall-cmd --get-active-zones

# 查看所有区域
firewall-cmd --get-zones

# 查看区域信息
firewall-cmd --zone=public --list-all

3.2.3 服务管理

# 查看可用服务
firewall-cmd --get-services

# 查看已开放的服务
firewall-cmd --list-services

# 添加服务
firewall-cmd --add-service=http
firewall-cmd --add-service=https

# 删除服务
firewall-cmd --remove-service=http

# 永久添加服务
firewall-cmd --permanent --add-service=http

3.2.4 端口管理

# 添加端口
firewall-cmd --add-port=8080/tcp

# 删除端口
firewall-cmd --remove-port=8080/tcp

# 查看开放端口
firewall-cmd --list-ports

# 永久添加端口
firewall-cmd --permanent --add-port=8080/tcp

3.3 firewalld高级配置

3.3.1 自定义服务

# 创建自定义服务配置文件
cat> /etc/firewalld/services/myapp.xml <

 MyApp
 My Application Service
 
 

EOF

# 重新加载配置
firewall-cmd --reload

# 添加自定义服务
firewall-cmd --add-service=myapp

3.3.2 富规则配置

# 允许特定IP访问特定端口
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'

# 限制连接速率
firewall-cmd --add-rich-rule='rule service name="ssh" limit value="10/m" accept'

# 阻止特定IP
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.200" drop'

# 端口转发
firewall-cmd --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'

3.3.3 接口绑定

# 将接口绑定到区域
firewall-cmd --zone=internal --add-interface=eth1

# 查看接口绑定
firewall-cmd --get-zone-of-interface=eth1

# 更改接口区域
firewall-cmd --zone=public --change-interface=eth1

3.3.4 IP伪装和端口转发

# 启用IP伪装
firewall-cmd --add-masquerade

# 端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100

# 查看转发规则
firewall-cmd --list-forward-ports

4. 实战配置案例

4.1 Web服务器防火墙配置

4.1.1 iptables配置

#!/bin/bash
# Web服务器防火墙配置脚本

# 清空现有规则
iptables -F
iptables -X
iptables -Z

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许SSH(限制连接数)
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT

# 允许DNS
iptables -A INPUT -p udp --dport 53 -j ACCEPT

# 保存规则
service iptables save

4.1.2 firewalld配置

#!/bin/bash
# Web服务器防火墙配置脚本

# 设置默认区域
firewall-cmd --set-default-zone=public

# 添加服务
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=ftp

# 限制SSH连接
firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="3/m" accept'

# 重新加载配置
firewall-cmd --reload

4.2 数据库服务器防火墙配置

4.2.1 iptables配置

#!/bin/bash
# 数据库服务器防火墙配置

# 清空现有规则
iptables -F
iptables -X
iptables -Z

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许SSH(仅限管理网段)
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

# 允许MySQL(仅限应用服务器)
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -s 192.168.1.101 -p tcp --dport 3306 -j ACCEPT

# 保存规则
service iptables save

4.2.2 firewalld配置

#!/bin/bash
# 数据库服务器防火墙配置

# 创建数据库区域
firewall-cmd --permanent --new-zone=database

# 设置默认区域
firewall-cmd --set-default-zone=database

# 添加SSH服务(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'

# 添加MySQL服务(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="mysql" accept'
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.101" service name="mysql" accept'

# 重新加载配置
firewall-cmd --reload

5. 故障排查和优化

5.1 常见问题排查

5.1.1 规则不生效

# 检查规则是否正确添加
iptables -L -n -v
firewall-cmd --list-all

# 检查服务状态
systemctl status iptables
systemctl status firewalld

# 检查日志
tail-f /var/log/messages
journalctl -u firewalld -f

5.1.2 连接被拒绝

# 启用日志记录
iptables -A INPUT -j LOG --log-prefix"INPUT DROP: "

# 查看日志
tail-f /var/log/messages

# firewalld日志
firewall-cmd --set-log-denied=all

5.2 性能优化

5.2.1 规则优化

# 将常用规则放在前面
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

# 使用状态匹配减少规则数量
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 使用multiport匹配多个端口
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

5.2.2 监控和统计

# 查看规则统计
iptables -L -n -v --line-numbers

# 重置计数器
iptables -Z

# 实时监控
watch -n 1'iptables -L -n -v'

6. 安全最佳实践

6.1 基本安全原则

1.最小权限原则:只开放必要的端口和服务

2.白名单策略:默认拒绝所有连接,只允许必要的连接

3.定期审计:定期检查和更新防火墙规则

4.日志监控:启用日志记录并定期分析

6.2 配置建议

# 设置合理的默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 启用连接跟踪
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 限制连接速率
iptables -A INPUT -p tcp --dport 22 -mlimit--limit5/min --limit-burst 10 -j ACCEPT

# 防止扫描
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

6.3 备份和恢复

# 备份iptables规则
iptables-save > /root/iptables.backup.$(date+%Y%m%d)

# 恢复iptables规则
iptables-restore < /root/iptables.backup.20231201

# 备份firewalld配置
cp?-r /etc/firewalld /root/firewalld.backup.$(date?+%Y%m%d)

7. 总结

iptables和firewalld都是Linux系统中强大的防火墙工具。iptables提供了更底层的控制能力,适合对防火墙规则有详细要求的场景;firewalld则提供了更友好的管理界面和动态配置能力,更适合日常运维管理。

选择使用哪种工具主要取决于具体的应用场景和个人偏好。在实际部署中,建议根据系统的具体需求制定合适的防火墙策略,并定期进行安全审计和规则优化。

记住防火墙只是网络安全的一个组成部分,还需要结合其他安全措施(如入侵检测、日志监控、定期更新等)来构建完整的安全防护体系。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Linux
    +关注

    关注

    87

    文章

    11522

    浏览量

    214219
  • 防火墙
    +关注

    关注

    0

    文章

    436

    浏览量

    36236
  • 网络安全
    +关注

    关注

    11

    文章

    3353

    浏览量

    61593

原文标题:Linux防火墙终极对决:iptables与firewalld完整配置教程

文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    Linux系统iptables和firewall防火墙的配置方法

    防火墙就是根据系统管理员设定的规则来控制数据包的进出,主要是保护内网的安全,目前 Linux 系统防火墙类型主要有两种:分别是 [
    发表于 07-17 10:34 ?1691次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>系统</b><b class='flag-5'>iptables</b>和firewall<b class='flag-5'>防火墙</b>的配置方法

    Linux中使用Iptables实现简单的网站防火墙

    Linux中使用Iptables实现一个简单的网站防火墙可以帮助我们保护服务器不受到恶意请求的攻击。一个现实的场景是我们想要阻止某个IP地址访问我们的网站。
    发表于 09-22 10:11 ?1073次阅读
    <b class='flag-5'>Linux</b>中使用<b class='flag-5'>Iptables</b>实现简单的网站<b class='flag-5'>防火墙</b>

    Linux系统firewalld防火墙实战指南

    本文浩道跟大家分享Linux系统firewalld防火墙硬核干货,通过对其介绍、相关概念回顾,数据包处理流程、其安装过程、其常用命令用法、其添加规则相关用法去开展,让大家在****
    发表于 10-16 16:47 ?1339次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>系统</b><b class='flag-5'>firewalld</b><b class='flag-5'>防火墙</b>实战指南

    Linux内核防火墙netfilter的原理和应用

    操作系统得到了迅速发展,而Linux内核中所内嵌的防火墙起着重要作用。在Linux1.1版中使用的防火墙是从BSD UNIX
    发表于 09-19 09:22

    Linux防火墙的配置方法(firewalld服务)

    红帽RHEL7系统已经用firewalld服务替代了iptables服务,新的防火墙管理命令firewall-cmd与图形化工具firewall-config。执行firewall-c
    发表于 10-30 12:27

    Linux下关闭防火墙的关闭及开放端口

    :service iptables stop3.查看防火墙状态serviceiptablesstatus重启服务service iptables restart实现配置文件及时生效source文件路径
    发表于 07-09 06:23

    Linux如何关闭防火墙

    1、查看防火墙规则vim /etc/sysconfig/iptables放开某个端口号不被防火墙拦截,适用于部署tomcat,nginx等之类的软件
    发表于 07-12 06:31

    请问如何自动加载iptables防火墙规则?

    系统启动后,如何自动加载iptables防火墙规则?
    发表于 01-07 08:46

    基于Linux防火墙的可视化管理系统的研究与实现

    针对Iptables 命令配置防火墙规则的复杂性,本系统研究和实现了基于linux 防火墙的可视化管理
    发表于 07-16 08:47 ?16次下载

    Linux新型内核防火墙研究和应用

    Linux新型防火墙netfilter框架原理及工作机制基础上,研究了该防火墙的应用设计, 提出了用netfilter/iptables构建门户服务器
    发表于 08-25 11:35 ?14次下载

    Linux防火墙配置(iptablesfirewalld)

    防火墙就是根据系统管理员设定的规则来控制数据包的进出,主要是保护内网的安全,目前 Linux 系统防火墙类型主要有两种:分别是 [
    的头像 发表于 03-31 10:09 ?1493次阅读

    请问Centos7如何配置firewalld防火墙规则?

    Firewalld是CentOS系统自带的一种动态防火墙管理工具。是一个前端工具,用于管理Linux系统上的netfilter
    的头像 发表于 10-09 09:33 ?1959次阅读
    请问Centos7如何配置<b class='flag-5'>firewalld</b><b class='flag-5'>防火墙</b>规则?

    Linux软件防火墙iptables详解

    Linux提供的软件防火墙,名为iptables,它可以理解为是一个客户端代理,通过iptables的代理,将用户配置的安全策略执行到对应的安全框架
    的头像 发表于 03-01 14:50 ?678次阅读
    <b class='flag-5'>Linux</b>软件<b class='flag-5'>防火墙</b><b class='flag-5'>iptables</b>详解

    如何在CentOS系统配置防火墙

    作为一名系统管理员或开发者,你是否曾经被 Linux 防火墙配置搞得头大?在生产环境,我们经常需要配置防火墙来保护服务器安全,但面对
    的头像 发表于 05-08 11:52 ?467次阅读
    如何在CentOS<b class='flag-5'>系统</b><b class='flag-5'>中</b>配置<b class='flag-5'>防火墙</b>

    Linux系统iptables防火墙配置详解

    iptablesLinux内核中用于配置防火墙规则的工具。它基于Netfilter框架,可以对通过网络接口的数据包进行过滤、修改等操作。通过设置一系列规则,iptables能够控制哪
    的头像 发表于 06-18 15:25 ?359次阅读