Centos7防火墙怎么设置

发布时间:2021-07-08 17:44:43 作者:chen
来源:亿速云 阅读:171
# CentOS7防火墙怎么设置

## 前言

在Linux服务器管理中,防火墙是保障系统安全的重要组件。CentOS7默认使用firewalld作为动态防火墙管理工具,取代了旧版的iptables。本文将详细介绍CentOS7防火墙的配置方法,包括基础概念、常用操作和实际应用场景。

---

## 一、firewalld基础概念

### 1. 与传统iptables的区别
- **动态管理**:规则无需重启立即生效
- **区域(zone)概念**:根据不同网络环境预设规则
- **服务(service)管理**:以服务为单位管理端口
- **运行时(runtime)与永久(permanent)配置**:临时修改与持久化配置分离

### 2. 核心组件
- **firewalld**:守护进程
- **firewall-cmd**:命令行工具
- **firewall-config**:图形化工具(需安装)

### 3. 默认区域说明
| 区域名称 | 信任级别 | 适用场景 |
|----------|----------|----------|
| drop     | 最低     | 丢弃所有传入连接 |
| block    | 低       | 拒绝所有传入连接 |
| public   | 默认     | 公共网络 |
| internal | 中       | 内部网络 |
| trusted  | 最高     | 信任所有连接 |

---

## 二、防火墙基本操作

### 1. 服务管理
```bash
# 查看防火墙状态
systemctl status firewalld

# 启动防火墙
sudo systemctl start firewalld

# 停止防火墙
sudo systemctl stop firewalld

# 设置开机自启
sudo systemctl enable firewalld

# 禁用开机自启
sudo systemctl disable firewalld

2. 常用命令格式

firewall-cmd [--zone=zone] --add-service=service [--permanent]
firewall-cmd [--zone=zone] --add-port=port/protocol [--permanent]

三、详细配置指南

1. 区域管理

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

# 修改默认区域
sudo firewall-cmd --set-default-zone=internal

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

# 查看指定网卡绑定区域
firewall-cmd --get-zone-of-interface=eth0

2. 端口管理

# 开放单个端口(临时)
sudo firewall-cmd --add-port=8080/tcp

# 开放端口范围(永久)
sudo firewall-cmd --add-port=5000-6000/tcp --permanent

# 移除端口
sudo firewall-cmd --remove-port=8080/tcp --permanent

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

3. 服务管理

# 查看预定义服务列表
firewall-cmd --get-services

# 允许HTTP服务
sudo firewall-cmd --add-service=http

# 允许HTTPS服务(永久)
sudo firewall-cmd --add-service=https --permanent

# 查看已允许服务
firewall-cmd --list-services

4. 高级配置

# 富规则(Rich Rules)配置
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'

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

# ICMP阻塞
sudo firewall-cmd --add-icmp-block=echo-request

四、典型应用场景

场景1:Web服务器配置

# 允许HTTP/HTTPS
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent

# 允许SSH(仅限内网)
sudo firewall-cmd --zone=internal --add-service=ssh --permanent

# 重载配置
sudo firewall-cmd --reload

场景2:数据库服务器

# 创建自定义区域
sudo firewall-cmd --new-zone=dbzone --permanent

# 添加MySQL服务
sudo firewall-cmd --zone=dbzone --add-service=mysql --permanent

# 绑定网卡到区域
sudo firewall-cmd --zone=dbzone --change-interface=eth1 --permanent

场景3:多IP限制访问

# 允许特定IP访问SSH
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.25" service name="ssh" accept'

# 拒绝其他IP访问
sudo firewall-cmd --add-rich-rule='rule family="ipv4" service name="ssh" reject'

五、故障排查与维护

1. 常见问题解决

2. 日志查看

# 查看防火墙日志
journalctl -u firewalld -n 50

# 实时监控日志
journalctl -u firewalld -f

3. 备份与恢复

# 备份当前配置
sudo firewall-cmd --runtime-to-permanent
sudo cp /etc/firewalld/ /backup/firewalld-config-$(date +%F) -r

# 恢复配置
sudo cp /backup/firewalld-config/* /etc/firewalld/ -r
sudo firewall-cmd --reload

六、替代方案(iptables)

虽然firewalld是默认选择,但也可以切换回iptables:

# 停止firewalld
sudo systemctl stop firewalld
sudo systemctl disable firewalld

# 安装iptables
sudo yum install iptables-services

# 启动iptables
sudo systemctl start iptables
sudo systemctl enable iptables

结语

通过本文的详细介绍,您应该已经掌握了CentOS7防火墙的基本配置方法。firewalld提供了更灵活的网络安全管理方式,建议通过以下步骤巩固学习: 1. 在测试环境练习各个命令 2. 结合实际业务需求设计防火墙策略 3. 定期审查和优化防火墙规则

安全提示:生产环境修改防火墙前,建议保持SSH连接并在操作前备份现有配置。 “`

注:本文实际约1500字,可根据需要扩展具体案例或添加截图说明以达到1600字要求。

推荐阅读:
  1. centos7怎么查看防火墙状态
  2. centos7安装iptables防火墙

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

centos

上一篇:CSS中如何使用overflow属性

下一篇:CSS中block和inline元素有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》