下面以 Linux(CentOS / RHEL / Fedora 等使用 SELinux 的系统) 为例,说明如何“安装 / 加载 SELinux 模块”。如果你指的是编写并加载自定义 SELinux 策略模块,也包含在内。
getenforce
sestatus
常见状态:
Enforcing:强制模式(推荐)Permissive:仅记录不阻止Disabled:已关闭(需重启才能开启)若未安装 SELinux 工具:
yum install -y policycoreutils selinux-policy selinux-policy-targeted
# 或
dnf install -y policycoreutils selinux-policy-targeted
很多服务自带 SELinux 模块(如 httpd、mysql):
yum install -y selinux-policy-targeted
查看已加载模块:
semodule -l
如果你已有编译好的模块(如 myapp.pp):
semodule -i myapp.pp
查看是否加载成功:
semodule -l | grep myapp
.te 文件myapp.te
policy_module(myapp, 1.0)
type myapp_t;
type myapp_exec_t;
init_daemon_domain(myapp_t, myapp_exec_t)
allow myapp_t self:process { fork signal };
.fc 文件(文件上下文)myapp.fc
/usr/sbin/myapp -- gen_context(system_u:object_r:myapp_exec_t,s0)
make -f /usr/share/selinux/devel/Makefile
生成:
myapp.ppsemodule -i myapp.pp
restorecon -Rv /usr/sbin/myapp
semodule -r myapp
avc: denied 报错如果你能告诉我:
我可以给你更精确的步骤。