#!/bin/bash ######################################################## # author: Logan # website: http://opsbase.cn # description: Service installation script # bash <(curl -sL http://internal.up-gram.com/shell/init-xedge.sh) pro-game-hsd-app-1 ######################################################## BIN_DIR="/usr/local/dingtone" VAR_DIR="/var/dingtone" ETC_DIR="/etc/dingtone" VAR_LOG_DIR="/var/log/dingtone" # 判断是否root用户 if [ $(id -u) -ne 0 ]; then color_msg error "### This script must be run as root !!!" exit 1 fi if [ -d "$BIN_DIR" ]; then echo "${BIN_DIR} existing" else echo "mkdir ${BIN_DIR}" mkdir $BIN_DIR fi if [ -d "$VAR_DIR" ]; then echo "${VAR_DIR} existing" else echo "mkdir ${VAR_DIR}" mkdir $VAR_DIR fi if [ -d "$ETC_DIR" ]; then echo "${ETC_DIR} existing" else echo "mkdir ${ETC_DIR}" mkdir $ETC_DIR fi if [ -d "$VAR_LOG_DIR" ]; then echo "${VAR_LOG_DIR} existing" else echo "mkdir ${VAR_LOG_DIR}" mkdir $VAR_LOG_DIR fi #### ----------- 打印颜色 ----------- #### function color_message() { case "$1" in "error"|"red") echo -e "\e[1;31m$2\e[0m" ;; "warn"|"yellow") echo -e "\e[1;33m$2\e[0m" ;; "info"|"blue") echo -e "\e[1;34m$2\e[0m" ;; "success"|"green") echo -e "\e[1;32m$2\e[0m" ;; esac } # Set_passwd() { # echo "tmp123456" | passwd --stdin "root" #修改密码 # } Init_Install() { # Set_passwd update_pkg Set_hostname $1 Set_Selinux Set_iptables Set_ssh Set_limits Set_profile Set_timezone Optimize_kernel Push_key } update_pkg() { #### ----------- 安装基础包 ----------- #### which yum &>/dev/null if [ $? -eq 0 ];then systemOS="centos" echo "*****************************************" color_message "info" ">> OS: $systemOS start to initialize" echo "*****************************************" color_message "info" "---- install base package ----" yum --debuglevel=1 install -y vim nfs-utils openssl-devel pcre-devel ntpdate \ nc iotop sysstat dstat iftop nload iperf \ lrzsz wget mlocate lsof telnet net-tools dmidecode yum-utils \ glibc bind-utils ethtool gdb unzip zip perf iotop mtr iperf fi which apt-get &>/dev/null if [ $? -eq 0 ];then systemOS="ubuntu" echo "*****************************************" color_message "info" ">> OS: $systemOS start to initialize" echo "*****************************************" color_message "info" "---- install base package ----" apt install vim ntpdate iotop sysstat dstat iftop \ nload iperf lrzsz wget lsof telnet net-tools dmidecode fi } Set_hostname() { # bash host_init.sh hostname 主机名传参 if [ $# -lt 1 ]; then #传参少于1个 color_message "warn" "---- Use default options ----" HOSTNAME="TemplateOS" # 默认主机名TemplateOS else color_message "info" "---- Set Hostname $HOSTNAME ----" HOSTNAME=$1 fi if [ -f /etc/hostname ]; then echo "$HOSTNAME" >/etc/hostname fi sed -i "/HOSTNAME/c HOSTNAME=$HOSTNAME" /etc/sysconfig/network || echo "HOSTNAME=$HOSTNAME" >>/etc/sysconfig/network hostname $HOSTNAME grep $HOSTNAME /etc/hosts || echo "127.0.0.1 $HOSTNAME" >>/etc/hosts } Set_Selinux() { color_message "info" "---- close selinux ----" if [ -s /etc/selinux/config ]; then setenforce 0 sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config fi } Set_iptables() { color_message "info" "---- setup iptables ----" if [ ! -f /etc/sysconfig/iptables ]; then yum install iptables-services -y chkconfig iptables on systemctl enable iptables systemctl disable firewalld systemctl stop firewalld service iptables restart fi iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables-save &>/dev/null service iptables save &>/dev/null } Set_timezone() { color_message "info" "---- Start configuring Timezone ---- " \cp /usr/share/zoneinfo/UTC /etc/localtime &>/dev/null # timedatectl set-timezone "Asia/Shanghai" } Set_ssh() { color_message "info" "---- Start configuring SSH optimize---- " sed -i '/^#Port/c Port 1022' /etc/ssh/sshd_config &>/dev/null # 默认端口修改 grep '#UseDNS yes' /etc/ssh/sshd_config && sed -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config grep '#AuthorizedKeysFile' /etc/ssh/sshd_config && sed -i "s/#AuthorizedKeysFile/AuthorizedKeysFile/" /etc/ssh/sshd_config grep 'GSSAPIAuthentication yes' /etc/ssh/sshd_config && sed -i "s/GSSAPIAuthentication yes/GSSAPIAuthentication no/g" /etc/ssh/sshd_config service sshd restart # sshd服务重启 } Set_limits() { color_message "info" "---- Start configuring System limits ---- " chmod +x /etc/rc.local grep ulimit /etc/rc.local || echo ulimit -HSn 1048576 >>/etc/rc.local grep 1048576 /etc/security/limits.conf || cat >>/etc/security/limits.conf <>/etc/profile grep HISTTIMEFORMAT /etc/profile || cat >>/etc/profile </etc/sysctl.conf <>/root/.ssh/authorized_keys chown root.root /root/.ssh/authorized_keys chmod 700 /root/.ssh ;chmod 600 /root/.ssh/authorized_keys useradd admin su - admin -c 'mkdir -p /home/admin/.ssh' echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3pPyWrNAYEIVq2yxN287isqYN8Uex3MH3ksGF9NJF2WvVnkKMohxxrS2cp9IZrOgh7CUvPUvBgud8Croi41jaSUF58SuaUGjiVllE62dwyu58NKzPZ097Jk/LRBirYbSx9Ug6Hx95zs49pk2IJCj69lP4BZeOFWVM2YM9LbIMuZdIYJLdedAqdEPTk3LyHMwEEMJ0kc2VpyfnogYiKEIKvRlnggBLt+221vR1piY0lUFeo/+vY03KUcSekHkMfpOKYvc4roZXFewQ/hjzIG6itgYN4L+vIPLiW5I1g2PzTmQOz0GOlPKjW7UK22LiQrA+MUP71LpgaH0l0AdtZNLp admin.pem.pub">>/home/admin/.ssh/authorized_keys chown admin.admin /home/admin/.ssh/authorized_keys chmod 700 /home/admin/.ssh ;chmod 600 /home/admin/.ssh/authorized_keys } # ----------- 执行中 ----------- # echo Init_Install $1 # ----------- 收尾配置 ----------- # if [ -f $0 ];then rm $0 -f # 回收此脚本文件 fi echo color_message "success" ">> 脚本执行结束... " # reboot