Linux System Optimization Script implementation

Source: Internet
Author: User

The script content at the bottom of the document, change the script suffix to. SH, put it in the system (centos6.x) directly execute can, there is no trouble to execute tell me, thank you!


#!/bin/bash

. /etc/init.d/functions


If ["$UID"-ne 0];then
echo "You should change to root and then run this script,please enter the root password:"
Su-root
Fi



Retval=0
Dir= "/tmp/result_${server_ip}.txt"

Read-p "Please enter your server IP:" server_ip


Result () {
[$RETVAL-eq 0] && echo "is OK." >> ${dir} | | echo "is false." >> ${dir}
}

Create_user () {
ID Nginx &>/dev/null
[$?-ne 0] | | Userdel-r Nginx >/dev/null 2>&1
GROUPADD-G 501 Nginx >/dev/null 2>&1
Useradd-u 501-r-G nginx-s/sbin/nologin nginx >/dev/null 2>&1
Retval=$?
Result Create_user
}

Fstab_opt () {
Sed-i-E '/\/tmp/s/defaults/defaults,nosuid,noexec,nodev/'-e '/\/home/s/defaults/defaults,nosuid,nodev/'-e '/\/var /s/defaults/defaults,nosuid/'/etc/fstab
Retval=$?
Result Fstab_opt
}


Time_opt () {
Sed-i ' s#^#\# #g '/etc/sysconfig/clock && sed-i ' 1i zone= "Asia/shanghai" '/etc/sysconfig/clock && \cp-af /usr/share/zoneinfo/asia/shanghai/etc/localtime
Retval=$?
Result Time_opt
}

Ntp_opt () {
echo "*/5 * * * */usr/sbin/ntpdate time.sfbest.bj" >/tmp/ntpdate && crontab/tmp/ntpdate && rm-rf/t Mp/ntpdate
Retval=$?
Result Ntp_opt
}

Passwd1_opt () {
Sed-i '/^pass/s#^#\# #g '/etc/login.defs && echo-e ' pass_max_days 180\npass_min_days 1\npass_min_len 8\nPASS_WA Rn_age 7\n ">>/etc/login.defs
Retval=$?
Result Passwd1_opt
}

Passwd2_opt () {
Sed-ir '/pam_cracklib.so/s#^.*$ #password requisite pam_cracklib.so try_first_pass retry=3 type= ifok=3 minlen=10 Ucredit=-1 lcredit=-3 dvredit=-3 dictpath=/usr/share/cracklib/pw_dict#g '/etc/pam.d/system-auth
Retval=$?
Result Passwd2_opt
}

Passwd3_opt () {
  grep ' Remember '/etc/pam.d/system-auth &>/dev/null
  If [$?-eq 0];then
    sed-n '/remember/p '/etc/pam.d/system-auth >> ${dir}
  Else
    Sed-i '/password    sufficient    pam_unix.so md5/s#$# remember=3#g '/etc/pam.d/ System-auth
  fi
  retval=$?
  result Passwd3_opt
}

Pamd_sshd_opt () {
  sed-i '/#%pam-1.0/a\auth       required      pam_listfile.so item=user sense=allow file=/etc/ssh/sshusers onerr=succeed '/etc/pam.d/sshd
  Echo sa >/etc/ssh/sshusers
  sed-i ' s/\ (^wheel.*\)/\1,sa/'/etc/group
  sed-i '/^ #auth. * use_uid$/a auth           required         pam_wheel.so use_uid '/etc/pam.d/su
  retval=$?
  result Pamd_sshd_opt
}

#pam_tally2_opt () {
 # find/lib*-name "pam_tally2.so" &>/dev/null
  #if [$?-ne 0];then
& nbsp;  # echo "Pam_tally2.so is no exsit." >> ${dir
  #else
   # grep ' pam_tally2.so '/et C/pam.d/sshd &>/dev/null
    #[$-eq 0] && sed-n '/pam_tally2.so/p '/etc/pam.d/sshd & Gt;> ${dir} | | Sed-i ' 1a auth       required     pam_tally2.so deny=3 unlock_time =300 '/etc/pam.d/sshd
  #fi
  #RETVAL =$?
  #result pam_tally2_opt
#}


Ssh_opt () {
Sed-i ' s#\ #PermitRootLogin yes#permitrootlogin no#g '/etc/ssh/sshd_config && sed-i ' s#\ #Port 22#port 9880#g '/et C/ssh/sshd_config && sed-i ' s#\ #ListenAddress 0.0.0.0#listenaddress ' $SERVER _ip ' #g '/etc/ssh/sshd_config && sed-i ' s#\ #UseDNS yes#usedns no#g '/etc/ssh/sshd_config && echo ' export tmout=300 ' >>/etc/profi Le &&. /etc/profile
Retval=$?
Result Ssh_opt
}


Issue_opt () {
Cat/etc/issue >> ${dir} && >/etc/issue && >/etc/issue.net
Retval=$?
Result Issue_opt
}


Chattr_file_opt () {
Chattr +i/etc/passwd/etc/shadow/etc/group/etc/gshadow/etc/inittab && mv/usr/bin/chattr/etc/zchattr &&A mp echo "Chattr moved to/etc/zchattr" >> ${dir}
Retval=$?
Result Chattr_file_opt
}


Ctr_opt () {
Sed-i ' s#exec#\ #exec #g '/etc/init/control-alt-delete.conf
Retval=$?
Result Ctr_opt
}

History_opt () {
Echo-e "Export Histfilesize=5\nexport histsize=5" >>/etc/profile &&. /etc/profile
Retval=$?
Result History_opt
}

Selinux_iptables_opt () {
Sed-i ' s#^selinux=.*$ #SELINUX =disabled#g '/etc/selinux/config && setenforce 0 &>/dev/null
/etc/init.d/iptables Stop && chkconfig iptables off
Retval=$?
Result Selinux_iptables_opt
}


Sysctl_opt () {
  echo-ne
Net.ipv4.tcp_max_syn_backlog = 655350000
Net.core.netdev_max_ Backlog =  327680000
Net.core.somaxconn = 327680
Net.core.wmem_default = 838860800
Net.core.rmem_ Default = 838860800
Net.core.rmem_max = 167772160
Net.core.wmem_max = 167772160
Net.ipv4.tcp_timestamps = 0
Net.ipv4.tcp_synack_retries = 2
Net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_ Max_orphans = 3276800
Net.ipv4.tcp_keepalive_time =
Net.ipv4.tcp_max_tw_buckets = net.ipv4.tcp_tw_ Reuse = 1
net.ipv4.tcp_tw_recycle = 1
Net.ipv4.tcp_fin_timeout = ten
Net.ipv4.ip_local_port_range = 1024  65535
Vm.swappiness = ten
Net.nf_conntrack_max = 6553500
Net.netfilter.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_tcp_timeout_established =
>>/etc/sysctl.conf
  sysctl-p &> /dev/null
  retval=$?
  result Sysctl_opt
}


Fs_file_opt () {
Echo-ne "
* Soft Nofile 65535
* Hard Nofile 65535
">>/etc/security/limits.conf
Echo-ne "
* Soft Nproc 65535
* Hard Nproc 65535
">>/etc/security/limits.d/90-nproc.conf
Sysctl-p &>/dev/null
Retval=$?
Result Fs_file_opt
}


Yum_opt () {
rm-rf/etc/yum.repos.d/*
cd/etc/yum.repos.d/
For i in [Yum] Name=yum ' baseurl=http://yum.sfbest.bj/centos/$releasever/os/$basearch/' enable=1 gpgcheck=0; Do echo $i >> Yum.repo;done
Retval=$?
Result Yum_opt
}


Lang_opt () {
Sed-i ' s#^lang#\ #LANG #g '/etc/sysconfig/i18n && sed-i ' 1i lang= ' zh_cn. UTF-8 "'/etc/sysconfig/i18n
Retval=$?
Result Lang_opt
}


Sys_server_opt () {
Chkconfig--list|awk ' {print $} ' |xargs-i chkconfig {}--level 0123456 off
For a in AUDITD crond irqbalance network Psacct rsyslog sshd sysstat; Do chkconfig--level 2345 $a on; Done
Retval=$?
Result Sys_server_opt
}


Postfix_opt () {
[-d/server/scripts] | | Mkdir-p/server/scripts
echo "TMPWATCH-AFV 30d/var/spool/postfix/maildrop/" >/server/scripts/delete_mail.sh
echo "/bin/sh/server/scripts/delete_mail.sh &>/dev/null" >>/var/spool/cron/root
Retval=$?
Result Postfix_opt
}


Other_opt () {
rm-rf/root/*
chmod 0700/usr/bin/passwd

}


Main () {
Create_user
Fstab_opt
Time_opt
Ntp_opt
Passwd1_opt
Passwd2_opt
Passwd3_opt
Pamd_sshd_opt
Ssh_opt
Issue_opt
Chattr_file_opt
Ctr_opt
History_opt
Selinux_iptables_opt
Sysctl_opt
Fs_file_opt
Yum_opt
Lang_opt
Sys_server_opt
Postfix_opt
Other_opt
}

Main


Linux System Optimization Script implementation

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.