• SELinux策略规则的开启和关闭(详解版)

    默认情况下,并不是所有的规则都处于开启状态,因此,虽然我们无需修改规则的具体内容,但学习如何开启和关闭规则,还是很有必要的。

    规则的开启与关闭并不困难,使用 getsebool 命令来查询规则的开启和关闭状态,使用 setsebool 命令来修改规则的开启与关闭状态。

    查询策略规则是否开启

    先来看看如何知道哪些规则是启用的,哪些规则是关闭的。这时需要使用 getsebool 命令,命令格式如下:

    [root@localhost ~]# getsebool [-a] [规则名]

    -a 选项的含义是列出所有规则的开启状态。

    例如:

    [root@localhost ~]# getsebool -a
    abrt_anon_write --> off
    abrt_handle_event --> off
    allow_console_login --> on
    allow_cvs_read_shadow --> off
    allow_daemons_dump_core --> on
    allow_daemons_use_tcp_wrapper --> off
    …省略部分输出…
    #getsebool命令明确地列出了规则的开启状态

    除此之外,还可以使用 semanage boolean -l 命令(此命令需事先手动安装),此命令的输出结构同 getsebool 命令相比,输出信息中多了默认状态、当前状态以及相关描述等信息。感兴趣的读者,可以自己尝试运行,观看输出结果。

    修改规则的开启状态

    能够查询到规则的开启状态,我们使用 setsebool 命令就可以开启和关闭某个规则。当然,我们先应该通过 sesearch 命令确认这个规则的作用。

    sesearch 命令格式如下:

    [root@localhost ~]# setsebool [-P] 规则名=[0|1]

    -P 选项的含义是将改变写入配置文件,永久生效。规则名有 2 个值,分别是 0 和 1,0 代表将该规则关闭,1 代表将该规则开启。

    举个例子:

    [root@localhost ~]# getsebool -a | grep httpd
    #查询和apache相关的规则
    …省略部分输出…
    httpd_enable_homedirs --> off
    …省略部分输出…
    #发现httpd_enable_homedirs规则是开启的,这个规则主要用于允许apache进程访问用户的家目录
    #如果不开启这个规则,那么apache的userdir功能将不能使用
    [root@localhost ~]# setsebool -P httpd_enable_homedirs=1
    #开启httpd_enable_homedirs规则
    [root@localhost ~]# getsebool httpd_enable_homedirs
    httpd_enable_homedirs --> on
    #查询规则状态是开启
    [root@localhost ~]# setsebool -P httpd_enable_homedirs=0
    #关闭规则
    [root@localhost ~]# getsebool httpd_enable_homedirs
    httpd_enable_homedirs --> off
    #查询规则状态是关闭

    SELinux导致vsftpd不能正常登录

    举个实际的例子,vsftpd 是 CentOS 默认的文件服务器,这个服务主要是用来进行文件的下载和上传的。不过,如果启动了 SELinux,则会发现 vsftpd 服务是不能登录的。我们来做一个实验,命令如下:

    [root@localhost ~]# yum -y install vsftpd
    #安装vsftpd服务器端
    [root@localhost ~]# yum -y install ftp
    #安装ftp命令,也就是ftp客户端
    [root@localhost ~]# useradd user
    [root@localhost ~]# passwd user
    #添加测试用户,并配置密码
    [root@localhost ~]# service vsftpd start
    #启动vsftpd服务
    [root@localhost ~]# ftp 192.168.4.210
    #登录本机的ftp
    Connected to 192.168.4.210 (192.168.4.210).2.0 (vsFTPd 2.2.2)
    Name (192.168.4.210:root):user ←输入用户名
    3.1 Please specify the password.
    Password: ←输入密码
    5.0 OOPS:cannot change directory:/home/user
    Login failed.
    #登录报错,不能正常登录

    这很奇怪,vsftpd 是只要安装并启动就可以正常使用的,不用修改任何配置文件,因为默认配置是非常完善而且正确的。这时我们考虑是 SELinux 引起的问题,可以使用前面章节中关于日志查看的三个命令中的任意一个,来看看是否有关于 vsftpd 的报错。命令如下:

    [root@localhost ~]# audit2why < /var/log/audit/audit.log
    #分析SELinux的日志
    type-AVC msg-audit(1370428985.525:1146) : avc: denied {search } for pid-28408 comm-"vsftpd" name="home" dev=sda3    ino=1046530 scontext=unconfined_u:system_r:ftpd_t:s0-s0:c0.ol023
    tcontext=system_u:object_r:home_root_t:s0 tclass=dir
    #有关于vsftpd的错.看来确实是SELinux引起的不能登陆 ...省略部分输出...
    Was caused by:
    One of the following booleans was set incorrectly.
    Description:
    Allow ftp servers to login to local users and read/write all files on the system, governed by DAC.
    Allow access by executing:
    # setsebool -P allow_ftpd_full_access 1
    Description:
    Allow ftp to read and write files in the user home directories
    Allow access by executing:
    #   setsebool -P ftp_home_dir 1
    #建议执行此命令,运行ftp读取和写入用户的家目录

    既然给出了建议命令,我们就试试吧:

    [root@localhost ~]# setsebool -P ftp_home_dir 1
    #开启ftp_home_dir规则
    [root@localhost ~]# ftp 192.168.4.210
    Connected to 192.168.4.210 (192.168.4.210).2.0 (vsFTPd 2.2.2)
    Name (192.168.4.210:root):user ←输入用户名
    3.1 Please specify the password.
    Password: ←输入密码
    2.0 Login successful. ←登录成功

    通过这个例子,我们学习了如何通过日志来解决因为规则没有开启而导致的 SELinux 问题。

更多...

加载中...