常用linux 初始化脚本

#!/bin/bash
# 线下服务器初始化脚本
# 设置内核版本 ‘6 7 redhat6′
RELEASEVER=’redhat6’

# 配置yum源
cd /etc/yum.repos.d/
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
if [ $RELEASEVER == redhat6 ];then
rpm -qa|grep yum|xargs rpm -e –nodeps
mkdir yum
cd yum
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-urlgrabber-3.9.1-11.el6.noarch.rpm &&
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm &&
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm &&
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-updateonboot-1.1.30-41.el6.noarch.rpm &&
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm &&
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm &&
rpm -Uvh python-urlgrabber-3.9.1-11.el6.noarch.rpm
rpm -ivh yum-*
cd /etc/yum.repos.d/ &&
wget -O /etc/yum.repos.d/CentOS6-Base-163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo &&
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
sed -i ‘s/$releasever/6/g’ /etc/yum.repos.d/CentOS6-Base-163.repo
fi

if [ $RELEASEVER == 6 ];then
wget -O /etc/yum.repos.d/CentOS6-Base-163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
sed -i ‘s/$releasever/6/g’ /etc/yum.repos.d/CentOS6-Base-163.repo
fi

if [ $RELEASEVER == 7 ];then
wget -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i ‘s/$releasever/7/g’ /etc/yum.repos.d/CentOS7-Base-163.repo
fi

yum clean all
yum makecache

# 安装常用软件
yum -y install vim bash-completion

# 配置limits.conf
if [ $RELEASEVER == 6 ];then
test -f /etc/security/limits.d/90-nproc.conf && rm -rf /etc/security/limits.d/90-nproc.conf && touch /etc/security/limits.d/90-nproc.conf
fi
if [ $RELEASEVER == redhat6 ];then
test -f /etc/security/limits.d/90-nproc.conf && rm -rf /etc/security/limits.d/90-nproc.conf && touch /etc/security/limits.d/90-nproc.conf
fi
if [ $RELEASEVER == 7 ];then
test -f /etc/security/limits.d/20-nproc.conf && rm -rf /etc/security/limits.d/20-nproc.conf && touch /etc/security/limits.d/20-nproc.conf
fi

>/etc/security/limits.conf
cat >> /etc/security/limits.conf <<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF

# 配置时区
test -f /etc/localtime && rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 清空防火墙
if [ $RELEASEVER == 6 ];then
/sbin/iptables -F
/etc/init.d/iptables save
chkconfig ip6tables off
fi
if [ $RELEASEVER == redhat6 ];then
/sbin/iptables -F
/etc/init.d/iptables save
chkconfig ip6tables off
fi
if [ $RELEASEVER == 7 ];then
systemctl disable firewalld
fi

# 关闭 SELINUX
setenforce 0
sed -i ‘s/^SELINUX=.*$/SELINUX=disabled/’ /etc/selinux/config

# 配置历史命令
sed -i ‘s/^HISTSIZE=.*$/HISTSIZE=100000/’ /etc/profile
grep -q ‘HISTTIMEFORMAT’ /etc/profile
if [[ $? -eq 0 ]];then
sed -i ‘s/^HISTTIMEFROMAT=.*$/HISTTIMEFORMAT=”%F %T “/’ /etc/profile
else
echo ‘HISTTIMEFORMAT=”%F %T “/’ >> /etc/profile
fi

# 配置DNS
> /etc/resolv.conf
cat >> /etc/resolv.conf << EOF
options timeout:1
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 114.114.114.114
nameserver 114.114.115.115
nameserver 8.8.8.8
EOF

yum install -y ntpdate
echo ’01 01 * * * /usr/sbin/ntpdate ntp1.aliyun.com >>/dev/null 2>&1′ > /var/spool/cron/root