设为首页 收藏本站
查看: 612|回复: 0

[经验分享] 一键优化系统脚本 for centos6.x

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-17 13:09:24 | 显示全部楼层 |阅读模式
#!/bin/bash
#set env
export LANG="en_US.UTF-8"
export PATH=$PATH:/bin/sbin:/usr/sbin
#whether root to running user
if [[ $(whoami) != root ]];then
   echo "please su - root run the script."
fi
SERVICE=`which service`
CHKCONFIG=`which chkconfig`
. /etc/init.d/functions

#install packages
installTmout(){
   echo "TMOUT 180" >> /etc/profile\
   [ $? -eq 0 ] && action "TMOUT is ok" /bin/true || exit 1
}

installTool(){
   yum -y install sysstat ntp net-snmp vmstat lrzsz rsync > /dev/vull 2>&1
   [ $? -eq 0 ] && action "Tools is ok" /bin/true || exit 1
}

#update the system lang
initI18n(){
   cat /etc/sysconfig/i18n > /etc/sysconfig/i18n.bak
   echo "LANG="en_US.UTF-8"" >> /etc/sysconfig/i18n
   source /etc/sysconfig/i18n
   [ $? -eq 0 ] && action "LANG is ok" /bin/true || exit 1
   sleep 1
}

#start iptables
initIptables(){
   cat /etc/selinux/config > /etc/selinux/config.bak
   setenforce 0
   sleep 1
}

#close not need to service
initService(){
   export LANG="en_US.UTF-8"
   for i in `chkconfig --list | grep -i "3:on" | awk '{print $1}'`;do chkconfig --level 3 $i off; done
   for i in crond network rsyslog sshd;do chkconfig --level 3 $i on;done
   [ $? -eq 0 ] && action "Service is ok" /bin/true || exit 1
   sleep 1
}

#youhua for ssh
initSsh(){
   cat /etc/ssh/sshd_config > /etc/ssh/sshd_config.bak
   sed -i 's/#Port 22/Port 22022/' /etc/ssh/sshd_config
   sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/' /etc/ssh/sshd_config
   sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
   /etc/init.d/sshd reload > /dev/null 2 >& 1
   [ $? -eq 0 ] && action "Ssh is ok" /bin/true || exit 1
}

#sync time
syncSystemTime(){
   ping www.baidu.com -c 1 > /dev/null 2 >& 1
   if [ $? -eq 0 -a `grep pool.ntp.org /var/spool/cron/root | grep -v grep | wc -l` -lt 1 ]; then
      echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com > /dev/null 2>&1" >> /var/spool/cron/root
          [ $? -eq 0 ] && action "SystemTime is ok" /bin/true || exit 1
   fi
}

#change file inode
openFiles(){
   cat /etc/security/limits.conf > /etc/security/limits.conf.bak
   echo "ulimit -HSn 65535" >> /etc/security/limits.conf
   sysctl -p
   [ $? -eq 0 ] && action "limit is ok" /bin/true || exi 1
   sleep 1
}

#youhua kernel
openKernel(){
   cat /etc/sysctl.conf > /etc/sysctl.conf.bak
   cat >> /etc/sysctl.conf << EOF
    net.ipv4.tcp_tw_reuse = 1  
        net.ipv4.tcp_tw_recycle = 1  
        net.ipv4.ip_local_port_range = 102465000  
        net.ipv4.tcp_max_syn_backlog = 8192  
        net.ipv4.tcp_max_tw_buckets = 5000  
        net.ipv4.tcp_mem = 192000 300000 732000
        net.ipv4.tcp_rmem = 51200 131072 204800
        net.ipv4.tcp_wmem = 51200 131072 204800
        net.ipv4.tcp_orphan_retries = 3
        net.ipv4.tcp_syn_retries = 3
        net.ipv4.tcp_synack_retries = 3
        net.ipv4.tcp_retries2 = 5
        net.ipv4.tcp_fin_timeout = 30
        net.ipv4.tcp_max_orphans = 2000
        net.ipv4.tcp_syncookies = 1
        net.ipv4.tcp_tw_reuse = 1
        net.ipv4.tcp_tw_recycle = 1
        net.ipv4.tcp_keepalive_time = 20
        net.ipv4.tcp_keepalive_intvl = 5
        net.ipv4.tcp_keepalive_probes = 2
        vm.dirty_expire_centisecs = 1500
        vm.dirty_writeback_centisecs = 1000
        vm.dirty_ratio = 20
        vm.dirty_background_ratio = 100
        vm.min_free_kbytes=409600
        vm.vfs_cache_pressure=200
        vm.swappiness = 40
   EOF
   /sbin/sysctl -p && action "kernel is ok" /bin/true || action "kernel is faile" /bin/false
   sleep 1
}


#init_snmp
init_snmp(){
   cat /etc/snmp/snmpd.conf > /etc/snmp/snmpd.conf.bak
   sed -i 's/#view all/view all/' /etc/snmp/snmpd.conf
   sed -i 's/#access MyROGroup/access MyROGroup/' /etc/snmp/snmpd.conf
   ${CHKCONFIG} snmpd on
   ${SERVICE} snmpd start
   [ $? -eq 0 ] && action "snmpd is starting" /bin/true \
   || action "snmpd is faile" /bin/false
}

installTool
initI18n
initSelinux
initService
initSsh
syncSystemTime
openFiles
openKernel
init_snmp



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-30424-1-1.html 上篇帖子: Python结合selenium自动领取无忧币的脚本 下篇帖子: python将不同的文件名上传到不同的服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表