centos 7.x 64位 基础配置shell脚本优化

centos 7.x 64位 基础配置shell脚本优化

将上次的shell脚本,模块化优化。 

#########################################################################
# File Name: envconfiguration.sh                                        #  
# Author: dinmin                                                        #
# mail: 103482702@qq.com                                                #
# this script is only for CentOS 7.x                                    #
# Created Time: 2021年11月26日 星期五 14时53分01秒                      #
#########################################################################
#!/bin/bash
function core_check(){
platform=`uname -i`
if [ $platform != "x86_64" ]
then
	echo "此脚本只能应用在64位平台系统!"
	exit 1
fi
echo "平台OK!"
cat << EOF
+---------------------------------------------------------------------------------------------------+
|                                     your system is CentOS7 X86_64                                 |
|                                         start optimizing......                                    |
+---------------------------------------------------------------------------------------------------+
EOF
}

function dns_seetings(){
cat >> /etc/resolv.conf <<EOF
nameserver 114.114.114.114
EOF
}

function yum_source_seetings(){
yum install wget -y
mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
if [ $? -eq 0 ]
then
	yum clean all
	yum makecache
else
	echo "替换yum源,不成功请检查!!!"
	exit 1
fi
}
function time_sync(){
yum install ntp -y
/usr/sbin/ntpdate ntp1.aliyun.com
echo "* 4 * * *" /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1 >> /var/spool/cron/root
systemctl restart crond.service
}

function modify_hostname(){
read -p "请输入主机名称:" name
hostnamectl set-hostname $name
if [ $? -eq 0 ]
then
	echo "主机名修改完成!您的主机名是:$name"
else
	echo "主机名修改失败!请检查!"
	exit 1
fi
}

function file_limit(){
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
*     soft     nofile    655350
*     hard     nofile    655350
EOF
}

function sshd_settings(){
#设置ssh
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes /UseDNS no/' /etc/ssh/sshd_config
sed -i 's/#MaxAuthTries 6/MaxAuthTries 5/' /etc/ssh/sshd_config
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 900/' /etc/ssh/sshd_config
systemctl restart sshd.service
}

function SELINUX_seetings(){
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
}

function update_soft(){
#升级软件
yum -y update
}

function restart(){
cat << EOF
+--------------------------------------------------------------------------------------------------+
|                                             优化配置完成                                         |
|                                            5S后重启服务器!                                       |
+--------------------------------------------------------------------------------------------------+
EOF
sleep 5
echo -e "\n\033[31m主机开始重启!!!\033[0m\n"
shutdown -r now
}

function main(){
	dns_seetings
	yum_source_seetings
	time_sync
	modify_hostname
	sshd_settings
	SELINUX_seetings
	update_soft
	restart
}

function mainmenu(){
cat << EOF
+---------------------------------------------------------------------------------------------------+
|                                             1.主机DNS设置                                         |
|                                             2.yum源aliyun设置                                     |
|                                             3.同步系统时间设置                                    |
|                                             4.修改主机名称                                        |
|                                             5.SSHD设置                                            |
|                                             6.关闭SELINUX                                         |
|                                             7.更新所有软件                                        |
|                                             8.重启服务器                                          |
|                                             9.新服务器一键设置                                    |
+---------------------------------------------------------------------------------------------------+
EOF

read -p "请输入你所选择要进行的设置:" menunum
case $menunum in 
    1) dns_seetings
	;;
	2) yum_source_seetings
	;;
	3) time_sync
	;;
	4) modify_hostname
	;;
	5) sshd_settings
	;;
	6) SELINUX_seetings
	;;
	7) update_soft
	;;
	8) restart
	;;
	9) main
    ;;
esac
}
core_check
mainmenu
posted @ 2021-11-28 20:44  清风6661  阅读(62)  评论(0)    收藏  举报