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

Crq
Crq
管理员
1964
文章
0
粉丝
Linux教程评论121字数 284阅读0分56秒阅读模式
摘要SELinux是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。但是SELinux的并不能与众多服务很好的兼容,有些人会关闭SELinux...

SELinux轻松一键开启与禁用脚本-图片1

脚本代码

#!/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轻松一键开启与禁用脚本-图片2

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

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

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2024年9月12日 04:58:58
  • 转载请注明:https://www.cncrq.com/10681.html
smem – Linux 内存监视软件 Linux教程

smem – Linux 内存监视软件

Linux 系统的内存管理工作中,内存使用情况的监控是十分重要的,在各种 Linux 发行版上你会找到许多这种工具。它们的工作方式多种多样,在这里,我们将会介绍如何安装和使用这样的...
Zookeeper集群搭建 Linux教程

Zookeeper集群搭建

由于公司缓存方案改进,准备采用codis集群作为主要的缓存解决方案(codis:国内豌豆荚开发的redis集群解决方案,已开源,github地址:https://github.co...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证