【Linux】centos 防火墙学习

一、防火墙基础概念

1. 两种防火墙管理方式

  • iptables: 直接管理netfilter内核模块的传统工具

  • firewalld: 动态管理防火墙的守护进程(CentOS 7/8默认)

2. 常用术语

  • zone: 网络区域(public, internal, trusted等)

  • service: 预定义的服务规则集合

  • port: 端口号

  • rich rule: 复杂规则

二、firewalld 基础操作

1. 服务管理

bash 复制代码
# 查看防火墙状态
sudo systemctl status firewalld

# 启动防火墙
sudo systemctl start firewalld

# 停止防火墙
sudo systemctl stop firewalld

# 开机自启
sudo systemctl enable firewalld

# 禁止开机自启
sudo systemctl disable firewalld
复制代码

2. 基本命令

bash 复制代码
# 查看运行状态
sudo firewall-cmd --state

# 重新加载配置(不中断现有连接)
sudo firewall-cmd --reload

# 完全重启(中断连接)
sudo systemctl restart firewalld
复制代码

三、区域(Zone)管理

1. 查看默认区域

bash 复制代码
# 查看默认区域
sudo firewall-cmd --get-default-zone

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

# 查看活动的区域
sudo firewall-cmd --get-active-zones
复制代码

2. 更改区域

bash 复制代码
# 更改默认区域
sudo firewall-cmd --set-default-zone=internal

# 更改网卡区域
sudo firewall-cmd --zone=public --change-interface=eth0
复制代码

四、规则管理

1. 开放/关闭端口

bash 复制代码
# 开放端口(临时)
sudo firewall-cmd --add-port=80/tcp

# 开放端口(永久)
sudo firewall-cmd --add-port=80/tcp --permanent

# 关闭端口
sudo firewall-cmd --remove-port=80/tcp --permanent

# 查看开放的端口
sudo firewall-cmd --list-ports
复制代码

2. 服务管理

bash 复制代码
# 允许服务(例:http)
sudo firewall-cmd --add-service=http --permanent

# 移除服务
sudo firewall-cmd --remove-service=http --permanent

# 查看允许的服务
sudo firewall-cmd --list-services
复制代码

3. 高级规则

bash 复制代码
# 允许IP段
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'

# 端口转发
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
复制代码

五、iptables 基础(了解)

1. 基本命令结构

bash 复制代码
# 查看规则
sudo iptables -L -n -v

# 清空规则
sudo iptables -F

# 保存规则(CentOS 7)
sudo service iptables save

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

# 拒绝某个IP
sudo iptables -A INPUT -s 192.168.1.100 -j DROP
复制代码

六、实战案例

案例1:配置Web服务器防火墙

bash 复制代码
# 设置默认区域为public
sudo firewall-cmd --set-default-zone=public

# 开放服务
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# 开放额外端口
sudo firewall-cmd --permanent --add-port=3000/tcp

# 重新加载
sudo firewall-cmd --reload

# 验证配置
sudo firewall-cmd --list-all
复制代码

案例2:限制访问

bash 复制代码
# 仅允许特定IP访问SSH
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'

# 拒绝某个IP访问
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.50" reject'
复制代码

七、配置文件位置

firewalld配置

bash 复制代码
# 配置文件目录
/etc/firewalld/

# 主要配置文件
/etc/firewalld/firewalld.conf

# 区域配置文件
/etc/firewalld/zones/

# 服务定义
/usr/lib/firewalld/services/
复制代码

iptables配置

bash 复制代码
# 规则保存位置(CentOS 7)
/etc/sysconfig/iptables
复制代码

八、故障排查

常用命令

bash 复制代码
# 查看拒绝的日志
sudo journalctl -xe | grep -i firewall

# 查看完整规则
sudo firewall-cmd --list-all --zone=public

# 测试端口连通性
telnet 服务器IP 端口号
nc -zv 服务器IP 端口号


firewalld 与 iptables 详细对比

一、核心区别概述

特性 iptables firewalld
诞生时间 2001年 2011年(CentOS 7引入)
管理方式 静态配置 动态管理
配置方式 直接修改规则 区域、服务抽象
配置存储 一次性加载 运行时+持久化分离
重新加载 中断所有连接 不中断现有连接
复杂性 较低,直接 较高,抽象层
默认状态 CentOS 6及以前 CentOS 7/8/9默认

二、架构和工作原理

iptables 架构

复制代码
用户空间命令 (iptables) 
    ↓
内核空间 (netfilter框架)
    ↓
规则链 (INPUT/OUTPUT/FORWARD)
    ↓
规则表 (filter/nat/mangle/raw)
复制代码

firewalld 架构

复制代码
firewall-cmd/cli/GUI
    ↓
firewalld 守护进程
    ↓
DBus 接口
    ↓
iptables/nftables 后端
    ↓
内核空间 (netfilter)

三、将 iptables 规则转换为 firewalld

场景1:开放端口
复制代码
# iptables方式
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

# firewalld等价命令
firewall-cmd --add-port=3306/tcp --permanent
复制代码
场景2:允许特定IP
复制代码
# iptables方式
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

# firewalld等价命令
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept' --permanent
复制代码
场景3:端口转发
复制代码
# iptables方式
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

# firewalld等价命令
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
复制代码



相关推荐
RisunJan16 小时前
Linux命令-mail (发送和接收电子邮件)
linux·服务器
万象.16 小时前
Linux套接字socket编程(含TCP,UDP)
linux·tcp/ip·udp
ycjunhua16 小时前
Gool NoteBookLM 创建无法进入开发界面
笔记·学习
weixin_4434785116 小时前
flutter组件学习之Cupertino 组件(iOS风格)
学习·flutter·ios
finegx17 小时前
反汇编objdump和strace学习
linux·经验分享·学习
朱一头zcy17 小时前
[Win11家庭中文版]如何关闭基于虚拟化的安全性VBS(为了解决VBS启用状态下 VMware性能很差 频繁闪退或有各种不一样的崩溃报错)
linux·经验分享·vbs
xlp666hub17 小时前
【Linux驱动实战】:带参数的内核模块
linux·c
Shining059617 小时前
前沿模型系列(三)《检索增强的语言模型》
人工智能·学习·其他·语言模型·自然语言处理·大模型·rag
芥子沫17 小时前
Linux下编程有什么优势?
linux·运维·服务器
QYQ_112717 小时前
嵌入式学习——51单片机(下)
嵌入式硬件·学习·51单片机