#!/bin/bash ### # @Author: 以谁为师 # @Website: attacker.club # @Date: 2020-08-05 10:40:47 # @LastEditTime: 2020-09-06 21:56:59 # @Description: Internet initialization scripts ### # ----------- 打印颜色 ----------- # 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 } pre_check(){ if [ $(id -u) -ne 0 ]; then color_message "warn" ">> This script must be run as root !!!" exit 0 fi Yum_aliyun_repo } Set_passwd() { echo "tmp123456" | passwd --stdin "root" #修改密码 } Init_Install() { # Set_passwd Set_dns Set_ntp Yum_update_pkg Set_hostname $1 Set_Selinux Set_iptables Set_virtual Centos_init Set_ssh Set_limits Set_profile Set_swap #Set_timezone Optimize_kernel } Set_swap(){ color_message "info" "---- stop swap ---- " swapoff -a # 临时关闭swap;将/etc/fstab文件中设置为swap的设备关闭 sed -i '/swap/d' /etc/fstab # 永久关闭swap } Yum_aliyun_repo() { color_message "info" "---- yum install ----" # find /etc/yum.repos.d/ -type f ! -name "*Base.repo" -exec rm -f {} \; # mkdir /etc/yum.repos.d/tmp &>/dev/null # mv /etc/yum.repos.d/*repo /etc/yum.repos.d/tmp &>/dev/null if [ $rhel_version = 6 ]; then #wget -O /etc/yum.repos.d/alyun-Centos-6.repo http://mirrors.aliyun.com/repo/Centos-6.repo wget -O /etc/yum.repos.d/aliyun-epel6.repo http://mirrors.aliyun.com/repo/epel-6.repo elif [ $rhel_version = 7 ]; then #curl -s http://mirrors.aliyun.com/repo/Centos-7.repo >/etc/yum.repos.d/aliyun-Centos7.repo curl -s http://mirrors.aliyun.com/repo/epel-7.repo >/etc/yum.repos.d/epel-7.repo else echo "Unknown version" exit 0 fi } Yum_update_pkg() { #yum update -y #Update all packages yum --debuglevel=1 install gcc gcc-c++ openssl-devel ntpdate nfs-utils libtool \ openssl-perl ncurses-devel pcre-devel zlib zlib-devel unzip -y #base yum --debuglevel=1 install nmap iotop sysstat dstat iftop nload iperf iproute net-tools \ lrzsz wget vim-enhanced mlocate lsof telnet yum-utils dmidecode -y #tools #yum install OpenIPMI OpenIPMI-devel OpenIPMI-tools OpenIPMI-libs -y #物理机ipmi } 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=$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_virtual() { virtual=$(dmesg | grep -i virtual | grep input | wc -l) if [ $virtual = 0 ]; then color_message "info" "---- physical machine setting ----" hwclock -w # 将系统时钟同步到硬件时钟 sed -i '/bell-style/c set bell-style none' /etc/inputrc # 替换禁止滴滴声 # dmidecode -s system-product-name|awk '{if($1!~"VMware")exit 1}' || hwclock -w else color_message "info" "---- virtual machine setting ----" # uuidgen ens fi } Centos_init() { if [ $rhel_version = 6 ]; then echo >/etc/udev/rules.d/70-persistent-net.rules &>/dev/null fi # if [ $rhel_version = 7 ]; then # cp /etc/sysconfig/grub /etc/sysconfig/grub.bak # grub2-mkconfig -o /boot/grub2/grub.cfg &>/dev/null # # net.ifnames=0 biosdevname=0 # systemctl disable NetworkManager # systemctl stop NetworkManager # echo >/etc/udev/rules.d/90-eno-fix.rules &>/dev/null # fi } Set_dns() { color_message "info" "---- Start configuring DNS ----" ping -c1 baidu.com &>/dev/null || echo "nameserver 223.5.5.5" >>/etc/resolv.conf # 如果dns无法解析添加一条dns } Set_ntp() { color_message "---- info" "Start configuring NTP ----" yum --debuglevel=1 install chrony -y cat >/etc/chrony.conf </dev/null timedatectl set-timezone "Asia/Shanghai" } Set_ssh() { color_message "info" "---- Start configuring SSH optimize---- " # sed -i '/^#Port/c Port 6022' /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 /etc/rc.local || cat >> /etc/rc.local< /sys/kernel/mm/transparent_hugepage/defrag echo never > /sys/kernel/mm/transparent_hugepage/enabled EOF grep HISTTIMEFORMAT /etc/profile || cat >>/etc/profile </etc/sysctl.conf <> 脚本执行结束... " #reboot