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

相关推荐
阿里云大数据AI技术6 分钟前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
你好潘先生6 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
orion5718 小时前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站1 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
程序员老赵1 天前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
火车叼位1 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
vivo互联网技术1 天前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式