#!/bin/bash
# 文件名: CentOS-7_init.sh
# 功能: CentOS-7 系列 系统初始化脚本
# 作者: shuhui
# 版本: v1.2

### 更新日志
# 2015-12-21 版本 v1.0: 1.实现基于CentOS-7系列简单的初始化脚本
# 2016-01-06 版本 v1.1: 1.添加 mailx 包，实现系统发现邮件功能
# 2016-01-28 版本 v.12: 1.将时间同步 chrony 替换成 ntp


### 定义变量 开始 ###

MAIL_ADDR=monitor@fanghubao.com          # 邮件发送地址
SMTP_SERV=smtp.exmail.qq.com             # SMTP 服务器地址
SMTP_USER=monitor@fanghubao.com          # SMTP 验证账户
SMTP_PASS=iHFPzmbvvwt52hekzalhre         # SMTP 验证密码

### 定义变量 结束 ###

# 关闭 Selinux
if [[ "$(/usr/sbin/getenforce)" != "Disable" ]]
then
    /usr/sbin/setenforce 0 >/dev/null 2>&1
    sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
fi

# 设置时区
if [[ "$(date -R|awk '{print $NF}')" != "+0800" ]]
then
    timedatectl set-timezone "Asia/Shanghai"
fi

# 设置 yum 仓库源
cd /etc/yum.repos.d/
mkdir /etc/yum.repos.d/save >/dev/null 2>&1
mv *.repo ./save >/dev/null 2>&1
curl -s -L http://mirrors.aliyun.com/repo/Centos-7.repo -o  ./CentOS-Base.repo ;
curl -s -L http://mirrors.aliyun.com/repo/epel-7.repo -o ./epel.repo ;
yum clean all && yum makecache fast && yum -y update

# 安装 vim
yum -y install vim-enhanced
cat > /etc/vimrc <<end
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set nu
syntax on
set autoindent
filetype on
set history=100
set smartindent
set tabstop=4
set shiftwidth=4
set ruler
set nohls
set incsearch
end


# 配置 NTP
yum -y install ntp
cat >/etc/ntp.conf <<EOF
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
# Default NTP Server
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# NTP Server Address(Data From:http://www.ntp.org.cn/)
server 202.108.6.95 iburst
server 202.112.29.82 iburst
server 110.75.190.198 iburst
server 115.28.122.198 iburst
server 182.92.12.11 iburst
server 120.25.108.11 iburst
server 110.75.186.249 iburst
server 110.75.186.248 iburst
server 110.75.186.247 iburst

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
EOF

systemctl restart ntpd
systemctl enable ntpd

# 安装常用软件
yum install -y lrzsz net-tools md5deep bash-completion mailx quagga

# 使用iptables 替代 firewalld
yum -y install iptables-services
/usr/bin/systemctl enable iptables
/usr/bin/systemctl start iptables
/usr/bin/systemctl disable firewalld

# 关闭 postfix
/usr/bin/systemctl disable postfix
/usr/bin/systemctl stop postfix

# 设置日志历史数量及格式
echo -en "\n# Histroy log config\nexport HISTFILESIZE=10000\nexport HISTSIZE=10000\nexport PROMPT_COMMAND=\"history -a\"\nexport HISTTIMEFORMAT=\"\$(whoami) %F %T \"" >> /etc/profile

# 设置 mailx,使系统能发送邮件
echo -en "# file: /etc/mail.rc\nset from=${MAIL_ADDR}\nset smtp=${SMTP_SERV}\nset smtp-auth-user="${SMTP_USER}"\nset smtp-auth-password="${SMTP_PASS}"\nset smtp-auth=login" > /etc/mail.rc
