SELinux详细配置

SELinux的三个选项:

 enforcing    开启并生效
    permissive    开启不生效
    disabled        关闭

1、临时关闭

    setenforce 0

2、永久关闭,重启生效

    vim /etc/sysconfig/selinux
    SELINUX=disabled

3、查看当前状态

    getenforce

例:selinux是开启状态
    yum -y install httpd
    systemctl start httpd
    echo test  > /var/www/html/index.html

4、查看文件上下文标签
    ll -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:unlabeled_t:s0 /var/www/html/index.html
    curl 127.0.0.1    #可以查看    
    
    echo test001 > /root/index.html
    ll -Z index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html
    mv  index.html /var/www/html/
    curl 127.0.0.1    #报错
    
    setenforce 0
    curl 127.0.0.1    #可以查看
5、修改上下文标签的两个命令

    chcon、semanage
    chcon -t httpd_sys_content_t /var/www/html/index.html
    ll -Z /var/www/html/index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
    setenforce 1
    curl 127.0.0.1    #可以查看
修改上下文规则
    semanage fcontext -a -t httpd_sys_content_t "/webapp(/.*)?"

6、恢复上下文
    restorecon  路径

SELinux  boolean
1、查看所有的布尔值

    getsebool -a
    off  关闭    
    on  开启

2、修改布尔变量

    临时生效
    setsebool  ftpd_use_nfs=1
    getsebool -a | grep ftpd_use_nfs
ftpd_use_nfs --> on

    永久生效
    setsebool  -P  ftpd_use_nfs=1

如果不会上面的命令,还使用上面的例子
这个命令可以帮你找到问题并提供解决方法

    sealert -a /var/log/audit/audit.log
    ausearch -c 'httpd' --raw | audit2allow -M my-httpd
    semodule -i my-httpd.pp

SELinux:port security

1、查看selinux对应的端口
    semanage  port  -l

2、添加端口
ssh默认端口是22,如果selinux是开启状态,改成2222端口,就启动不了了
使用以下命令就可以重启了
    semanage port -a -t ssh_port_t -p tcp 2222
    semanage port -l | grep ssh
ssh_port_t                     tcp      2222, 22

3、删除添加的端口
    semanage port --delete -p tcp 2222
    semanage port -l | grep ssh
ssh_port_t                     tcp      22

原文地址:https://www.cnblogs.com/ccshi/p/14142485.html