Centos 7防火墙策略配置指南

Centos 7防火墙策略配置指南

—— 清听凌雪慕忆

@

1. 开启防火墙

1.1 user切换到root用户

[user@localhost ~] su root
密码:
[root@localhost home]# cd ~
[root@localhost ~]#

1.2 查看防火墙服务状态

 systemctl status firewalld

Centos 7防火墙策略配置指南

1.3 查看firewall的状态

 systemctl firewall state 或 firewall-cmd --state

Centos 7防火墙策略配置指南

1.4 启动/关闭防火墙

启动防火墙
 systemctl start firewalld

重启防火墙
 systemctl restart firewalld  或  systemctl reload firewalld

关闭
 systemctl stop firewalld

设置开机自启动防火墙
 systemctl enable firewalld.service

查看防火墙设置开机自启是否成功
 systemctl is-enabled firewalld.service

Centos 7防火墙策略配置指南

如下图所示,即为防火墙设置开机自启是否成功。

Centos 7防火墙策略配置指南

2. 防火墙端口配置

在开启防火墙之后,我们有些服务就会访问不到,是因为服务的相关端口没有打开。

2.1 查看已开启端口

 firewall-cmd --list-ports 或 netstat -ntlp

2.2 查看防火墙规则

 firewall-cmd --list-all

Centos 7防火墙策略配置指南

2.3 查看允许协议

 firewall-cmd --list-protocals

2.4 开启、关闭、查询端口

本次以开启80端口为例。

查询端口是否开放
 firewall-cmd --query-port=80/tcp

Centos 7防火墙策略配置指南

命令规则:

–permanent:表示永久生效,若没有重启失效;

— zone :表示作用域

–add-port=80/tcp 表示添加端口,格式为端口/通讯协议

开启端口的关键字:add

移除的关键字:remove

永久增加/开启80端口
 firewall-cmd --permanent --add-port=80/tcp
 firewall-cmd --permanent --add-port=998/tcp
 firewall-cmd --add-port=999/tcp
重启防火墙服务
 systemctl restart firewalld
删除端口
firewall-cmd --remove-port=999/tcp

Centos 7防火墙策略配置指南

Centos 7防火墙策略配置指南

3. 防火墙ip指向限制配置

3.1 允许指定ip访问所有流量

允许ip访问的关键字:accept

阻止ip访问的关键字:drop

开启192.168.43.88访问
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.43.100" accept"

重启防火墙服务
firewall-cmd --reload

禁止192.168.43.88访问
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.43.100" drop"

3.2 允许指定ip访问指定端口

允许IPV4 Ip是8.8.8.8 连接端口80 accept表示允许使用
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="8.8.8.8" port protocol="tcp" port="80" accept"

允许IPV4 Ip是10.168.186.25 连接端口22 accept表示允许使用
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.168.186.25" port protocol="tcp" port="22" accept"

 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.168.186.25,10.168.186.211" port protocol="tcp" port="3306" accept"

 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.168.186.0/24" port protocol="tcp" port="22" accept"

以上运行完后提示success则表示配置成功

3.3 允许指定ip访问指定协议

阻止ICMP包
 firewall-cmd  --permanent --add-rich-rule="rule protocol value="icmp" drop"

允许特定地址通过icmp
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="120.12.23.136" protocol value="icmp" accept"

3.4 允许指定ip访问指定服务

允许特定地址可以访问SSH服务
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.168.186.25" service name="ssh" accept"

3.5 防火墙移除某个服务

 firewall-cmd --remove-rich-rule="rule family="ipv4" source address="10.168.186.25" service name="ssh" drop"
#从允许通过防火墙的列表里移除SSH服务
 sudo firewall-cmd --remove-service=ssh --permanent

 firewall-cmd --remove-rich-rule="rule protocol value="icmp" drop" --permanent

Centos 7防火墙策略配置指南

3.6 其他

重启防火墙(修改配置后要重启防火墙)
 firewall-cmd --reload

加入白名单
 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="8.8.8.8" port protocol="tcp" port="80" accept"

查看规则
 firewall-cmd --list-rich-rules
显示是
 rule family="ipv4" source address="8.8.8.8" port port="80" protocol="tcp" accept
删除规则
 firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="8.8.8.8" port port="80" protocol="tcp" accept"

注意端口添加,删除后要更新防火墙
 firewall-cmd --reload

4. 获取所有支持的服务

firewall-cmd --get-services

Centos 7防火墙策略配置指南

4.1 增加、删除和服务

增加服务 (临时增加)
firewall-cmd --add-service=http
删除服务 (临时删除)
firewall-cmd --remove-service=http
查询服务
firewall-cmd --query-service=http

>”不是看到了希望才去坚持,而是坚持了才能看到希望”

Centos 7防火墙策略配置指南

Original: https://www.cnblogs.com/FlyingShrimp/p/16205209.html
Author: 小土豆大土豆
Title: Centos 7防火墙策略配置指南

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/583426/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球