Redhat7.x 优化脚本
操作系统版本 | IP | 版本 |
Redhat |
172.21.3.220 |
7.x |
Redhat7.x优化脚本
#!/bin/sh
. /etc/rc.d/init.d/functions
export LANG=zh_CN.UTF-8
#一级菜单
menu1()
{
clear
cat < /etc/locale.conf < /dev/null
echo '#firewall-cmd --state'
firewall-cmd --state
systemctl disable firewalld.service &> /dev/null
echo '#systemctl list-unit-files | grep firewalld'
systemctl list-unit-files | grep firewalld
action "完成禁用firewalld" /bin/true
echo "==========================================================="
sleep 5
}
#4.精简开机启动
chkset()
{
echo "=======================精简开机启动========================"
systemctl disable auditd.service
systemctl disable postfix.service
systemctl disable dbus-org.freedesktop.NetworkManager.service
echo '#systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"'
systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"
action "完成精简开机启动" /bin/true
echo "==========================================================="
sleep 2
}
#5.修改文件描述符
limitset()
{
echo "======================修改文件描述符======================="
echo '* - nofile 65535'>/etc/security/limits.conf
ulimit -SHn 65535
echo "#cat /etc/security/limits.conf"
cat /etc/security/limits.conf
echo "#ulimit -Sn ; ulimit -Hn"
ulimit -Sn ; ulimit -Hn
action "完成修改文件描述符" /bin/true
echo "==========================================================="
sleep 2
}
#6.安装常用工具及修改yum源
yumset()
{
echo "=================安装常用工具及修改yum源==================="
yum install wget -y &> /dev/null
if [ $? -eq 0 ];then
cd /etc/yum.repos.d/
\cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
ping -c 1 mirrors.aliyun.com &> /dev/null
if [ $? -eq 0 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
sed -i 's#$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
else
echo "无法连接网络"
exit $?
fi
else
echo "wget安装失败"
exit $?
fi
yum -y install lsof lrzsz vim lrzsz tree nmap nc sysstat &> /dev/null
action "完成安装常用工具及修改yum源" /bin/true
echo "==========================================================="
sleep 2
}
#7. 优化系统内核
kernelset()
{
echo "======================优化系统内核========================="
chk_nf=`cat /etc/sysctl.conf | grep conntrack |wc -l`
if [ $chk_nf -eq 0 ];then
cat >>/etc/sysctl.conf< /dev/null
if [ $? -eq 0 ];then
/usr/sbin/ntpdate time.windows.com
echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com &>/dev/null" >> /var/spool/cron/root
else
echo "ntpdate安装失败"
exit $?
fi
action "完成设置时间同步" /bin/true
echo "==========================================================="
sleep 2
}
#11. history优化
historyset()
{
echo "========================history优化========================"
chk_his=`cat /etc/profile | grep HISTTIMEFORMAT |wc -l`
if [ $chk_his -eq 0 ];then
cat >> /etc/profile <<'EOF'
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk '{print $NF}'|sed -r 's#[()]##g'`]: "
#记录shell执行的每一条命令
export PROMPT_COMMAND='\
if [ -z "$OLD_PWD" ];then
export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)";
fi;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'
EOF
source /etc/profile
else
echo "优化项已存在。"
fi
action "完成history优化" /bin/true
echo "==========================================================="
sleep 2
}
#控制函数
main()
{
menu1
case $num1 in
1)
localeset
selinuxset
firewalldset
chkset
limitset
yumset
kernelset
sshset
restartset
ntpdateset
historyset
;;
2)
menu2
case $num2 in
1)
localeset
;;
2)
selinuxset
;;
3)
firewalldset
;;
4)
chkset
;;
5)
limitset
;;
6)
yumset
;;
7)
kernelset
;;
8)
sshset
;;
9)
restartset
;;
10)
ntpdateset
;;
11)
historyset
;;
12)
main
;;
13)
exit
;;
*)
echo 'Please select a number from [1-13].'
;;
esac
;;
3)
exit
;;
*)
echo 'Err:Please select a number from [1-3].'
sleep 3
main
;;
esac
}
main $*