centos系统初始化脚本

#!/bin/bash

echo "-----只用于新购服务器初始化,有需要的自己可以写进去-----"

echo "-----只用于CENTOS7.0版本---------------"

echo "-----其他版本差异,可以自行修改------"

 

read -p "是否继续 y/n": YN

if [ $YN != y ];then

exit 3

fi

#查看系统版本

linux_release=`cat /etc/redhat-release`

release_centos6="release 6."

release_centos7="release 7."

#关闭防火墙并禁止开机自启

if [[ $linux_release =~ $release_centos7 ]];then

systemctl stop firewalld.service && systemctl disable firewalld.service

elif [[ $linux_release =~ $release_centos6 ]];then

service iptables stop && chkconfig iptables off

fi

 

#关闭内核机制

#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux && setenforce 0

if [[ $linux_release =~ $release_centos6 ]];then

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux

elif [[ $linux_release =~ $release_centos7 ]];then

sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config

fi

 

#安装初始工具

yum install gcc* gcc-* zlib-devel pcre-devel openssh-clients net-tools ntp rsync -y

#修改ssh默认端口

netstat -anpt | grep 22

if [ $? = 0 ];then

sed -i 's/#Port 22/Port 12022/' /etc/ssh/sshd_config

if [[ $linux_release =~ $release_centos7 ]];then

systemctl restart sshd

elif [[ $linux_release =~ $release_centos6 ]];then

service sshd restart

fi

fi

#获取IP地址 (centos7版本网卡默认是ens33 阿里云已更改为eth,请根据实际更改)更改主机名

IP=$(ip add | grep -w "inet" | grep eth0 |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g')

NAME=$(echo $IP|awk -F"." '{print $3$4}')

if [[ $linux_release =~ $release_centos6 ]];then

sed -i "/HOSTNAME=*/c HOSTNAME=aliyun.${NAME}" /etc/sysconfig/network

elif [[ $linux_release =~ $release_centos7 ]];then

hostnamectl set-hostname aliyun${NAME}

fi

#安装zabbix-agent

wget http://10.168.10.16/add_zabbix.sh && bash add_zabbix.sh

 

#创建普通用户web

USER=`cat /etc/passwd |awk -F: '{print $1}'|grep web`

if [ -z ${USER} ];then

useradd web

fi

#设置密码

passwd web << EOF

AjDWgvx8z

AjDWgvx8z

EOF

#判断/home/web目录是否存在

if [ ! -d "/home/web" ]; then

/bin/mkdir /home/web/

chown -R web:web /home/web/

else

chown -R web:web /home/web/

fi

#判断/home/web/.ssh目录是否存在,不存在则创建

if [ ! -d "/home/web/.ssh" ]; then

/bin/mkdir /home/web/.ssh

chown -R web:web /home/web/.ssh

fi

#判断/home/web/.ssh/authorized_keys目录是否存在,不存在则创建

if [ ! -f "/home/web/.ssh/authorized_keys" ]; then

/bin/touch /home/web/.ssh/authorized_keys

/bin/chmod 600 /home/web/.ssh/authorized_keys

else

/bin/chmod 600 /home/web/.ssh/authorized_keys

fi

#把zabbix服务器上的web用户的key追加到认证文件中

/bin/grep web@zabbix /home/web/.ssh/authorized_keys || echo "ssh-rsa  xxxxxxxx    web@zabbix" >> /home/web/.ssh/authorized_keys

 

#创建读取日志用户

USER=readlog

/bin/grep ${USER} /etc/passwd || useradd ${USER}

#设置readlog用户密码

passwd ${USER} << EOF

MAqglc2Mqz

MAqglc2Mqz

EOF

#判断/home/readlog/.ssh目录是否存在,不存在则创建

if [ ! -d "/home/${USER}/.ssh" ]; then

/bin/mkdir "/home/${USER}/.ssh"

fi

#判断/home/web/.ssh/authorized_keys目录是否存在,不存在则创建

if [ ! -f "/home/${USER}/.ssh/authorized_keys" ]; then

/bin/touch /home/${USER}/.ssh/authorized_keys

chown -R ${USER}:${USER} /home/${USER}/.ssh/

/bin/chmod 600 /home/${USER}/.ssh/authorized_keys

fi

#把zabbix服务器上的readlog用户的key追加到认证文件中

/bin/grep ${USER}@zabbix /home/${USER}/.ssh/authorized_keys || echo "ssh-rsa  xxxxxxx      readlog@zabbix" >> /home/${USER}/.ssh/authorized_keys

 

#判断/root/.ssh目录是否存在,不存在则创建

if [ ! -d "/root/.ssh" ]; then

/bin/mkdir "/root/.ssh"

fi

#判断/root/.ssh/authorized_keys目录是否存在,不存在则创建

if [ ! -f "/root/.ssh/authorized_keys" ]; then

/bin/touch /root/.ssh/authorized_keys

/bin/chmod 600 /root/.ssh/authorized_keys

fi

#把zabbix服务器上的root用户的key追加到认证文件中

/bin/grep root@zabbix /root/.ssh/authorized_keys || echo "ssh-rsa   xxxxxxxxxxxx  root@zabbix" >> /root/.ssh/authorized_keys

 

echo "创建基础应用目录"

mkdir -pv /opt/deploy/tomcat

mkdir -pv /opt/deploy/service

mkdir -pv /opt/deploy/repository

mkdir -pv /opt/logs/service

mkdir -pv /opt/logs/tomcat

chown -R web.web /opt

 

#休眠三十秒,重启系统

sleep 10

shutdown -r now

 

你可能感兴趣的:(shell)