CentOS中SELinux Context配置文件的位置及说明
在CentOS系统中,SELinux(Security-Enhanced Linux)的Context(安全上下文) 主要用于定义文件、目录、进程等客体的安全属性(如用户、角色、类型),其配置文件集中存储在/etc/selinux/targeted/contexts/目录下(targeted是CentOS默认的SELinux策略类型,若使用mls策略则路径类似)。以下是关键配置文件及用途:
/etc/selinux/targeted/contexts/files/file_contexts/var/www/html/目录默认属于httpd_sys_content_t类型)。新增或修改此文件中的规则后,需使用restorecon命令恢复相关对象的上下文。/etc/selinux/targeted/contexts/files/file_contexts.localfile_contexts中的默认规则,修改后同样需要restorecon生效。/etc/selinux/targeted/contexts/files/dir_contextsfile_contexts互补),例如/tmp/目录的上下文设置。/etc/selinux/targeted/contexts/files/port_contexts80/tcp端口,其上下文为http_port_t)。/etc/selinux/targeted/contexts/users/user_contexts/etc/selinux/configSELINUX=enforcing/permissive/disabled)和策略类型(SELINUXTYPE=targeted/mls)。修改此文件需重启系统生效,是SELinux的基础配置文件。file_contexts、file_contexts.local)后,需使用restorecon -Rv /path/to/target命令恢复目标对象(文件/目录)的默认上下文,否则新规则不会自动应用。chcon命令(如chcon -t httpd_sys_content_t /path/to/file),但该修改在restorecon后会失效。semanage fcontext命令(属于policycoreutils-python工具包)可更安全地管理文件上下文(支持正则表达式,规则持久化),例如:semanage fcontext -a -t httpd_sys_content_t "/data/web(/.*)?",随后执行restorecon -Rv /data/web应用规则。通过以上配置文件,SELinux实现了对系统中各类资源的细粒度访问控制,确保只有授权的主体(如进程)能访问对应的客体(如文件)。