阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

28次阅读
没有评论

共计 1525 个字符,预计需要花费 4 分钟才能阅读完成。

导读 SELinux 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux 历史上最杰出的新安全子系统。但是 SELinux 的并不能与众多服务很好的兼容,有些人会关闭 SELinux 一了百了。在日常的运维过程中很少去频繁的开启关闭 SElinux,今天我就写一个关闭与开启 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

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

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

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

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计1525字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中