掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
SELinux 主要由美國國家安全局開發(fā)。2.6 及以上版本的 Linux 內(nèi)核都已經(jīng)集成了 SELinux 模塊,SELinux 的結(jié)構(gòu)及配置非常復雜,而且有大量概念性的東西,要學精難度較大。很多 Linux 系統(tǒng)管理員嫌麻煩都把 SELinux 關(guān)閉了,本篇文章就為大家分享一下如何方便的關(guān)閉與開啟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
腳本測試
根據(jù)case語句對用戶的位置變量(輸入的參數(shù))進行判斷,進而根據(jù)不同的參數(shù)實現(xiàn)不同的效果。
SELinux在enforcing狀態(tài)與disabled狀態(tài)切換時必須要進行重啟才能生效,所以要在腳本中判斷用戶之前的SELinux的狀態(tài)是什么樣的,詢問用戶是否進程重啟操作系統(tǒng)。

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流