2022
我们一起努力

轻松自如关闭与开启SELinux

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

img

脚本代码

#!/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

脚本测试 SELinux轻松一键开启与禁用脚本SELinux轻松一键开启与禁用脚本

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

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

本文来源:www.lxlinux.net/4135.html,若引用不当,请联系修改。

赞(0)
文章名称:《轻松自如关闭与开启SELinux》
文章链接:https://www.fzvps.com/153771.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!