前期准备

  1. 在虚拟化环境中部署CentOS 8,包含Hyper-V、Esxi、Virtualbox等;
  2. 使用mini组件部署,大约会安装378个软件包;
  3. 安装过程中添加简体中文支持、设定时区为上海、使用硬盘自动分区、关闭kdump;
  4. 设定root密码(后期关闭root的ssh访问权限),并添加远程ssh访问管理员账户;
  5. 设定网络开机自动启动;

系统调整

  1. 安装工具

    # 安装更新
    yum makecache
    yum upgrade -y
    # 安装系统工具
    yum install -y wget curl zip unzip vim mailx mlocate telnet net-tools bash-completion tmux
    yum install -y chrony rsync iotop git screen tree open-vm-tools yum-utils
    updatedb && ldconfig
    # 配置默认主机名
    hostnamectl set-hostname vlnx000000
    # 配置模板机网络别名为eth0,如果是Hyper-V的话,可以忽略
    nmcli c m ens160 con-name eth0
    # 设定模板机IP
    nmcli c m eth0 ipv4.method manual ipv4.addresses 192.168.248.245/24 ipv4.dns 192.168.248.2 ipv4.gateway 192.168.248.2
    nmcli c d eth0 && nmcli c u eth0
    # 添加IP地址配置脚本
    touch ipset.sh && chmod +x ipset.sh
    vim ipset.sh
    
    #!/bin/bash
    #该脚本用于设置网卡的IP地址
    
    read -p "请输入设置IP地址:" IP
    read -p "请输入设置的网关:" GW
    #read -p "请输入设置的子网掩码:" MASK
    read -p "请输入首选DNS服务器:" D1
    #read -p "请输入备选DNS服务器:" D2
    read -p "请输入主机名:" MachineNAME
    
    nmcli c m eth0 ipv4.method manual ipv4.address $IP ipv4.gateway $GW ipv4.dns $D1
    nmcli c d eth0
    nmcli c u eth0
    hostnamectl set-hostname $MachineNAME
  2. 系统调整

    # 添加华为云的YUM镜像
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo
    # 关闭SELINUX
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    sed -i 's/vmlinuz.*/& selinux=0/g' /boot/grub2/grub.cfg
    setenforce 0
    
    # SSHD服务优化,设定如下项
    
    LoginGraceTime 2m
    PermitRootLogin no
    StrictModes yes
    MaxAuthTries 6
    MaxSessions 10
    
    PermitEmptyPasswords no
    PasswordAuthentication yes
    
    GSSAPIAuthentication no
    GSSAPICleanupCredentials no
    
    X11Forwarding no
    UseDNS no
    Compression delayed
    Banner none
    
    # 开启网络BBR模块
    echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
    echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
    sysctl -p
    
    # 调整SWAP使用策略
    echo vm.swappiness = 10 >> /etc/sysctl.conf
  3. 监控工具

    # 安装EPEL源
    yum install -y epel-release
    
    # 添加华为云源
    sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
    sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
    sed -i "s@https\?://download.example/pub@https://repo.huaweicloud.com@g" /etc/yum.repos.d/epel.repo
    
    yum makecache
    yum upgrade -y
    
    # 更新和安装工具
    yum install -y htop iftop atop smem 
    
    # 关闭EPEL
    yum-config-manager --disable epel
    yum-config-manager --disable epel-modular
    yum-config-manager --disable epel-testing-modular
  4. 调整启动参数,取消开机画面

    # vim /etc/default/grub
    # 设定等待时间为3秒
    GRUB_TIMEOUT=3
    GRUB_DEFAULT=saved
    GRUB_DISABLE_SUBMENU=true
    GRUB_TERMINAL_OUTPUT="console"
    # 删除原有的rhgb选项,关闭开机画面,显示服务启动状态,保留quiet避免输出过多硬件自检内容
    GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
    GRUB_DISABLE_RECOVERY="true"
    grub2-mkconfig -o /boot/grub2/grub.cfg
  5. 启动时间服务

    # 安装时间同步服务器
    yum install -y chrony
    
    # 添加阿里云NTP服务器:w
    cat >> /etc/chronyd.conf <<EOF
    pool time.pool.aliyun.com iburst
    pool cn.pool.ntp.org iburst
    
    allow 192.168.248.0/24
    EOF
    
    # 设置时区为亚洲/上海
    timedatectl set-timezone Asia/Shanghai
    
    # 重启时间服务
    systemctl enable chronyd
  6. 清理交付

    # 同步数据
    sync
    ldconfig
    
    # 删除主机ID
    >/etc/machine-id
    
    # 删除ssh公钥
    rm -rf /etc/ssh/*key*
    
    # 清理yum缓存
    yum clean all
    
    #清理日志
    logrotate -f /etc/logrotate.conf
    rm -f /var/log/*-???????? /var/log/*.gz
    rm -f /var/log/dmesg.old
    rm -rf /var/log/anaconda*
    cat /dev/null > /var/log/audit/audit.log
    cat /dev/null > /var/log/wtmp
    cat /dev/null > /var/log/lastlog
    cat /dev/null > /var/log/grubby
    
    unset HISTFILE
    rm -rf ~root/.ssh/
    
    # 清理历史记录
    history -c
    
    # 使用外部工具关机,避免留下shutdown历史记录

    导出模板

    • 导出为ova模板,或直接在控制台使用。