SELinux可以让进程在沙箱内运行,进程只能访问沙箱内的文件,从而达到安全目的。它会在启动linux时给每个文件打上label标签,当进程运行时SELinux根据设定的规则通过对比这个标签来判定此进程是否可以访问这个文件。也就是在普通的文件权限上又加上了标签机制。


激活SELinux:

        在/etc/sysconfig/selinux文件中:

        # This file controls the state of SELinux on the system.

        # SELINUX= can take one of these three values:

        #     enforcing - SELinux security policy is enforced.

        #     permissive - SELinux prints warnings instead of enforcing.

        #     disabled - No SELinux policy is loaded.

        SELINUX=enforcing

        # SELINUXTYPE= can take one of these two values:

        #     targeted - Targeted processes are protected,

        #     mls - Multi Level Security protection.

        SELINUXTYPE=targeted

        将SELINUX的disabled改为enforcing或permissive,然后重启系统。关闭设为disabled,无论是启用或关闭都需要重启系统才能生效,因为需要重新打标。


SELinux的状态:

        enforcing:强制,每个受限的进程都必然受限

        permissive:启用,每个受限的进程违规操作不会被禁止,但会被记录于审计日志

        disabled:关闭


查看标签:

        ls -Z

                -rw-r--r--. root root system_u:object_r:selinux_config_t:s0 config

                -rw-r--r--. root root system_u:object_r:selinux_config_t:s0 restorecond.conf

        ps auxZ


标签:

        user:role:type

        user: SELinux的user

        role: 角色

        type: 类型


相关命令:

        getenforce             获取selinux当前状态

        setenforce 0|1        0:设置为permissive,1:设置为enforcing,重启系统后无效


配置文件:

        /etc/sysconfig/selinux,/etc/selinux/config 这两个配置文件是同一个文件。

        SELINUX={disabled|enforcing|permissive}


给文件重新打标:

        chcon

               chcon [OPTION]... CONTEXT FILE...

               chcon [OPTION]... [-u USER] [-r ROLE] [-t TYPE] FILE...

               chcon [OPTION]... --reference=RFILE FILE...

        常用的就是chcon -type可以修改标签类型已达到阻止访问该文件的目的。


还原文件的默认标签:

        restorecon [-R] /path/to/somewhere


布尔型规则:

        getsebool -a        查看规则内容

                abrt_anon_write --> off

                abrt_handle_event --> off

                allow_console_login --> on

                allow_cvs_read_shadow --> off

        setsebool file_home_dir 1            开启或关闭此规则,临时生效。

        setsebool  -P  file_home_dir on     将此规则永久生效,使用此命令时会将配置文件转换成二进制,需要等待一段时间。


日志文件:

        /var/log/audit/audit.log    当SELinux为permissive模式时,不会阻止访问,而是把访问过的文件记录在日志中,就是这个日志文件。

关闭SELinux:

sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config