【Shell/Python】CentOS 6.X 系统初始化生产脚本

#!/bin/bash
# Program:
#       system_init_shell
# History:
#       2012/06/01 25061008@qq.com
# Release:
#       1.1
cat << EOF
+————————————————————–+
|          === Welcome to Centos 6.x System init ===           |
+————————————————————–+
+—————————by opdba————————–+
EOF

#update 163 yum
cd /etc/yum.repos.d
mv CentOS-Base.repo  CentOS-Base.repo.bak
wget http://mirrors.163.com/.help/CentOS-Base-163.repo
mv CentOS-Base-163.repo  CentOS-Base.repo
yum clean metadata
yum makecache

#add epel
rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm  –import  /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

#add rpmforge
rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm –import  /etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag

#update system pack
yum -y install gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel bison patch unzip libmcrypt-devel libmhash-devel ncurses-devel sudo bzip2 mlocate flex lrzsz sysstat lsof setuptool  system-config-network-tui system-config-firewall-tui ntsysv ntp libaio-devel wget ntp

#set ntp

/usr/sbin/ntpdate ntp.api.bz
echo “*/5 * * * * /usr/sbin/ntpdate ntp.api.bz > /dev/null 2>&1” >> /var/spool/cron/root
service crond restart

#set clock
hwclock –set –date=”`date +%D\ %T`”
hwclock –hctosys

#set ulimit
echo “ulimit -SHn 102400” >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
*           soft   nofile       102400
*           hard   nofile       102400
*           soft   nproc        102400
*           hard   nproc        102400
EOF

#set max user processes
sed -i ‘s/1024.*/102400/’ /etc/security/limits.d/90-nproc.conf

# turnoff the control-alt-delete
sed -i ‘s#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#’ /etc/init/control-alt-delete.conf

#close useless service
for i in `ls /etc/rc3.d/S*`
do
  CURSRV=`echo $i|cut -c 15-`
  echo $CURSRV
case $CURSRV in
  crond | irqbalance | network | sshd | rsyslog | sysstat )
  echo “Base services, Skip!”
  ;;
  *)
  echo “change $CURSRV to off”
  chkconfig –level 2345 $CURSRV off
  service $CURSRV stop
  ;;
esac
done
echo “service is init is ok…………..”

#set LANG
:> /etc/sysconfig/i18n
cat >> /etc/sysconfig/i18n << EOF
LANG=”en_US.UTF-8″
EOF

#set ssh
sed -i ‘s/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/’ /etc/ssh/sshd_config
sed -i ‘s/#UseDNS yes/UseDNS no/’ /etc/ssh/sshd_config
sed -i ‘s/#Port 22/Port 6343/’ /etc/ssh/sshd_config
service sshd restart

#set sysctl
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
/sbin/sysctl -p
echo “sysctl set OK!!”

#disable ipv6
echo “alias net-pf-10 off” >> /etc/modprobe.conf
echo “alias ipv6 off” >> /etc/modprobe.conf
/sbin/chkconfig ip6tables off
echo “ipv6 is disabled!”

#disable selinux
sed -i ‘/SELINUX/s/enforcing/disabled/’ /etc/selinux/config
setenforce 0

#vim setting

sed -i “8 s/^/alias vi=’vim’/” /root/.bashrc
echo ‘syntax on’ > /root/.vimrc

cat << EOF
+————————————————————–+
|                    ===System init over===                    |
+————————————————————–+
+—————————by 邱治军————————–+
EOF
echo “###############################################################”

关于脚本的解释说明:
1.将系统默认yum源改成163源
2.增加第三方epel源
3.增加第三方rpmforge软件库
4.更新系统的基础软件
5.设置系统时钟同步
6.其中第一条语句是设定硬件时钟,第二条语句是设定系统时钟和硬件时钟同步
7.增加打开最大文件描述符
8.增加系统进程数(线程)的限制–>max user processes
centos 5与centos 6不同的是,只要在/etc/security/limits.conf 设置了root soft nofile 102400 和root hard nofile102400,对应的uilmit -u 后的结果就会是102400,这里需要注意区别!
9.关闭control-alt-delete
10.关闭不用的系统服务
11.设置系统语系
12.设置SSH
13.设置系统内核参数
14.关闭IPV6
15.关闭selinux
16.vi相关设置

    原文作者:qzhijun
    原文地址: https://blog.51cto.com/opsmysql/985316
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞