#/bin/bash
# ------------------------------------------
# Filename: 服务器系统配置初始化脚本.sh
# Version: 1.1
# Date: 2023/09/24
# Author: tanghao
# Email: 2826588067@qq.com
# Description: This is the first script
# Copyright: 2023 tanghao
# License: GPL
# ------------------------------------------
# 设置时区并同步时间
function date_1 (){
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime & > /dev/null
if ! crontab -l |grep ntpdate &>/dev/null ; then
(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
fi
}
# 禁用selinux
function selinux_2(){
sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
}
# 关闭防火墙
function firewalld_3(){
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
systemctl stop firewalld
systemctl disable firewalld
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
service iptables stop
chkconfig iptables off
fi
}
# 历史命令显示操作时间
function command_date_4(){
if ! grep HISTTIMEFORMAT /etc/bashrc; then
echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
. /etc/bashrc
fi
}
# SSH超时时间
function ssh_fullDate_5(){
if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
echo "export TMOUT=600" >> /etc/profile
fi
}
# 禁止root远程登录
function down_rootSsh_6(){
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
}
# 禁止定时任务向发送邮件
function crontab_mali_7(){
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
}
# 设置最大打开文件数
function openFileMax_8(){
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 655350
* hard nofile 655350
* soft nproc 655350
* hard nproc 655350
* soft core unlimited
* hard core unlimited
EOF
fi
}
# 系统内核优化
function ker_9(){
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
EOF
}
function swapLimit_10(){
# 减少SWAP使用
echo "0" > /proc/sys/vm/swappiness
}
#软件下载优化
function install_common_software_11(){
package="
sysstat
lrzsz
gcc
vim
telnet
tree
lsof
tcpdump
iotop
net-tools
bash-completion
vim-enhanced"
for i in $package ;do
rpm -q $i || yum -y -q install $i
done
}
#下载源优化
install_optimize_12(){
rpm -q wget || yum install -q -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
}
option="
设置时区并同步时间
禁用selinux
关闭防火墙
历史命令显示操作时间
SSH超时时间优化
禁止root远程登录
禁止定时任务向发送邮件
设置最大打开文件数
系统内核优化
减少SWAP使用
软件下载优化
下载源优化—阿里云
一键全部优化
退出
"
COLOR='echo -e \033[1;31m'
COLOR2='echo -e \033[1;32m'
END="\033[0m"
PS3="Please select the option you want to optimize 1-14 :"
select menu in $option ;do
case $REPLY in
1)
date_1
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
2)
selinux_2
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
3)
firewalld_3
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
4)
command_date_4
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
5)
ssh_fullDate_5
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
6)
down_rootSsh_6
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
7)
crontab_mali_7
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
8)
openFileMax_8
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
9)
ker_9
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
10)
swapLimit_10
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
11)
install_common_software_11
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
12)
install_optimize_12
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Fail $END
;;
13)
date_1
selinux_2
firewalld_3
command_date_4
ssh_fullDate_5
down_rootSsh_6
crontab_mali_7
openFileMax_8
ker_9
swapLimit_10
install_common_software_11
install_optimize_12
[ $? -eq 0 ] && $COLOR2 Success $END || $COLOR2 Success $END
;;
14)
exit
;;
*)
echo -e "\033[31m;1 你输入数字有误,请重新输入 \033[0m"
;;
esac
done