#!/bin/bash ######################################################## # author: 以谁为师 # website: http://opsbase.cn # description: Service installation script # bash <(curl -sL http://internal.up-gram.com/shell/init.sh) pro-game-hsd-app-1 ######################################################## # 判断是否root用户 if [ $(id -u) -ne 0 ]; then color_msg error "### This script must be run as root !!!" exit 1 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 } 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 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/Asia/Shanghai /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服务重启 if [ -d /home/ec2-user ];then su - ec2-user -c 'mkdir -p /home/ec2-user/.ssh' grep TzAdminManageKey-20200810.pem.pub /home/ec2-user/.ssh/authorized_keys || echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrUm0WfWve64OeJ8Yyj410FjNnUio28o9Nt7FThmD5FZxMhGwkGj/mPHdCUlRRWgultZE2vE18cd1loEzMJGPOUZpQpwhdHy8e4ppAdco6HYIY8Rmcg4wQK6FabeT0pZvtbSggXjYfiSEikaHaqwcwb5qrWSuSq7cBfR1XNbTVQwJlzHAtV9Od3Jidmkypfw8TEJDGX/rkk3dmPauxExXg2PLA/uwSMc4abja4hoWUKdrZT20WAEVb6vWu2Pr8tyrZoHflZIDHHv0eynZFeS4dMbB/65X4st1h3Yi9n0N7wyTgmumAYmIa+PcdYGJnh5ju9jX1PIk9mX+rVQsPXRRX #By key name: TzAdminManageKey-20200810.pem.pub">>/home/ec2-user/.ssh/authorized_keys chown -R ec2-user.ec2-user /home/ec2-user/.ssh/ chmod 700 /home/ec2-user/.ssh ;chmod 600 /home/ec2-user/.ssh/authorized_keys else mkdir /root/.ssh grep TzAdminManageKey-20200810.pem.pub /home/ec2-user/.ssh/authorized_keys || echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrUm0WfWve64OeJ8Yyj410FjNnUio28o9Nt7FThmD5FZxMhGwkGj/mPHdCUlRRWgultZE2vE18cd1loEzMJGPOUZpQpwhdHy8e4ppAdco6HYIY8Rmcg4wQK6FabeT0pZvtbSggXjYfiSEikaHaqwcwb5qrWSuSq7cBfR1XNbTVQwJlzHAtV9Od3Jidmkypfw8TEJDGX/rkk3dmPauxExXg2PLA/uwSMc4abja4hoWUKdrZT20WAEVb6vWu2Pr8tyrZoHflZIDHHv0eynZFeS4dMbB/65X4st1h3Yi9n0N7wyTgmumAYmIa+PcdYGJnh5ju9jX1PIk9mX+rVQsPXRRX #By key name: TzAdminManageKey-20200810.pem.pub">>/root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys fi } 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 <> 脚本执行结束... " # reboot