#!/usr/bin/bash #set -e yum_host=10.36.172.9 yum_host2=10.36.174.100 color(){ unset c declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34) #echo -e "\033[${c[$1]}m[`date +%T`]($1) $2\033[0m" printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2" sleep 0.5 } static_addr(){ ifname=$(ip -f inet a | awk '/^2/{print $2}') ifname=$(echo ${ifname/:/}) ipaddr=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $1}') prefix=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $2}') gateway=$(ip r |awk '/default/{print $3}') cat > /etc/sysconfig/network-scripts/ifcfg-$ifname </dev/null && \ return 10 || \ return 20 } local_yum_make(){ cd /etc/yum.repos.d/ && \ rename .repo .repo.bak *.repo &>/dev/null grep "$yum_host package.qf.com" /etc/hosts &>/dev/null if [ $? -ne 0 ];then cat >> /etc/hosts </dev/null curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo yum repolist && yum clean all && yum makecache && \ color Success "阿里源配置完成" || color Error "yum配置错误,可以\ncd /etc/yum.repos.d/ && rm -rf centos7.repo && rename .repo.bak .repo *.repo.bak \n使用手动恢复历史yum" } init(){ # 系统信息采集 color Info "当前用户 $USER" color Info "当前时间 `date "+%F %X"`" echo 3 >/proc/sys/vm/drop_caches color Info "当前内存 $(free |awk 'NR==2{print $4/1024}')M" color Info "当前核心 $(grep -E '^processor' /proc/cpuinfo|wc -l)个" color Info "核心品牌 $(grep -E '^model name' /proc/cpuinfo |head -n 1 |awk -F: '{print $2}')" color Info "启动时长 $(uptime |awk -F',' '{print $1}')" color Success "开始配置静态ip" cat >>/etc/security/limits.conf <> /etc/sysctl.conf color Success "已经开启系统路由转发" sed -i 's/#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config color Success "已经优化ssh服务连接速度" static_addr if [ $? -eq 10 ];then color Success "静态ip配置并检测完成" else color Error "静态ip配置异常" fi color Info "正在关闭防火墙、selinux" color Warning "注意,这将降低服务器安全性" systemctl disable firewalld postfix --now if [ `getenforce` = 'Enforcing' ];then setenforce 0 fi sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && \ color Success "配置完成" || color Error "配置异常,请检查" color Info "配置yum源, 此操作将备份您的历史源" ping -w 2 -c 2 $yum_host &>/dev/null if [ $? -eq 0 ];then local_yum_make else remote_yum_make fi color Info "开始安装常用软件" yum install -y wget unzip vim yum-utils httpd-tools psacct net-tools nc ntpdate jq htop bash-completion && \ color Success "安装完成" || color Warning "部分包可能失败" color Info "开始进行时间校准" ntpdate ntp.aliyun.com && \ # 修改时间到硬件 clock -w #保存时间到硬件 hwclock -s color Success "时间校准完成" # color Info "修改终端前缀显示" # cat > /etc/profile.d/psdiy.sh < /tmp/.init } if [ -f /tmp/.init ];then color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init" exit 0 fi init color Success "初始化完成"