centos8配置防火墙

今天来给大家介绍一下配置centos8配置防火墙

centos8配置防火墙centos8配置防火墙

1、安装

yum install iptables-services #安装iptables

2、systemctl使用

systemctl unmask firewalld #执行命令,即可实现取消服务的锁定
 systemctl mask firewalld # 下次需要锁定该服务时执行
 systemctl start firewalld.service #启动防火墙 
 systemctl stop firewalld.service #停止防火墙 
 systemctl reloadt firewalld.service #重载配置
 systemctl restart firewalld.service #重启服务
 systemctl status firewalld.service #显示服务的状态
 systemctl enable firewalld.service #在开机时启用服务
 systemctl disable firewalld.service #在开机时禁用服务
 systemctl is-enabled firewalld.service #查看服务是否开机启动
 systemctl list-unit-files|grep enabled #查看已启动的服务列表
 systemctl --failed #查看启动失败的服务列表

3、firewall-cmd使用

 firewall-cmd --state #查看防火墙状态 
 firewall-cmd --reload #更新防火墙规则 
 firewall-cmd --state #查看防火墙状态 
 firewall-cmd --reload #重载防火墙规则 
 firewall-cmd --list-ports #查看所有打开的端口 
 firewall-cmd --list-services #查看所有允许的服务 
 firewall-cmd --get-services #获取所有支持的服务

区域相关

firewall-cmd --list-all-zones #查看所有区域信息 
 firewall-cmd --get-active-zones #查看活动区域信息 
 firewall-cmd --set-default-zone=public #设置public为默认区域 
 firewall-cmd --get-default-zone #查看默认区域信息 
 firewall-cmd --zone=public --add-interface=eth0 #将接口eth0加入区域public

接口相关

firewall-cmd --zone=public --remove-interface=eth0 #从区域public中删除接口eth0 
 firewall-cmd --zone=default --change-interface=eth0 #修改接口eth0所属区域为default 
 firewall-cmd --get-zone-of-interface=eth0 #查看接口eth0所属区域

4、用例

firewall-cmd --add-port=80/tcp --permanent #永久添加80端口例外(全局)
 firewall-cmd --remove-port=80/tcp --permanent #永久删除80端口例外(全局)
 firewall-cmd --add-port=65001-65010/tcp --permanent #永久增加65001-65010例外(全局) 
 firewall-cmd --zone=public --add-port=80/tcp --permanent #永久添加80端口例外(区域public)
 firewall-cmd --zone=public --remove-port=80/tcp --permanent #永久删除80端口例外(区域public)
 firewall-cmd --zone=public --add-port=65001-65010/tcp --permanent #永久增加65001-65010例外(区域public)   
firewall-cmd --query-port=8080/tcp # 查询端口是否开放

本文地址:https://www.linuxprobe.com/centos-iptables-linux.html

### 配置 CentOS 6 中的防火墙规则 在 CentOS 6 系统中,防火墙管理主要依赖于 `iptables` 工具。与后续版本的 `firewalld` 不同,CentOS 6 使用的是传统的 `iptables` 服务来管理网络规则。 #### 启动和管理防火墙 可以通过以下命令控制 `iptables` 服务: ```bash # 启动防火墙 service iptables start # 停止防火墙 service iptables stop # 重启防火墙 service iptables restart # 查看防火墙状态 service iptables status # 设置开机自动启动 chkconfig iptables on # 关闭开机启动 chkconfig iptables off ``` #### 开启特定端口 如果需要开放某个特定端口(例如 HTTP 的 80 端口),可以在 `/etc/sysconfig/iptables` 文件中添加相应的规则[^4]。以下是开启 80 端口的示例规则: ```bash -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT ``` 修改完成后,使用以下命令保存并重新加载配置: ```bash # 保存配置 service iptables save # 重新加载配置 service iptables reload ``` #### 添加基于 IP 地址的访问规则 除了开放端口之外,还可以根据源 IP 地址限制访问。例如,允许特定 IP 访问某个服务,可以添加如下规则: ```bash -A INPUT -s 192.168.5.14 -p tcp --dport 80 -j ACCEPT ``` 此规则表示允许来自 `192.168.5.14` 的流量通过 TCP 协议访问 80 端口[^5]。 #### 规则列表查看 可以通过以下命令查看当前的防火墙规则列表: ```bash chkconfig --list iptables ``` #### 安全策略建议 为了防止在配置过程中意外锁定服务器,建议先添加一些宽泛的访问策略,例如允许所有流量通过 22 端口(SSH)以确保能够远程访问系统。完成基础配置后,逐步移除不必要的宽松规则,并定期检查已生效的规则以确保配置正确性[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值