CentOS/RHEL安全配置腳本

Shell Code

#! /bin/bash

# 
# Name:     lintool
# Auther:   Jon Lau
# Version:  V1.1.6 (2019/02/14)
# Usage:    chmod +x lintool
#           ./lintool
# Update:   1. Add function for print file infomation
# 

Yum(){
    echo -e "\033[01;32mYum Setting...\033[0m"
    echo -e "\033[33mDelete repo files...\033[0m"
    rm -rfv /etc/yum.repos.d/*
    echo -e "\033[33mCreate repo file...\033[0m"
    case $1 in
        el6)
            echo [os] >> /etc/yum.repos.d/CentOS-6.10.repo
            echo name=os >> /etc/yum.repos.d/CentOS-6.10.repo
            echo baseurl=http://10.172.115.106/centos/6.10/os/ >> /etc/yum.repos.d/CentOS-6.10.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo [extras] >> /etc/yum.repos.d/CentOS-6.10.repo
            echo name=extras >> /etc/yum.repos.d/CentOS-6.10.repo
            echo baseurl=http://10.172.115.106/centos/6.10/extras/ >> /etc/yum.repos.d/CentOS-6.10.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo [updates] >> /etc/yum.repos.d/CentOS-6.10.repo
            echo name=updates >> /etc/yum.repos.d/CentOS-6.10.repo
            echo baseurl=http://10.172.115.106/centos/6.10/updates/ >> /etc/yum.repos.d/CentOS-6.10.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo [epel] >> /etc/yum.repos.d/CentOS-6.10.repo
            echo name=epel >> /etc/yum.repos.d/CentOS-6.10.repo
            echo baseurl=http://10.172.115.106/epel/6/ >> /etc/yum.repos.d/CentOS-6.10.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo [zabbix] >> /etc/yum.repos.d/CentOS-6.10.repo
            echo name=zabbix >> /etc/yum.repos.d/CentOS-6.10.repo
            echo baseurl=http://10.172.115.106/zabbix/4.0/rhel/6/ >> /etc/yum.repos.d/CentOS-6.10.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-6.10.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-6.10.repo
            ;;
        el7)
            echo [os] >> /etc/yum.repos.d/CentOS-7.5.repo
            echo name=os >> /etc/yum.repos.d/CentOS-7.5.repo
            echo baseurl=http://10.172.115.106/centos/7.5.1804/os/ >> /etc/yum.repos.d/CentOS-7.5.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo >> /etc/yum.repos.d/CentOS-7.5.repo
            echo [updates] >> /etc/yum.repos.d/CentOS-7.5.repo
            echo name=updates >> /etc/yum.repos.d/CentOS-7.5.repo
            echo baseurl=http://10.172.115.106/centos/7.5.1804/updates/ >> /etc/yum.repos.d/CentOS-7.5.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo >> /etc/yum.repos.d/CentOS-7.5.repo
            echo [extras] >> /etc/yum.repos.d/CentOS-7.5.repo
            echo name=extras >> /etc/yum.repos.d/CentOS-7.5.repo
            echo baseurl=http://10.172.115.106/centos/7.5.1804/extras/ >> /etc/yum.repos.d/CentOS-7.5.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo >> /etc/yum.repos.d/CentOS-7.5.repo
            echo [epel] >> /etc/yum.repos.d/CentOS-7.5.repo
            echo name=epel >> /etc/yum.repos.d/CentOS-7.5.repo
            echo baseurl=http://10.172.115.106/epel/7/ >> /etc/yum.repos.d/CentOS-7.5.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo >> /etc/yum.repos.d/CentOS-7.5.repo
            echo [zabbix] >> /etc/yum.repos.d/CentOS-7.5.repo
            echo name=zabbix >> /etc/yum.repos.d/CentOS-7.5.repo
            echo baseurl=http://10.172.115.106/zabbix/4.0/rhel/7/ >> /etc/yum.repos.d/CentOS-7.5.repo
            echo enable=1 >> /etc/yum.repos.d/CentOS-7.5.repo
            echo gpgcheck=0 >> /etc/yum.repos.d/CentOS-7.5.repo
            ;;
        *)
            ;;
    esac
    echo -e "\033[33mUpdate repolist...\033[0m"
    yum clean all && yum repolist all
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mYum repolist failed!\033[0m"
        return [-1]
    fi
}

InstallPackage(){
    echo -e "\033[01;32mInstall Packages...\033[0m"
    echo -e "\033[33mInstall vim...\033[0m"
    yum install -y vim-enhanced
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mInstall vim failed!\033[0m"
        return [-1]
    fi
    echo -e "\033[33mInstall bash-completion...\033[0m"
    yum install -y bash-completion
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mInstall bash-completion failed!\033[0m"
        return [-1]
    fi
    echo -e "\033[33mInstall openssh-clients...\033[0m"
    yum install -y openssh-clients
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mInstall openssh-clients failed!\033[0m"
        return [-1]
    fi
}

DisableService(){
    echo -e "\033[01;32mDisable Services...\033[0m"
    case $1 in
        el6)
            echo -e "\033[33mDisable iptables...\033[0m"
            service iptables stop && chkconfig iptables off
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mDisable iptables failed!\033[0m"
                return [-1]
            fi

            echo -e "\033[33mDisable postfix...\033[0m"
            service postfix stop && chkconfig postfix off
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mDisable postfix failed!\033[0m"
                return [-1]
            fi
            ;;
        el7)
            echo -e "\033[33mDisable firewalld postfix and NetworkManager...\033[0m"
            systemctl stop firewalld postfix NetworkManager && systemctl disable firewalld postfix NetworkManager
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mDisable firewalld postfix or NetworkManager failed!\033[0m"
                return [-1]
            fi
            ;;
        *)
            ;;
    esac
}

Ntp(){
    echo -e "\033[01;32mNtp Setting...\033[0m"
    echo -e "\033[33mInstall ntp...\033[0m"
    yum install -y ntp
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mInstall ntp failed!\033[0m"
        return [-1]
    fi
    echo -e "\033[33mEdit ntp profile ntp.conf...\033[0m"
    N=$(grep -E "^server 10.172.113.163$" /etc/ntp.conf | wc -l)
    if [[ ${N} -eq 0 ]]; then
        sed -i 's/^server/# server/g' /etc/ntp.conf
        echo "server 10.172.113.163" >> /etc/ntp.conf
        echo "server 10.173.173.163" >> /etc/ntp.conf
    fi
    case $1 in
        el6)
            echo -e "\033[33mStart ntpd...\033[0m"
            service ntpd start && chkconfig ntpd on
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mStart ntpd failed!\033[0m"
                return [-1]
            fi
            ;;
        el7)
            echo -e "\033[33mStart ntpd...\033[0m"
            systemctl start ntpd && systemctl enable ntpd
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mStart ntpd failed!\033[0m"
                return [-1]
            fi
            ;;
        *)
            ;;
    esac
}

DisableCtrlAltDelete(){
    echo -e "\033[01;32mDisable Ctrl-Alt-Delete...\033[0m"
    case $1 in
        el6)
            sed -i 's/^start/# start/g' /etc/init/control-alt-delete.conf
            sed -i 's/^exec/# exec/g' /etc/init/control-alt-delete.conf
            ;;
        el7)
            if [[ -f /usr/lib/systemd/system/ctrl-alt-del.target ]]; then
                mv -v /usr/lib/systemd/system/ctrl-alt-del.target{,.bak}
            fi
            ;;
        *)
            ;;
    esac
}

InitMod(){
    echo -e "\033[01;32mInit Mod Setting...\033[0m"
    echo -e "\033[33mSet multi-user default...\033[0m"
    case $1 in
        el6)
            sed -i 's/id:5/id:3/g' /etc/inittab
            ;;
        el7)
            systemctl set-default multi-user.target
            ;;
        *)
            ;;
    esac
}

EditProfile(){
    echo -e "\033[01;32mProfile Setting...\033[0m"
    N=$(grep -E "^TMOUT=600$" /etc/profile | wc -l)
    if [[ ${N} -eq 0 ]]; then
        echo >> /etc/profile
        echo -e "\033[33mSet time-out...\033[0m"
        echo "TMOUT=600" >> /etc/profile
        echo -e "\033[33mSet cmd history size...\033[0m"
        echo "HISTSIZE=3000" >> /etc/profile
        echo -e "\033[33mSet cmd history format...\033[0m"
        echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami`: "' >> /etc/profile
        source /etc/profile
    fi
}

Log(){
    echo -e "\033[01;32mLogrotate Setting...\033[0m"
    echo -e "\033[33mSet rotate weeks...\033[0m"
    sed -i 's/rotate 4/rotate 52/g' /etc/logrotate.conf
}

DisableRoot(){
    echo -e "\033[01;32mDisable Root Login...\033[0m"
    echo -e "\033[33mAdd user natalia and set password...\033[0m"
    N=$(grep -E "natalia" /etc/passwd | wc -l)
    if [[ ${N} -eq 0 ]]; then
        useradd -s /bin/bash -m natalia
    fi
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mAdd user natalia failed!\033[0m"
        return [-1]
    fi
    echo dpbg123. | passwd --stdin natalia
    echo -e "\033[33mDisable root login...\033[0m"
    N=$(grep -E "^PermitRootLogin no$" /etc/ssh/sshd_config | wc -l)
    if [[ ${N} -eq 0 ]]; then
        echo >> /etc/ssh/sshd_config
        echo "PermitRootLogin no" >> /etc/ssh/sshd_config
    fi
    echo -e "\033[33mRestart sshd service...\033[0m"
    case $1 in
        el6)
            service sshd restart && chkconfig sshd on
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mRestart sshd failed!\033[0m"
                return [-1]
            fi
            ;;
        el7)
            systemctl restart sshd && systemctl enable sshd
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mRestart sshd failed!\033[0m"
                return [-1]
            fi
            ;;
        *)
            ;;
    esac
}

DisableSELinux(){
    echo -e "\033[01;32mDisable SELinux...\033[0m"
    sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
}

ZabbixAgent(){
    echo -e "\033[01;32mZabbix Agent Setting...\033[0m"
    echo -ne "\033[01;33mPlease enter the Zabbix Server IP: \033[0m"
    read ZBXIP
    echo -e "\033[33mInstall zabbix-agent...\033[0m"
    yum install -y zabbix-agent
    if [[ $? -ne 0 ]]; then
        echo -e "\033[01;31mInstall zabbix-agent failed!\033[0m"
        return [-1]
    fi
    echo -e "\033[33mSome settings on zabbix_agentd.conf...\033[0m"
    sed -i "s/LogFileSize=0/LogFileSize=1/g" /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^Server=127.0.0.1/Server=${ZBXIP}/g" /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^ServerActive=127.0.0.1/ServerActive=${ZBXIP}/g" /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^Hostname=Zabbix server/Hostname=${HOSTNAME}/g" /etc/zabbix/zabbix_agentd.conf
    echo -e "\033[33mStart zabbix-agent service...\033[0m"
    case $1 in
        el6)
            service zabbix-agent start && chkconfig zabbix-agent on
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mStart zabbix-agent failed!\033[0m"
                return [-1]
            fi
            ;;
        el7)
            systemctl start zabbix-agent && systemctl enable zabbix-agent
            if [[ $? -ne 0 ]]; then
                echo -e "\033[01;31mStart zabbix-agent failed!\033[0m"
                return [-1]
            fi
            ;;
        *)
            ;;
    esac
}

PrintInfo(){
    NAME='lintool'
    AUTHER='Jon Lau'
    VER='V1.1.6 (2019/02/14)'
    echo -e "\033[33mName:     ${NAME}\033[0m"
    echo -e "\033[33mAuther:   ${AUTHER}\033[0m"
    echo -e "\033[33mVersion:  ${VER}\033[0m"
    echo -e "\033[33mUsage:    chmod +x lintool\033[0m"
    echo -e "\033[33m          ./lintool\033[0m"
}

DoFunc(){
    COLS=$(stty size | awk '{print $2}')
    NN=$(echo -n $1 | wc -c)
    NS=$((${COLS} - ${NN} - 9))
    NF=$((${COLS} - ${NN} - 8))
    $1 $2 2> /dev/null
    if [[ $? -eq 0 ]]; then
        echo -n "$1"
        for i in $(seq 1 ${NS}); do
            echo -n " "
        done
        echo -e "[\033[00;32mSuccess\033[0m]"
    else
        echo -n "$1"
        for i in $(seq 1 ${NF}); do
            echo -n " "
        done
        echo -e "[\033[00;31mFaild\033[0m]"
    fi
}

SettingAll(){
    if [[ -f /root/.SETTINGALL ]]; then
        echo -e "\033[01;31mHas been execed, do nothing!\033[0m"
    else
        touch /root/.SETTINGALL
        DoFunc Yum $1
        DoFunc InstallPackage $1
        DoFunc DisableService $1
        DoFunc Ntp $1
        DoFunc DisableCtrlAltDelete $1
        DoFunc InitMod $1
        DoFunc EditProfile $1
        DoFunc Log $1
        DoFunc DisableRoot $1
        DoFunc DisableSELinux $1
        DoFunc ZabbixAgent $1
    fi
}

Print_Menu(){
    COLS=$(stty size | awk '{print $2}')
    N=$(( (${COLS} - 42) / 2 ))
    echo
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m -------------- Linux Menu -------------- \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      v) Print Version                    \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      q) Quit                             \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      0) Setting All                      \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      1) Yum Setting                      \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      2) Install Packages                 \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      3) Disable Services                 \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      4) Ntp Setting                      \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      5) Disable Ctrl-Alt-Delete          \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      6) Init Mod                         \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      7) Profile Setting                  \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      8) Log Setting                      \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m      9) Disable Root                     \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m     10) Disable SELinux                  \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m     11) Zabbix Agent                     \033[0m"
    for i in $(seq 1 ${N}); do echo -n " "; done; echo -e "\033[37;41m ---------------------------------------- \033[0m"
    echo; for i in $(seq 1 ${N}); do echo -n " "; done; echo -ne "\033[33mEnter your choice: \033[0m"
}

Menu(){
    while [ 1 -eq 1 ]; do
        RELEASE=$(uname -r | awk -F '.' '{i=NF-1; print $i}')
        Print_Menu
        read key
        case $key in
            0)
                SettingAll ${RELEASE}
                ;;
            1)
                DoFunc Yum ${RELEASE}
                ;;
            2)
                DoFunc InstallPackage ${RELEASE}
                ;;
            3)
                DoFunc DisableService ${RELEASE}
                ;;
            4)
                DoFunc Ntp ${RELEASE}
                ;;
            5)
                DoFunc DisableCtrlAltDelete ${RELEASE}
                ;;
            6)
                DoFunc InitMod ${RELEASE}
                ;;
            7)
                DoFunc EditProfile ${RELEASE}
                ;;
            8)
                DoFunc Log ${RELEASE}
                ;;
            9)
                DoFunc DisableRoot ${RELEASE}
                ;;
            10)
                DoFunc DisableSELinux ${RELEASE}
                ;;
            11)
                DoFunc ZabbixAgent ${RELEASE}
                ;;
            v | V)
                DoFunc PrintInfo ${RELEASE}
                ;;
            q | Q)
                break
                ;;
            *)
                echo -e "\033[31m[!] Please enter [0-11]\033[0m"
                ;;
        esac
    done
}

Menu
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末圃庭,一起剝皮案震驚了整個濱河市蟹演,隨后出現(xiàn)的幾起案子播演,更是在濱河造成了極大的恐慌,老刑警劉巖生年,帶你破解...
    沈念sama閱讀 212,294評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異盔夜,居然都是意外死亡荠锭,警方通過查閱死者的電腦和手機(jī)呼股,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評論 3 385
  • 文/潘曉璐 我一進(jìn)店門耕魄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人彭谁,你說我怎么就攤上這事吸奴。” “怎么了?”我有些...
    開封第一講書人閱讀 157,790評論 0 348
  • 文/不壞的土叔 我叫張陵则奥,是天一觀的道長考润。 經(jīng)常有香客問我,道長读处,這世上最難降的妖魔是什么糊治? 我笑而不...
    開封第一講書人閱讀 56,595評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮罚舱,結(jié)果婚禮上井辜,老公的妹妹穿的比我還像新娘。我一直安慰自己管闷,他們只是感情好粥脚,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,718評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著包个,像睡著了一般刷允。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碧囊,一...
    開封第一講書人閱讀 49,906評論 1 290
  • 那天树灶,我揣著相機(jī)與錄音,去河邊找鬼糯而。 笑死破托,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的歧蒋。 我是一名探鬼主播土砂,決...
    沈念sama閱讀 39,053評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼谜洽!你這毒婦竟也來了萝映?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,797評論 0 268
  • 序言:老撾萬榮一對情侶失蹤阐虚,失蹤者是張志新(化名)和其女友劉穎序臂,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體实束,經(jīng)...
    沈念sama閱讀 44,250評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡奥秆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,570評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了咸灿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片构订。...
    茶點(diǎn)故事閱讀 38,711評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖避矢,靈堂內(nèi)的尸體忽然破棺而出悼瘾,到底是詐尸還是另有隱情囊榜,我是刑警寧澤,帶...
    沈念sama閱讀 34,388評論 4 332
  • 正文 年R本政府宣布亥宿,位于F島的核電站卸勺,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏烫扼。R本人自食惡果不足惜曙求,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,018評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望映企。 院中可真熱鬧圆到,春花似錦、人聲如沸卑吭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,796評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽豆赏。三九已至挣菲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間掷邦,已是汗流浹背白胀。 一陣腳步聲響...
    開封第一講書人閱讀 32,023評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抚岗,地道東北人或杠。 一個月前我還...
    沈念sama閱讀 46,461評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像宣蔚,于是被迫代替她去往敵國和親向抢。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,595評論 2 350