【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
复制代码



相关推荐
崔小汤呀13 小时前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应13 小时前
vi编辑器使用
linux·后端·操作系统
何中应13 小时前
Linux进程无法被kill
linux·后端·操作系统
何中应13 小时前
rm-rf /命令操作介绍
linux·后端·操作系统
何中应13 小时前
Linux常用命令
linux·操作系统
葛立国13 小时前
从 / 和 /dev 说起:Linux 文件系统与挂载点一文理清
linux
西岸行者1 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
哇哈哈20211 天前
信号量和信号
linux·c++
不是二师兄的八戒1 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
欧云服务器1 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian