#!/bin/bash
echo "-------Just for centos5.X-----------------"
echo "-------if not centos5.X,pleast ctrl+c-----"


#test whether root
if [ $(id -u) != 0 ];then
    echo "error,You must be root to be run this script"
    exit
fi

    get_char()
    {
    SAVEDSTTY=`stty -g`
    stty -echo
    stty cbreak
    dd if=/dev/tty bs=1 count=1 2> /dev/null
    stty -raw
    stty echo
    stty $SAVEDSTTY
    }
    echo ""
    echo "Press any key to start..."
    char=`get_char`


for i in `ls /etc/rc3.d/S*`
do
             CURSRV=`echo $i|cut -c 15-`
echo $CURSRV
case $CURSRV in
     atd | auditd | cpuspeed | crond | haldaemon | iptables | irqbalance | iscsi | iscsid | lvm2-monitor | messagebus | microcode_ctl | mysql | network | nginx | php-fpm | portmap | rawdevices | readahead_early | smartd | sshd | syslog | local )
     echo "Base services, Skip!"
     ;;
     *)
         echo "change $CURSRV to off"
         chkconfig --level 235 $CURSRV off
         service $CURSRV stop
     ;;
esac
done
echo "----------service are setted-----------------"

#tty optimization
sed -i 's/3\:2345/\#&/' /etc/inittab
sed -i 's/4\:2345/\#&/' /etc/inittab
sed -i 's/5\:2345/\#&/' /etc/inittab
sed -i 's/6\:2345/\#&/' /etc/inittab
echo "----------tty are setted---------------------"

#disable the ipv6
echo "alias net-pf-10 off" >> /etc/modprobe.conf
echo "alias ipv6 off" >> /etc/modprobe.conf
/sbin/chkconfig --level 35 ip6tables off
sed -i '/NETWORKING_IPV6/d' /etc/sysconfig/network
sed -i '/IPV6INIT/d' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/IPV6_AUTOCONF/d' /etc/sysconfig/network-scripts/ifcfg-eth0
echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
echo "IPV6INIT = no" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "----------ipv6 is setted---------------------"


#set locale
cat > /etc/sysconfig/i18n <<EOF
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF




#disable selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "----------selinus is setted------------------"


#set timezone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate 210.72.145.44
echo "----------time is setted---------------------"

#user resource limit
sed -i '/^ulimit/d' /etc/profile
cat >> /etc/profile <<EOF
ulimit -SHn 60000
EOF
echo "----------user limit is setted---------------"

# kernel parameter optimization
true > /etc/sysctl.conf
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65535
EOF
sysctl -p
echo "----------kernel parameter is setted----------"

#file open limit
cat >> /etc/security/limits.conf <<EOF
* soft noproc 60240
* hard noproc 60240
* soft nofile 60240
* hard nofile 60240
EOF
echo "----------file open limit is setted------------"

#disabled icmp ping
sed -i '/icmp_echo_ignore_all/d' /etc/rc.local
cat >> /etc/rc.local <<EOF
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
EOF
echo "----------icmp is setted-----------------------"

#sshd init
ssh_cf="/etc/ssh/sshd_config"
sed -i -e '74 s/^/#/' -i -e '76 s/^/#/' $ssh_cf
#sed -i "s/#Port 22/Port 65535/" $ssh_cf
sed -i "s/#UseDNS yes/UseDNS no/" $ssh_cf
#client
sed -i -e '44 s/^/#/' -i -e '48 s/^/#/' $ssh_cf
service sshd restart
echo "----------ssh init is ok-----------------------"

echo "----------pleash reboot you system ------------"