轻松自如关闭与开启SELinux

树叶云

SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块,SELinux 的结构及配置非常复杂,而且有大量概念性的东西,要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux 关闭了,本篇文章就为大家分享一下如何方便的关闭与开启SELinux。

脚本代码

#!/bin/bash  #  -------------+--------------------  # * Filename    :       selinux.sh          # * Revision    :       2.0            # * Date        :       2017-09-02# * Author      :       Aubin          # * Description :                      #  -------------+---------------------#       www.shuaiguoxia.com#

path=/app/selinux
selinux=`sed -rn "/^(SELINUX=).*\$/p" $path`

case $1 in
enforcing|en)

       sed -ri "s@^(SELINUX=).*\$@\1enforcing@g" $path
       if [ $selinux ==  'SELINUX=disabled' ];then
               read -p "SELinux enforcing. you need reboot system ( yes or no ):" input
               [ $input == 'yes' -o $input == 'y' ] && reboot || echo "please Manual operation reboot"
       else
               echo "SELinux enforcing."
       fi
       ;;
permissive|per|pe)
       sed -ri "s@^(SELINUX=).*\$@\1permissive@g" $path
       if [ $selinux == 'SELINUX=disabled' ];then
               read -p "SELinux permissive. you need reboot system ( yes or no ):" input
               [ $input == 'yes' -o $input == 'y'] && reboot || echo "please Manual operation reboot"
       else
               echo "SELINUX permissive"
       fi
       ;;
disabled|dis|di)
       sed -ri "s@^(SELINUX=).*\$@\1disabled@g" $path
       if [ $selinux == 'SELINUX=enforcing' ];then
                read -p "SELinux permissive. you need reboot system ( yes or no ):" input
               [ $input == 'yes' -o $input == 'y' ] && reboot || echo "please Manual operation reboot"
       else
               echo "SELINUX disabled"
       fi
       ;;
l|a)
       echo `sed -nr 's@(^SELINUX=.*)@\1@p' $path`
       ;;
help|--help)
       echo "$0 [ enforcing | permissive | disabled  ]"
       ;;
*)
       echo "$0 [ enforcing | permissive | disabled  ]"

       ;;
esac

脚本测试

根据case语句对用户的位置变量(输入的参数)进行判断,进而根据不同的参数实现不同的效果。

SELinux在enforcing状态与disabled状态切换时必须要进行重启才能生效,所以要在脚本中判断用户之前的SELinux的状态是什么样的,询问用户是否进程重启操作系统。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/220587.html<

(0)
运维的头像运维
上一篇2025-04-14 15:37
下一篇 2025-04-14 15:38

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注