Centos7服务器防火墙设置教程

Centos7服务器防火墙设置教程

系统环境:Centos7

首先,确保你的系统上安装了 firewalld。通常,在 CentOS 7 上,firewalld 已经预装。如果没有安装,可以通过以下命令安装:

sudo yum install firewalld

启动并设置 firewalld 在系统启动时自动启动:

sudo systemctl start firewalld

sudo systemctl enable firewalld

以下是一些防火墙的常用安全设置实例

开放Web服务端口

对于运行Web服务的服务器,通常需要开放80(HTTP)和443(HTTPS)端口。

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-service=https

firewall-cmd --reload

开放SSH端口

为了安全地远程管理服务器,需要开放SSH端口(默认22)。

firewall-cmd --permanent --add-service=ssh

firewall-cmd --reload

开放数据库端口

如果运行MySQL/MariaDB数据库,需要开放3306端口。

firewall-cmd --permanent --add-port=3306/tcp

firewall-cmd --reload

开放DNS服务端口

运行DNS服务需要开放53端口(TCP和UDP)。

firewall-cmd --permanent --add-service=dns

firewall-cmd --reload

配置FTP服务

FTP服务通常需要开放21端口和被动模式端口范围。

firewall-cmd --permanent --add-service=ftp

firewall-cmd --permanent --add-port=30000-31000/tcp # 被动模式端口范围

firewall-cmd --reload

配置邮件服务器

邮件服务器可能需要开放多个端口,如25(SMTP)、110(POP3)、143(IMAP)等。

firewall-cmd --permanent --add-service=smtp

firewall-cmd --permanent --add-service=pop3

firewall-cmd --permanent --add-service=imap

firewall-cmd --reload

配置网络文件系统服务

网络文件系统服务需要开放2049端口和其他相关端口。

firewall-cmd --permanent --add-service=nfs

firewall-cmd --permanent --add-service=rpc-bind

firewall-cmd --permanent --add-service=mountd

firewall-cmd --reload

配置Windows文件共享服务

Windows文件共享服务需要开放137-139和445端口。

firewall-cmd --permanent --add-service=samba

firewall-cmd --reload

配置DHCP服务

DHCP服务需要开放67和68端口(UDP)。

firewall-cmd --permanent --add-service=dhcp

firewall-cmd --reload

注意事项

规则生效:在配置完所有规则后,需要重新加载firewalld以使规则生效。使用firewall-cmd --reload命令。

安全性:在开放端口时,应仅开放必要的服务端口,避免开放不必要的端口以减少安全风险。

补充:

禁用某个IP为192.168.0.100的地址禁止访问80端口:

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.100" port protocol="tcp" port="80" reject"

firewall-cmd --reload

解除刚才被限制的192.168.0.100

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.200" port protocol="tcp" port="80" accept"

firewall-cmd --reload

限制IP地址段

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="172.16.1.0/24" port protocol="tcp" port="80" reject"

firewall-cmd --reload

相关推荐
未济4 天前
linux 配置环境变量
linux
傲世仙尊4 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.4 天前
进程间通信
linux
AI职业加油站4 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-054 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏4 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz4 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅4 天前
linux(8) 软硬链接
linux·运维·服务器
Wang's Blog4 天前
Java 项目实战: 外卖平台-文件下载与ServletOutputStream回写浏览器
服务器·项目开发