• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

【linux运维】一键优化linux系统shell脚本(内含下载源码包)

技术栈 lixian 4年前 (2020-01-09) 2396次浏览 1个评论 扫描二维码
文章目录[隐藏]

前言

当我们装完系统之后,需要做一些优化,比如安装一些常用的软件,关闭防火墙、selinux等并设置开机自启,还有一些内核的优化,设置时间同步等等等。。
但这样设置太过于繁琐,博主在学习优化时写了一个脚本,只要执行脚本,一分钟之内就可以完成系统的优化,非常的方便,感兴趣的一起来看看吧!
【linux运维】一键优化linux系统shell脚本(内含下载源码包)

sh脚本

[root@lixian ~]# cat optimize.sh 
# Version: V1.0
# Author: Lixian
# Organization: https://www.lixian.fun
# Created Time : 2020-01-09 18:35:35
# Description:
##############################################################
. /etc/init.d/functions
cat <<END 
+----------------------------------+
|                                  |
|     This  is  a  系统优化        |
|                                  |
|     一键优化系统 最强版          |
|				   |
|	显哥哥专用shell脚本	   |
|				   |
|	请输入 ok 开始安装	   |
|				   |
+----------------------------------+
END
#node 输入指令开始优化系统. 
  read  -p "请你输入指令 ok 开始优化系统:" NUM
  if [ "$NUM" != "ok" ];then
    action "对不起,您输入的指令有误!!!" /bin/false
    exit 1
  fi
#node 开始优化.
 [ "$NUM" = "ok" ] && {
#优化更新yum仓库
	echo  "开始优化linux系统,请耐心等待..."
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null && curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
if [ "$?" -eq 0 ];then
          action "优化yum源仓库更新成功!" /bin/true
          else
          action "对不起,yum源更新失败,请检查脚本或者手动更新yum仓库!" /bin/false
          exit 1
fi
#永久关闭selinux
sed -i 's#^SELINUX=.*#SELINUX=disabled#g' /etc/selinux/config
if [ "$?" -eq 0 ];then
	action "永久关闭selinux模块成功!" /bin/true
	else
	action "对不起,永久关闭selinux失败,请检查脚本或者手动关闭selinux!" /bin/false
	exit 1
fi
#关闭防火墙并加入开机自启
systemctl stop firewalld && systemctl disable firewalld  &> /dev/null
if [ "$?" -eq 0 ];then
        action "关闭firewalld防火墙成功!" /bin/true
        else
        action "对不起,关闭防火墙失败,请检查脚本或者手动关闭防火墙!" /bin/false
        exit 1
fi
#同步系统时间设置成定时任务
yum install -y ntpdate &> /dev/null && echo '*/3 * * * * /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null' >> /var/spool/cron/root
if [ "$?" -eq 0 ];then
        action "同步系统时间和加入定时任务完成..." /bin/true
        else
        action "对不起,同步系统时间和定时任务失败,请检查脚本或者手动添加定时任务!" /bin/false
        exit 1
fi
#关闭NetworkManager
systemctl stop NetworkManager && systemctl disable NetworkManager &> /dev/null
if [ "$?" -eq 0 ];then
        action "关闭NetworkManager成功!" /bin/true
        else
        action "对不起,关闭NetworkManager失败,请检查脚本或者手动关闭NetworkManager!" /bin/false
        exit 1
fi
#加大文件描述符数量
echo  '*        -      nofile     65535' >> /etc/security/limits.conf
if [ "$?" -eq 0 ];then
	action "加大文件描述符数量成功!" /bin/true
	else
	action "对不起,加大文件描述符数量失败,请检查脚本或者手动加大描述符数量!"
	exit 1
fi
#禁止DNS反向解析
sed -i  's#^\#UseDNS.*#UseDNS no#g'  /etc/ssh/sshd_config
if [ "$?" -eq 0 ];then
        action "禁止DNS反向解析完成!" /bin/true
        else
        action "对不起,禁止DNS反向解析失败,请检查脚本或者手动禁止DNS解析!"
        exit 1
fi
#禁止GSS认证
sed -i  's#^GSSAPIA.*#GSSAPIAuthentication no#g'  /etc/ssh/sshd_config && systemctl  restart  sshd
if [ "$?" -eq 0 ];then
        action "禁止GSS认证成功!" /bin/true
        else
        action "对不起,禁止GSS认证失败,请检查脚本或者手动禁止GSS认证!"
        exit 1
fi
#内核优化
cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
EOF
sysctl  -p &> /dev/null

if [ "$?" -eq 0 ];then
        action "内核优化成功!" /bin/true
        else
        action "对不起,内核优化失败,请检查脚本或者手动进行内核优化!"
        exit 1
fi
#设置别名及环境变量
cat>>/etc/profile.d/color.sh<<"EOF"
alias ll='ls -l --color=auto --time-style=long-iso'
PS1='[\[\e[34;40m\]\u\[\e[37;40m\]@\h \[\e[31;40m\]\w\[\e[37;40m\]]\$\[\e[0m\] '
export HISTTIMEFORMAT='%F-%T '
EOF
source  /etc/profile

if [ "$?" -eq 0 ];then
        action "设置别名及环境变量成功!" /bin/true
        else
        action "对不起,设置别名和环境变量失败,请检查脚本或者手动设置!"
        exit 1
fi
#安装常用的软件
yum -y install tree nmap sysstat lrzsz telnet bash-completion bash-completion-extras vim lsof net-tools rsync ntpdate nfs-utils wget &> /dev/null
if [ "$?" -eq 0 ];then
        action "安装常用软件成功!" /bin/true
        else
        action "对不起,安装常用软件失败,请检查脚本或者手动安装常用软件!"
        exit 1
fi
echo "恭喜你!优化系统成功!开始干吧!"
}

脚本下载

文件下载

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:【linux运维】一键优化linux系统shell脚本(内含下载源码包)
喜欢 (34)

您必须 登录 才能发表评论!

(1)个小伙伴在吐槽
  1. I don't think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.