系统初始化

centos 7

#!/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 <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="${ifname}"
UUID="`uuidgen`"
DEVICE="${ifname}"
ONBOOT="yes"
IPADDR=${ipaddr}
PREFIX=${prefix}
GATEWAY=${gateway}
DNS1=${gateway}
DNS2=114.114.114.114
DNS3=8.8.8.8
EOF
systemctl restart network
ping -w2 -c2 www.baidu.com &>/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 <<EOF
$yum_host package.qf.com
$yum_host2 package2.qf.com
EOF
    fi
    curl -o /etc/yum.repos.d/centos7.repo https://download.beyourself.org.cn/repo/centos7-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"
}

remote_yum_make(){
    cd /etc/yum.repos.d/ && \
    rename .repo .repo.bak *.repo &>/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 <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
    color Success "已经优化系统文件打开数、进程打开数"
echo "net.ipv4.ip_forward=1" >> /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 <<EOF
    # export PS1="[\e[32m(\t)\u\e[36m@\h\e[31m\e[0m \W]\$ "
# EOF
    # color Success "修改完成,重新进入终端查看"
    echo {"time": "`date +'%F %T'`"} > /tmp/.init
}
if [ -f /tmp/.init ];then
    color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init"
    exit 0
fi
init
color Success "初始化完成"

centos 9

#!/usr/bin/bash

set -e
color(){
    unset c
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    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 <<EOF
[connection]
id=${ifname}
uuid=`uuidgen`
type=ethernet
autoconnect-priority=-999
interface-name=${ifname}
timestamp=1676740817

[ethernet]

[ipv4]
method=manual
address1=${ipaddr}/${prefix},${gateway}
dns=223.5.5.5

[ipv6]
method=auto
addr-gen-mode=eui64

[proxy]

EOF
nmcli c reload
nmcli c up ${ifname}
ping -w1 -c1 www.baidu.com &>/dev/null && \
return 10 || \
return 20
}


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 <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
    color Success "已经优化系统文件打开数、进程打开数"
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    color Success "已经开启系统路由转发"
    static_addr 
    if [ $? -eq 10 ];then
        color Success "静态ip配置并检测完成"
    else
        color Error  "静态ip配置异常"
    fi
    color Info "正在关闭防火墙、selinux"
    color Warning "注意,这将降低服务器安全性"
    systemctl disable firewalld --now
    if [ `getenforce` = 'Enforcing' ];then
        setenforce 0
    fi
    sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && \
    color Success "配置完成" || color Error "配置异常,请检查"
    dnf install -y epel-release
    dnf makecache
    dnf install -y wget unzip vim yum-utils httpd-tools psacct net-tools nc jq htop bash-completion lrzsz ntpsec && \
    color Success "安装完成" || color Warning "部分包可能失败"
    color Info "开始进行时间校准"
    ntpdate ntp.aliyun.com && \
    # 修改时间到硬件
    clock -w
    #保存时间到硬件
    hwclock -s
    color Success "时间校准完成"
    echo {"time": "`date +'%F %T'`"} > /tmp/.init
}
if [ -f /tmp/.init ];then
    color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init"
    exit 0
fi
init
color Success "初始化完成"

ubuntu 20.04

#!/bin/bash

# Ubuntu 20.04 System Initialization Script

color() {
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    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/netplan/01-network-manager-all.yaml <<EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    ${ifname}:
      addresses:
        - ${ipaddr}/${prefix}
      gateway4: ${gateway}
      nameservers:
        addresses:
          - ${gateway}
          - 114.114.114.114
          - 8.8.8.8
EOF

    netplan apply
    ping -w2 -c2 www.ubuntu.com &>/dev/null && return 10 || return 20
}

init() {
    color Info "当前用户 $USER"
    color Info "当前时间 `date "+%F %X"`"
    echo 3 >/proc/sys/vm/drop_caches
    color Info "当前内存 $(free -m | awk 'NR==2{print $4}')M"
    color Info "当前核心 $(nproc)个"
    color Info "启动时长 $(uptime -p)"
    color Success "开始配置静态ip"
    
    static_addr 
    if [ $? -eq 10 ];then
        color Success "静态ip配置并检测完成"
    else
        color Error  "静态ip配置异常"
    fi

    color Info "正在关闭防火墙、apparmor"
    systemctl disable ufw --now
    systemctl disable apparmor --now

    color Info "配置yum源, 此操作将备份您的历史源"
    apt-get update && apt-get upgrade -y
    color Success "本地源配置完成"

    color Info "开始安装常用软件"
    apt-get install -y wget unzip vim net-tools ntpdate jq htop bash-completion

    color Info "开始进行时间校准"
    ntpdate ntp.aliyun.com
    color Success "时间校准完成"
}

init
color Success "初始化完成"