Linux如何启动SELinux,


目录
  • Linux启动SELinux
    • centos
    • Utunbu
  • 总结

    Linux启动SELinux

    centos

    centos自带SELinux。

    检查SELinux是否开启

    root@ubuntu:/home/wenyao/Desktop# getenforce
    Permissive
    #如果是disabled证明是未开启,而(enforcing,permissive,不同模式)证明已经开启
    

    开启

    先编辑/etc/selinux/config配置信息

    • 强制模式SELINUX=enforcing:表示所有违反安全策略的行为都将被禁止。
    • 宽容模式SELINUX=permissive:表示所有违反安全策略的行为不被禁止,但是会在日志中作记录。
    # 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.
    #改为permissive(测试一般是permissive模式,会有警告提醒)
    SELINUX=permissive
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    

    在根目录下创建隐藏文件.autorelabel,然后重启Linux

    #新建标志文件
    touch /.autorelabel
    #重启
    reboot
    

    验证

    #查看模式
    root@ubuntu:/home/wenyao/Desktop# getenforce
    #查看SELinux状态
    root@ubuntu:/home/wenyao/Desktop# sestatus
    

    切换SELinux模式

    模式切换不需要重启linux,而在关闭SELinux(设为disabled)或开启(设为permissive,Enforcing)需要重启Linux。

    #将 SELinux 在 Enforcing 与 permissive 之间切换(不需要重启)
    #0 : 转成permissive 宽容模式
    #1 : 转成Enforcing 强制模式
    #切换为宽容模式
    setenforce 0
    
    #想关闭只能去改配置文件/etc/selinux/config(需要重启)
    vim /etc/selinux/config
    

    Utunbu

    安装SELinuxUtunbu似乎不是自带SELinux的,而是使用AppArmor代替。

    本人在Utunbu20.4测试,发现只有AppArmor,所以我需要使用SELinux进行替换。

    安装SELinux

    #暂停apparmor
    sudo systemctl stop apparmor
    #卸载apparmor
    sudo apt purge apparmor
    #更新apt源
    sudo apt update
    #安装SELiunx需要的包
    sudo apt install policycoreutils selinux-utils selinux-basics
    #验证安装成功(会被告知你需要重启)
    sudo selinux-activate
    #重启
    reboot
    

    开启

    也是/etc/selinux/config配置信息,会发现跟centos那边的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.
    #默认是premissive
    SELINUX=permissive
    # SELINUXTYPE= can take one of these two values:
    # default - equivalent to the old strict and targeted policies
    # mls     - Multi-Level Security (for military and educational use)
    # src     - Custom policy built from source
    SELINUXTYPE=default
    # SETLOCALDEFS= Check local definition changes
    SETLOCALDEFS=0
    

    验证

    验证方式没改变

    root@ubuntu:/home/wenyao/Desktop# getenforce 
    Permissive
    
    root@ubuntu:/home/wenyao/Desktop# sestatus
    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             default
    Current mode:                   permissive
    Mode from config file:          permissive
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Memory protection checking:     requested (insecure)
    Max kernel policy version:      33
    

    切换SELinux模式

    跟Centos一样。

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持PHP之友。

    您可能感兴趣的文章:
    • Linux中如何开启或关闭SELinux
    • Linux中SELinux三种模式的启动、关闭与查看方式
    • Linux之CentOS7下如何关闭SELINUX
    • Linux中SELinux、Shell简介、touch命令的应用小结
    • 开启Selinux遇到的坑及解决

    相关内容