新手上路:学会使用SELinux保护你的系统

1 Selinux的介绍

SELinux是为了提高系统安全性的机制。

它对系统的每一个程序、文件都引入了安全上下文。安全上下文标签,用于唯一标识文件、进程和资源。这些标签包括了安全策略的信息,允许SELinux强制执行策略。

1.1 Selinux关闭状态下

bash 复制代码
getenforce # 查看selinux开启状态
/etc/selinux/config # 在此文件中可以修改selinux的开启与关闭



1.2 Selinux开启状态下

2 Selinux的开启与关闭

2.1 Selinux的永久开启与关闭

selinux的开启与关闭切换需要reboot系统

bash 复制代码
/etc/selinux/config # 修改文件
SELINUX=disabled #selinux关闭
SELINUX=enforcing #selinux开机设定为强制状态此状态为selinux开启
SELINUX=permissive #selinux开机设定为警告状态此状态为selinux开启

2.2 Selinux的临时修改

bash 复制代码
selinux开启后强制和警告级别的转换
setenforce 0 ##警告
setenforce 1 ##强制


3 Selinux的安全上下文

3.1 查看安全上下文

bash 复制代码
ls -Z ##查看文件的安全上下文
ls -Zd ##查看目录的安全上下文
ps axZ ##查看进程的安全上下文

3.1 临时修改安全上下文

bash 复制代码
chcon -t public_content_t /var/ftp/westosfile1 # public_content_t是安全上下文标签
chcon -Rt public_content_t /westosdir #修改目录及目录中的所有子文件的安全上下文

3.2 永久修改安全上下文

bash 复制代码
semanage fcontext -l | less ##查看内核安全上下文列表
semanage fcontext -a -t public_content_t '/westosdir(/.*)?' # 修改安全上下文
semanage fcontext -d -t public_content_t '/westosdir(/.*)?' # 恢复安全上下文为默认
restorecon -RvvF /westosdir/  # 刷新
touch /.autorelabel ##重启系统时selinux初始化文件标签开关文件



4 SEBOOL

bash 复制代码
getsebool -a ##现实服务的bool值
setsebool -P ftpd_anon_write on #更改

5 SEPORT 端口设定

bash 复制代码
semanage port -l | grep ssh # 查看允许的端口号
semanage port -a -t ssh_port_t -p tcp 1111 # 将想要使用的端口号添加到seport表中


6 Selinux排错

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