基本思路沿用centos8的制作方法,制作CentOS8的黄金模板,以下内容有部分改动。懒得脚本化了,就这样吧。

系统部署

软件安装这个没啥好说的,只是注意几点:

  1. 使用英文部署,可以在语言支持选项中添加简体中文的支持;
  2. 需要在部署一开始添加localhost的主机名,否则硬盘分区和主机名会有IP头的信息;
  3. 账号选项采用默认配置,添加非root管理员账号,使用uid和gid为1000,并添加到wheel组;
  4. 不开启root远程访问和锁定root账号,不允许root登陆,后续按需开启

系统设置

# 修改系统源
sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' -i.bak /etc/yum.repos.d/rocky*.repo

# 系统更新
dnf update -y
# 安装常用软件
dnf install -y wget curl zip unzip vim mailx mlocate telnet bash-completion tmux
dnf install -y chrony rsync git screen tree open-vm-tools yum-utils lrzsz
dnf install -y device-mapper-persistent-data lvm2 psmisc net-tools
dnf install -y bind-utils yum-utils python3-dnf-plugin-versionlock
dnf install -y lnav nc lsof ncdu dstat git
dnf remove -y podman*

# 安装EPEL
dnf install -y epel-release
# 修改epel源
sed -e 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' -e 's|^metalink=|#metalink=|g' -i.bak /etc/yum.repos.d/epel*
# 刷新缓存
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.bak /etc/yum.repos.d/backup
mv /etc/yum.repos.d/epel-* /etc/yum.repos.d/backup
dnf makecache

# 安装监控工具
dnf install -y htop iftop atop smem

# 关闭SELINUX
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

# 开启网络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

# 打开连接数限制
echo "* hard nofile 64000" >> /etc/security/limits.conf
echo "* soft nofile 64000" >> /etc/security/limits.conf
echo "root hard nofile 64000" >> /etc/security/limits.conf
echo "root soft nofile 64000" >> /etc/security/limits.conf

# 安装时间同步服务器
dnf install -y chrony
systemctl enable chronyd
# 设置时区为亚洲/上海
timedatectl set-timezone Asia/Shanghai

# 添加阿里云NTP服务器
cat >> /etc/chronyd.conf <<EOF
pool time.pool.aliyun.com iburst
pool cn.pool.ntp.org iburst

allow 192.168.0.0/16
EOF

# 开启root的访问
sed -e 's|#PermitRootLogin prohibit-password|PermitRootLogin yes|g' -i.bak /etc/ssh/sshd_config

# 修改开机选项
# 设定等待时间为3秒
# 删除原有的rhgb选项,关闭开机画面,显示服务启动状态,保留quiet避免输出过多硬件自检内容
sed -e 's|5|3|g' -e 's|"crashkernel=auto rhgb quiet"|"crashkernel=auto quiet"|g' -i.bak /etc/default/grub
# 生成grub引导配置
grub2-mkconfig -o /boot/grub2/grub.cfg

# 配置vimrc
cat >> ~/.vimrc <<EOF
set autoindent
set nobackup
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set number
set ruler
set nocompatible
set syntax=on
set noeb
EOF

# 加载配置文件
source ~/.vimrc
cp ~/.vimrc /etc/skel/

# 更新系统缓存
updatedb && ldconfig

# 配置默认主机名
hostnamectl set-hostname Rocky

# 设置网卡属性,取消DHCP分配,使用指定部署主机IP
nmcli c m ens160 ipv4.meth man ipv4.addr 192.168.10.192/24 ipv4.gate 192.168.10.2 ipv4.dns 192.168.10.2
nmcli c d ens160
nmcli c u ens160

# 删除主机ID
>/etc/machine-id

# 删除ssh公钥
rm -rf /etc/ssh/*key*

# 清理yum缓存
dnf clean all

# 清理旧内核
dnf remove -y --oldinstallonly --setopt installonly_limt=1 kernel

# 添加Github解析地址(可选)
# sh -c 'sed -i "/# GitHub520 Host Start/Q" /etc/hosts && curl https://raw.hellogithub.com/hosts >> /etc/hosts'

#清理日志
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

# 使用空格+poweroff进行关机,避免留下shutdown历史记录
poweroff

系统交付

需要注意的事项:

  1. 主机制作时需要看一下配置的兼容性级别;
  2. 模板需要在虚拟机的设置中删除网卡的MAC地址。

个人感觉RockyLinux 9是CentOS7的优选替代品。