配置防火墙和SELinux(1)

systemctl命令速查

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

1.启动防火墙之前要先关闭

2.服务的状态

firewall-cmd命令速查

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

区域相关命令速查

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

接口相关命令速查

root@client \~\]# firewall-cmd --zone=public --remove-interface=ens160 //从区域public中删除接口ens160 \[root@client \~\]# firewall-cmd --zone=external --change-interface=ens160 //修改接口ens160 所属区域为 default \[root@client \~\]# firewall-cmd --get-zone-of-interface=ens160 //查看接口 ens160 所属区域

端口控制命令速查

root@client \~\]# firewall-cmd --add-port=80/tcp --permanent //永久开启 80 端口(全局) \[root@client \~\]# firewall-cmd --remove-port=80/tcp --permanent //永久关闭 80 端口(全局) \[root@client \~\]# firewall-cmd --add-port=65001-65010/tcp --permanent //永久开启 65001-65010端口(全局) \[root@client \~\]# firewall-cmd --zone=public --add-port=80/tcp --permanent //永久开启 80 端口(区域 public) \[root@client \~\]# firewall-cmd --zone=public --remove-port=80/tcp --permanent //永久关闭 80端口(区域 public) \[root@client \~\]# firewall-cmd --zone=public --add-port=65001-65010/tcp --permanent //永久开启 65001-65010 端口(区域 public) \[root@client \~\]# firewall-cmd --query-port=8080/tcp //查询端口是否开放 \[root@client \~\]# firewall-cmd --permanent --add-port=80/tcp //开放 80 端口 \[root@client \~\]# firewall-cmd --permanent --remove-port=8080/tcp //移除端口 \[root@client \~\]# firewall-cmd --reload //重启防火墙(修改配置后要重启防火墙)

使用终端管理工具实例

1.查看firewalld服务当前状态和使用的区域

root@client \~\]# firewall-cmd --state //查看防火墙状态 \[root@client \~\]# systemctl restart firewalld \[root@client \~\]# firewall-cmd --get-default-zone //查看默认区域

2.查询防火墙生效ens160网卡在firewalld服务中的区域

root@client \~\]# firewall-cmd --get-active-zones //查看当前防火墙中生效的区域 \[root@client \~\]# firewall-cmd --set-default-zone=trusted //设定默认区域

3.把firewalld服务中ens160网卡默认区域修改为external,并在系统重启后生效。分别查看运行时模式与永久模式下的区域名称

root@client \~\]# firewall-cmd --list-all --zone=work //查看指定区域的防火墙策略 \[root@client \~\]# firewall-cmd --permanent --zone=external --change-interface=ens160 \[root@client \~\]# firewall-cmd --get-zone-of-interface=ens160 \[root@client \~\]# firewall-cmd --permanent --get-zone-of-interface=ens160

4.把firewalld服务的当前默认区域设置为public

root@client \~\]# firewall-cmd --set-default-zone=public \[root@client \~\]# firewall-cmd --get-default-zone

5.启动/关闭firewalld服务的应急状况模式,阻断一切网络连接

root@client \~\]# firewall-cmd --panic-on \[root@client \~\]# firewall-cmd --panic-off

6.查询public区域是否允许请求SSH和HTTPS的流量

root@client \~\]# firewall-cmd --zone=public --query-service=ssh \[root@client \~\]# firewall-cmd --zone=public --query-service=https

7.把firewalld服务中请求https的流量设置为永久允许,并立即生效

root@client \~\]# firewall-cmd --get-services //查看所有可以设定的服务 \[root@client \~\]# firewall-cmd --zone=public --add-service=https \[root@client \~\]# firewall-cmd --permanent --zone=public --add-service=https \[root@client \~\]# firewall-cmd --reload \[root@client \~\]# firewall-cmd --list-all //查看生效的防火墙策略

8.把firewalld服务中请求https的流量设置为永久拒绝,并立即生效

root@client \~\]# firewall-cmd --permanent --zone=public --remove-service=https \[root@client \~\]# firewall-cmd --reload \[root@client \~\]# firewall-cmd --list-all //查看生效的防火墙策略

9.把在firewalld服务中访问8088和8089端口的流量策略设置为允许,但仅限当前生效

root@client \~\]# firewall-cmd --zone=public --add-port=8088-8089/tcp \[root@client \~\]# firewall-cmd --zone=public --list-ports

图像化的配置

先配置yum仓库

root@server \~\]#cd /etc/yum.repos.d/ \[root@server \~\]#rm -f \* \[root@server \~\]#curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo \[root@server \~\]#sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo \[root@server \~\]# mount /dev/cdrom /media/ \[root@server \~\]# dnf install firewall-config -y

设置SELinux的模式

使用配置文件设置SELinux的模式

root@client \~\]# cat /etc/selinux/config -n

使用命令行命令设置SELinux的模式

root@client \~\]# getenforce //检查当前SELinux的运行状态 \[root@client \~\]# setenforce permissive //切换到宽容模式(Permissive) \[root@client \~\]# setenforce 1 //1代表强制模式(Enforcing) \[root@client \~\]# setenforce 0 //0代表宽容模式(Permissive) \[root@client \~\]# sestatus //查看SELinux的运行状态

查看用户、文件和进程的安全上下文

Z都是大写!!!

root@client \~\]# id -Z //查看用户的安全上下文 \[root@client \~\]# ls -Zl //查看文件的安全上下文 \[root@client \~\]# ps -Z //查看进程的安全上下文

相关推荐
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
Sinclair3 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
Rockbean4 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩4 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
崔小汤呀4 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应4 天前
vi编辑器使用
linux·后端·操作系统