Linux服務器安全初始化自選安裝Shell腳本

 2012-10-30 18:14:55

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://300second.blog.51cto.com/7582/1043603

PS:本Shell腳本主要用於新安裝Linux服務器系統的初始化 安全設置,具體包括:修改系統Yum源,設置字符編碼,關閉防火牆和Selinux,關閉不必要系統服務,關閉IPV6模塊,調整系統打開文件數,優化系 統內核等。大家可以根據自己的實際情況做出修改,用於生產環境前請先做好測試,已經在CentOS 5.8 i386下通過。

#!/bin/bash
#
# Script Name: initSystem.sh
# Description: setup linux system init.
# Author: 300second - 51cto.com
# Date: 2012-10-30

#set env
export PATH=$PATH:/bin:/sbin:/usr/sbin
export LANG="zh_CN.GB18030"

#require root to run this script.
if [[ "$(whoami)" != "root" ]]; then
 echo "Please run this script as root." >&2
 exit 1
fi

#define cmd var
SERVICE=`which service`
CHKCONFIG=`which chkconfig`

#Source function library.
. /etc/init.d/functions

#Modify the system yum source  //修改系統Yum源
initYum() {
cat << EOF
+--------------------------------------------------------------+
+------      Welcome to Modify the system yum source.    ------+
+--------------------------------------------------------------+
EOF
 
cd /etc/yum.repos.d/
\cp CentOS-Base.repo CentOS-Base.repo.ori.$(date +%F)
ping -c 1 baidu.com >/dev/null
[ ! $? -eq 0 ] && echo $"Networking not configured - exiting" && exit 1
wget --quiet -o /dev/null
http://mirrors.sohu.com/help/CentOS-Base-sohu.repo
\cp CentOS-Base-sohu.repo CentOS-Base.repo
echo "Modify the system yum source.------->OK"
sleep 3
}

#Set the character encoding  //設置字符編碼
initI18n() {
cat << EOF
+--------------------------------------------------------------+
+------       Welcome to Set the character encoding.     ------+
+--------------------------------------------------------------+
EOF

echo "#set LANG="zh_cn.gb18030""
\cp /etc/sysconfig/i18n /etc/sysconfig/i18n.$(date +%F)
sed -i 's#LANG="en_US.UTF-8"#LANG="zh_CN.GB18030"#' /etc/sysconfig/i18n
source /etc/sysconfig/i18n
grep LANG /etc/sysconfig/i18n
echo "Set the character encoding.------->OK"
sleep 3
}

#Close the firewall and Selinux  //關閉防火牆和Selinux
initFirewall() {
cat << EOF
+--------------------------------------------------------------+
+------     Welcome to Close the firewall and Selinux.   ------+
+--------------------------------------------------------------+
EOF

\cp /etc/selinux/config /etc/selinux/config.`date +"%Y-%m-%d_%H-%M-%S"`
/etc/init.d/iptables stop
chkconfig iptables off
sed -i 's/SELINUX=enable/SELINUX=disabled/' /etc/selinux/config
setenforce 0
/etc/init.d/iptables status
grep SELINUX=disabled /etc/selinux/config
echo "Close the firewall and Selinux.------->OK"
sleep 3
}

#Close unnecessary system service  //關閉不必要系統服務
initService() {
cat << EOF
+--------------------------------------------------------------+
+------   Welcome to Close unnecessary system service .  ------+
+--------------------------------------------------------------+
EOF

export LANG="en_US.UTF-8"
for i in `chkconfig --list |grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $i off;done
for i in crond network sshd syslog;do chkconfig --level 3 $i on;done
export LANG="zh_CN.GB18030"
echo "Close unnecessary system service.------>OK"
sleep 3
}

#Set the sshConfig banned root login  //設置sshConfig,禁止root登錄
initSsh() {
cat << EOF
+--------------------------------------------------------------+
+------  Welcome to Set the sshConfig banned root login. ------+
+--------------------------------------------------------------+
EOF

\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"`
sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config
sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config
sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config
sed -i 's%#UseDNS yes%UseDNS no' /etc/ssh/sshd_config
egrep "UseDNS|52113|RootLogin|EmptyPass" /etc/ssh/sshd_config
/etc/init.d/sshd reload
echo "Set the sshConfig banned root login.------>OK"
sleep 3
}

#Disable ctrlaltdel three key to reboot system  //禁止ctrl+alt+del三個鍵重啓系統
initSafe() {
cat << EOF
+--------------------------------------------------------------+
+-- Welcome to Disable ctrlaltdel three key to reboot system.--+
+--------------------------------------------------------------+
EOF

\cp /etc/inittab /etc/inittab.`date +"%Y-%m-%d_%H-%M-%S"`
sed -i "s/ca::ctrlaltdel:\/sbin/shutdown -t3 -r now/#ca::ctrlaltdel:\/sbin/shutdown -t3 -r now/" /etc/inittab
/sbin/init q
echo "Disable ctrlaltdel three key to reboot system.------>OK"
sleep 3
}

#Add users and set permissions in sudo //添加SA用戶並設置sudo權限
initAddUser() {
cat << EOF
+--------------------------------------------------------------+
+------Welcome to Add  users and set permissions in sudo.------+
+--------------------------------------------------------------+
EOF

datetmp=`date +"%Y-%m-%d_%H-%M-%S"`
\cp /etc/sudoers /etc/sudoers.${datetmp}
saUserArr=(test test1 test2)
groupadd -g 901 sa

for((i=0;i<${#saUserArr[@]};i++))
do
#add user //添加用戶
useradd -g sa -u 90${i} ${saUserArr[$i]}
#set password //設置密碼
echo "${saUserArr[$i]}123"|passwd ${saUserArr[$i]} --stdin
#set permissions //設置sudo權限
[ $(grep "${saUserArr[$i]} ALL=(ALL) NOPASSWD: ALL" /etc/sudoers|wc -l) -le 0 ] &&echo "${saUserArr[$i]} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
[ `grep "\%sa" /etc/sudoers|grep -v grep |wc -l` -ne 1 ] && \
echo "%sa ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
done 

/usr/sbin/visudo -c
[ $? -ne 0 ] && /bin/cp /etc/sudoers.${datetmp} /etc/sudoers && echo $"Sudoers not configured - exiting" && exit 1
echo "Add  users and set permissions in sudo.------>OK"
sleep 3
}

#Adjust the number of open files  //調整系統打開文件數
initOpenFiles() {
cat << EOF
+--------------------------------------------------------------+
+------    Welcome to Adjust the number of open files.   ------+
+--------------------------------------------------------------+
EOF

\cp /etc/security/limits.conf /etc/security/limits.conf.`date +"%Y-%m-%d_%H-%M-%S"`
sed -i ' /# End of file/i\*\t\t-\tnofile\t\t65535' /etc/security/limits.conf
ulimit -HSn 65535
echo "ulimit -HSn 65535" >> /etc/rc.local
echo "Adjust the number of open files.------>OK"
sleep 3 
}

#Set system time synchronization  //設置系統同步時間
initSysTime() {
cat << EOF
+--------------------------------------------------------------+
+------    Welcome to Set system time synchronization.   ------+
+--------------------------------------------------------------+
EOF
 
yum -y install ntp >>/dev/null 2>&1
ntpdate time.windows.com
echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1" >>/var/spool/cron/root

echo "Set system time synchronization.------>OK"
sleep 3
}

#Optimization of system kernel   //優化系統內核
initKernel() {
cat << EOF
+--------------------------------------------------------------+
+------     Welcome to Optimization of system kernel.    ------+
+--------------------------------------------------------------+
EOF

\cp /etc/sysctl.conf /etc/sysctl.conf.`date +"%Y-%m-%d_%H-%M-%S"`

cat>>/etc/sysctl.conf<<EOF
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 87380 16777216
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
EOF

/sbin/sysctl -p
echo "Optimization of system kernel.------>OK"
sleep 3
}

#Installation system tools  //安裝系統工具
initTool() {
cat << EOF
+--------------------------------------------------------------+
+------       Welcome to Installation system tools.      ------+
+------        <sysstat ntp net-snmp lrzsz rsync>        ------+
+--------------------------------------------------------------+
EOF

yum -y install sysstat ntp net-snmp lrzsz rsync >/dev/null 2>&1
echo "Installation system tools.------->OK"
sleep 3
}

#Prohibit the use of IPV6  //禁止使用IPV6
initIPV6() {
cat << EOF
+--------------------------------------------------------------+
+------        Welcome to Prohibit the use of IPV6.      ------+
+--------------------------------------------------------------+
EOF

\cp /etc/modprobe.conf /etc/modprobe.conf.`date +"%Y-%m-%d_%H-%M-%S"`
echo "alias net-pf-10 off" >> /etc/modprobe.conf
echo "alias ipv6 off" >> /etc/modprobe.conf
echo "Prohibit the use of IPV6.------>OK"
sleep 3
}

AStr="修改系統Yum源,設置字符編碼,關閉防火牆和Selinux,關閉不必要系統服務"
BStr="配置sshConfig,修改默認端口22->52113和禁止root登錄"
CStr="禁止Ctrl+Alt+Del三個鍵重啓系統"
DStr="添加SA用戶並設置sudo權限"
EStr="調整系統打開文件數"
FStr="設置系統同步時間"
GStr="優化系統內核"
HStr="安裝系統工具"
IStr="禁止使用IPV6"
JStr="一鍵初始化"

echo "+--------------------------------------------------------------+"
echo "+-----------------歡迎對系統進行初始化安全設置!---------------+"
echo "A:${AStr}"
echo "B:${BStr}"
echo "C:${CStr}"
echo "D:${DStr}"
echo "E:${EStr}"
echo "F:${FStr}"
echo "G:${GStr}"
echo "H:${HStr}"
echo "I:${IStr}"
echo "J:${JStr}"
echo "+--------------------------------------------------------------+"
echo "注意:如果沒有選擇初始化選項,20秒後將自動選擇一鍵初始化安裝!"
echo "+--------------------------------------------------------------+"

option="-1"
read -n1 -t20 -p "請選擇初始化選項【A-B-C-D-E-F-G-H-I-J】:" option

flag1=$(echo $option|egrep "\-1"|wc -l)
flag2=$(echo $option|egrep "[A-Ja-j]"|wc -l)

if [ $flag1 -eq 1 ];then
    option="K"
elif [ $flag2 -ne 1 ];then
    echo -e "\n\n請重新運行腳本,輸入從A--->J的字母!"
    exit 1
fi

echo -e "\n你選擇的選項是:$option\n"
echo "5秒之後開始安裝 ......"
sleep 5
case $option in
    A|a)
          initYum
          initI18n
          initFirewall
          initService
       ;;
    B|b)
          initSsh
       ;;
    C|c)
          initSafe
       ;;
    D|d)
          initAddUser
       ;;
    E|e)
          initOpenFiles
       ;;
    F|f)
          initSysTime
       ;;
   G|g)
          initKernel
       ;;
   H|h)
          initTool
       ;;
    I|i)
          initIPV6
       ;;
    J|j)
          initYum
          initI18n
          initFirewall
          initService
          initSsh
          initSafe
          initAddUser
          initOpenFiles
          initSysTime
          initKernel
          initTool
          initIPV6
       ;;
      *)
          echo "請輸入從A--->J的字母,謝謝!"
          exit
       ;;
esac   

使用方法:將其複製,保存爲一個shell文件,比如initSystem.sh。執行sh initSystem.sh,就可以使用該腳本了,如下圖所示:


 





BTW:如果大家認爲我寫的還可以,希望能給我的博客投個票,謝謝!O(∩_∩)O
https://blog.51cto.com/contest2012/7582
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章