Centos_7下初始化系统的shell脚本

#!/bin/bash

cat << EOF

+---------------------------------------------------------------------------+

|  Initialize for the CentOS 7_installed.                                  |

+---------------------------------------------------------------------------+

EOF

function format() {

    #sleep 1    #echo -e "\033[42;37m ########### Finished ########### \033[0m\n"    echo -e "\033[32m Install Success!!!\033[0m\n"}

########################################################################### Set time 时区/时间同步设置echo "Set time."/bin/cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &> /dev/nullyum -y install ntpdate &> /dev/nullntpdate  0.centos.pool.ntp.org &> /dev/nullhwclock -w

format###########################################################################

# Set ntp client 设置时间服务客户端

echo "Set ntp client."

SED() {

    cp -p /etc/ntp.conf /etc/ntp.conf.bak

    sed -i '/^server/d' /etc/ntp.conf

    sed -i '/^includefile/ i\server 0.centos.pool.ntp.org iburst' /etc/ntp.conf

    sed -i '/0.centos.pool.ntp.org/ a\server 1.centos.pool.ntp.org iburst' /etc/ntp.conf

    sed -i '/1.centos.pool.ntp.org/ a\server 2.centos.pool.ntp.org iburst' /etc/ntp.conf

    sed -i '/2.centos.pool.ntp.org/ a\server 3.centos.pool.ntp.org iburst' /etc/ntp.conf

  systemctl enable ntpd.service &> /dev/null

}

rpm -q ntp &> /dev/null

if [ $? -eq 0 ]

then

    SED

else

    yum -y install ntp &> /dev/null

    SED

fi

format

##########################################################################

# Create Log 创建该脚本运行记录日志

echo "Create log file."

DATE1=`date +"%F %H:%M"`

LOG=/var/log/sysinitinfo.log

echo $DATE1 >> $LOG

echo "------------------------------------------" >> $LOG

format

############################################################################ Disabled Selinux 禁用Selinuxecho "Disabled SELinux."sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux

format

###########################################################################

# Disable ipv6 禁用IPV6

echo "Disable ipv6."

cat << EOF > /etc/modprobe.conf

alias net-pf-10 off

alias ipv6 off

EOF

format

##########################################################################

#Set history commands  设置命令历史记录参数

echo "Set history commands."

sed -i 's/HISTSIZE=100/HISTSIZE=1000/' /etc/profile

sed -i "8 s/^/alias vi='vim'/" /root/.bashrc

grep 'HISTFILESIZE' /etc/bashrc &>/dev/null

if [ $? -ne 0 ]

then

cat << EOF >> /etc/bashrc

HISTFILESIZE=4000

HISTSIZE=4000

HISTTIMEFORMAT='%F/%T'

EOF

fi

source /etc/bashrc

format

##########################################################################

# set vim

echo "Set Vim."

cat << EOF > ~/.vimrc

set number

EOF

format

##########################################################################

# Epel 升级epel源

echo "Install epel"

yum install epel-release

format

##########################################################################

#Yum install Development tools  安装开发包组及必备软件

echo "Install Development tools(It will be a moment)"

yum groupinstall -y "Development tools" &> /dev/null

yum install -y bind-utils lrzsz wget gcc gcc-c++ vim htop openssl &>/dev/null

format

##########################################################################

# Yum update bash and openssl  升级bash/openssl

echo "Update bash and openssl"

yum -y update bash openssl &> /dev/null

format

############################################################################ Set ssh 设置ssh登录策略echo "Set sshd."sed -i "s/^#PermitEmptyPasswords/PermitEmptyPasswords/" /etc/ssh/sshd_config

sed -i "s/^#LoginGraceTime 2m/LoginGraceTime 6m/" /etc/ssh/sshd_config

grep "UseDNS no" /etc/ssh/sshd_config &>/dev/nullif [ $? -ne 0 ]then    echo "UseDNS no" >> /etc/ssh/sshd_config

fi#禁止 root 使用 ssh 登入sed -i "s/^#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config#修改sshd默认端口sed -i "s/^#Port 22/Port 38389/" /etc/ssh/sshd_config

systemctl  restart sshd.service

format###########################################################################

# Del unnecessary users 删除不必要的用户

echo "Del unnecessary users."

for USERS in adm lp sync shutdown halt mail news uucp operator games gopher

do

    grep $USERS /etc/passwd &>/dev/null

    if [ $? -eq 0 ]

    then

        userdel $USERS &> /dev/null

    fi

done

format

############################################################################ Del unnecessary groups 删除不必要的用户组echo "Del unnecessary groups."for GRP in adm lp mail news uucp games gopher mailnull floppy dip pppusers popusers slipusers daemondo    grep $GRP /etc/group &> /dev/null    if [ $? -eq 0 ]

    then        groupdel $GRP &> /dev/null    fi

done

format

###########################################################################

# Disabled reboot by keys ctlaltdelete 禁用ctlaltdelete重启功能

echo "Disabled reboot by keys ctlaltdelete"

sed -i 's/^exec/#exec/' /etc/init/control-alt-delete.conf

format

############################################################################ Set ulimit  设置文件句柄数echo "Set ulimit 1000000"

cat << EOF > /etc/security/limits.conf

*    soft    nofile  1000000*    hard    nofile  1000000*    soft    nproc 102400*    hard    nproc 102400EOF

sed -i 's/4096/102400/' /etc/security/limits.d/20-nproc.conf

format

  ###########################################################################

# Disabled crontab send mail 禁用执行任务计划时向root发送邮件

echo "Disable crontab send mail."

sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab

sed -i 's/^mail\.\*/mail\.err/' /etc/rsyslog.conf

format

############################################################################ Set sysctl.conf 设置内核参数echo "Set sysctl.conf"#web应用中listen函数的backlog默认会将内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认是511,所以必须调整,一般调整为2048cat << EOF > /etc/sysctl.conf

net.core.somaxconn = 2048net.core.wmem_default = 262144net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem = 4096 4096 16777216net.ipv4.tcp_wmem = 4096 4096 16777216net.ipv4.tcp_mem = 786432 2097152 3145728net.ipv4.tcp_max_syn_backlog = 16384net.core.netdev_max_backlog = 20000net.ipv4.tcp_fin_timeout = 15net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_max_orphans = 131072net.ipv4.ip_local_port_range = 1024 65535net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1net.bridge.bridge-nf-call-ip6tables = 0net.bridge.bridge-nf-call-iptables = 0net.bridge.bridge-nf-call-arptables = 0kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 4294967295kernel.shmall = 26843545EOF

#modprobe bridge  > /dev/null/sbin/sysctl  -p  > /dev/nullformat

你可能感兴趣的:(Centos_7下初始化系统的shell脚本)