Linux One-click Optimization script

Source: Internet
Author: User

#!/bin/bash############################################################### file name: /var/www/ Html/ks_config/optimization.sh# version: v1.0# author: ersa ma# organization:  http://iersa.blog.51cto.com/# Created Time : 2016-11-14 22:26:08#  description: linux system initialization# e.g: /bin/sh optimization.sh  172.16.1.41##############################################################. /etc/init.d/functionsif [  $# -ne 1 ];thenMsg  "please enter the ip address passed  to the script! " Exit -1fiipaddr=$1# defined result functionfunction msg () {         if [ $? -eq 0 ];then           action  "$"  /bin/true        else           action  "$"  /bin/false         fi}# defined time synchronization functionsfunction time () {         echo  "#time  sync by ersa at $ ( date +%f) " >>/var/spool/cron/root        echo " * * 5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null '  >> /var/spool/cron/root        msg  "Time Synchronization"}#  defined ip functionfunction configip () {        # suffix= ' ifconfig eth1|awk -f  ' [ .] + "  ' nr==2 {print $6} ' suffix= ' echo  $ipaddr  |awk -F ". "   ' {print $4} ' Cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<eofdevice=eth0type=ethernetonboot=yesnm_controlled=yesbootproto=noneuserctl=noipv6init=noipaddr=10.0.0. $SuffixNETMASK = 255.255.255.0dns2=223.5.5.5gateway=10.0.0.2dns1=10.0.0.2name= "System eth0" EOF         Msg  "Config eth0" cat >/etc/sysconfig/network-scripts/ifcfg-eth1  <<eofdevice=eth1type=ethernetonboot=yesnm_controlled=yesbootproto=noneuserctl=noipv6init=noipaddr= 172.16.1. $SuffixNETMASK =255.255.255.0dns2=223.5.5.5gateway=10.0.0.2dns1=10.0.0.2name= "System eth1" EOF         Msg  "Config eth1"} #Defined  cloned after  internet optimization functionfunction clonednetworkopti ()  {sed -i  '/UUID/ D;/hwaddr/d '  /etc/sysconfig/network-scripts/ifcfg-eth*>/etc/udev/rules.d/70-persistent-net.rulesmsg   "Clone online optimization"}# defined yum source functionsfunction  Updateyumsource () {        yumdir=/etc/yum.repos.drepodir=http://mirrors.aliyun.com/repo/ centos-6.repoepeldir=http://mirrors.aliyun.com/repo/epel-6.repo         [ -f  "$YumDir/centos-base.repo"  ] && cp  $YumDir/centos-base.repo{, .ori}          #wget  -O  $YumDir/centos-base.repo  /HTTP $Ip: $Port/$ConfigDir/centos-base.repo &>/dev/null &&          #wget  -O  $YumDir/epel.repo http://$Ip: $Port/$ConfigDir/epel.repo  &>/dev/null &&wget -O  $YumDir/centos-base.repo  $repoDir  &>/dev/ null &&wget -o  $YumDir/epel.repo  $epelDir  &>/dev/null &&# Flush the Yum cache and set up Yum cache yum clean all && yum makecache &&# Then use the following command to update the system to the latest # rpm --import /etc/pki/rpm-gpg/rpm-gpg-key*        #导入签名KEY到RPM # yum  upgrade  -y      #更新系统内核到最新         Msg  YUM  source "} #Install  the base package  (tree nmap sysstat lrzsz  Telnet dos2unix) function installbasepackage ()  {yum  install -y tree  nmap sysstat lrzsz dos2unix telnet &>/dev/null &&msg  "Base  packages "} #Lock  critical file systems () function lockcriticalfile ()  {chattr  +i /etc/passwd &&chattr +i /etc/inittab &&chattr +i / etc/group &&chattr +i /etc/shadow &&chattr +i /etc/gshadow  &&Msg  "Lock files"}# defined hide the system version number  functionsfunction  Hideversion () {        [ -f  "/etc/issue"  ] & & >/etc/issue        Msg  "Hide issue"           [ -f  "/etc/issue.net"  ] &&  > /etc/issue.net        Msg  "Hide issue.net"}#  Defined open files functionsfunction openfiles () {         [ -f  "/etc/security/limits.conf"  ] && {         echo  ' *  -  nofile  65535 '  >> /etc/ security/limits.conf        msg  "Open files"          }} #Defined  Stop iptables Functionsfunction  Stopiptables ()  {[ -f  "/etc/init.d/iptables " ] && {/etc/init.d/iptables stopchkconfig iptables  offmsg  "Stop iptables"}} #Defined  close selinux functionsfunction closeselinux () { [ -f  "/etc/selinux/config"  ] && {sed -i  "s#selinux=enforcing# Selinux=disabled#g " /etc/selinux/configsetenforce 0Msg " Close selinux "}} #Defined   Modify the remote login configuration on the ssh server function  modifysshconfig () {[ -f  "/etc/ssh/sshd_config"  ] && {cp /etc/ssh/ sshd_config /etc/ssh/sshd_config.orised  ' 13i port 52113\npermitrootlogin no\ Npermitemptypasswords no\nusedns no\ngssapiauthentication no '  /etc/ssh/sshd_config/etc/ init.d/sshd reloadmsg  "Modify ssh config"}} #Kernel  parameter  Optimizationfunction kernelparameteropti ()  {cat&Nbsp;>>/etc/sysctl.conf <<eofnet.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_reuse  = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive _time =600net.ipv4.ip_local_port_range = 4000    65000net.ipv4.tcp_max_syn_ backlog = 16384net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.route.gc_timeout =  100net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_synack_retries = 1net.core.somaxconn =  16384net.core.netdev_max_backlog = 16384net.ipv4.tcp_max_orphans = 16384# The following parameters are optimized for iptables firewall, the firewall does not have a meeting prompt, you can ignore Net.ipv4.ip_conntrack_max = 25000000net.ipv4.netfilter.ip_ conntrack_max = 25000000net.ipv4.netfilter.ip_conntrack_tcp_timeout_established =  180net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120net.ipv4.netfilter.ip_conntrack_tcp_ Timeout_close_wait = 60net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120eof# make the configuration file effective sysctl –p &>/dev/null  &&Msg  "Kernel parameter optimization"}# defined system startup  Services functionsfunction boot () {        for oldboy  in  ' chkconfig --list|grep  ' 3:on ' |awk  ' {print $1} ' |grep -ve  ' crond| Network|rsyslog|sshd|sysstat "'            do             chkconfig  $oldboy  off         done        Msg  "Boot config"} # defined main functionsfunction main () { Configipclonednetworkoptitimeupdateyumsourceinstallbasepackagecloseselinuxstopiptablesopenfilesbootkernelparameteropti           &nbSp;     hideversion       lockcriticalfile}main 


Linux One-click Optimization script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.