Linux基础篇、第五章_01利用 Cobbler 实现 CentOS 7 与 Rocky 9.5 自动化安装全攻略

Linux_基础篇

欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神!

题目:Cobbler 实现 CentOS 7 与 Rocky 9.5 自动化安装全攻略

版本号 : 1.0,0
作者 : @老王要学习
日期 : 2025.06.02
适用环境: Rocky9.5

文档说明

本文档围绕使用 Cobbler 自动化安装 CentOS 7 和 Rocky 9.5 展开。详细介绍了环境准备,涵盖硬件与软件要求,包括安装 Cobbler 及依赖、修改配置文件等步骤。还阐述了导入镜像、自定义应答文件过程,最后说明了自动化安装的操作及注意事项

环境准备

硬件要求

  • 服务器: 2核CPU、2GB内存,20GB硬盘空间
  • 网络: 确保服务器具有固定的IP地址,并且防火墙允许FTP端口(默认22端口)的通信

软件要求

  • 操作系统:Centos7
  • FTP软件:SecureCRT
  • 软件包:Cobbler
  • 软件包:CentOS7
  • 软件包:Rocky9.5

一、环境准备

1.1安装Cobbler即依赖包

复制代码
# 安装epel源
dnf install -y epel-release

# 安装Cobbler即依赖
dnf -y install cobbler dhcp-server

# 查看Cobbler安装相关文件
rpm -ql cobbler

1.2修改Cobbler主配置文件

复制代码
# 生成SHA512加密的密码
openssl passwd -6 "1"
#输出如下: 
$6$RlxFkgfdBGPu16N1$2k1u73/UbFJjD3FpGA7PaHHsw2V6./WYfmr1fxmEre2SE39wL609S75hPfD49UeuOLGKyllGKXFH46r.PVG6P.

# 修改配置文件如下:
vim /etc/cobbler/settings.yaml

# 配置root登录密码
default_password_crypted:"$6$RlxFkgfdBGPu16N1$2k1u73/UbFJjD3FpGA7PaHHsw2V6./WYfmr1fxmEre2SE39wL609S75hPfD49UeuOLGKyllGKXFH46r.PVG6P."

# 启用 Cobbler 对 DHCP 服务的管理
manage_dhcp: true
manage_dhcp_v4: true

# 修改Cobbler地址为本机ip
server:192.168.174.10

# 修改tftp地址为本机地址
next_server_v4: 192.168.174.10

# 启用 Cobbler 对 tftp 服务的管理
manage_tftpd: true

# 启用 Cobbler 对 PXE 服务的管理
pxe_just_once: true

1.3DHCP 配置(自动生成时的网段调整)

复制代码
vim /etc/dhcp/dhcpd.conf
#修改如下: 
subnet 192.168.174.0 netmask 255.255.255.0 {
     option routers             192.168.174.2;
     option domain-name-servers 223.5.5.5;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.174.10 192.168.174.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                192.168.174.10;

1.4启动Cobbler同步配置

复制代码
systemctl enable --now cobblerd
#输出如下: 
Created symlink /etc/systemd/system/multi-user.target.wants/cobblerd.service → /usr/lib/systemd/system/cobblerd.service.

1.5启动dhcp、tftp、http服务

复制代码
systemctl enable --now tftp httpd dhcpd
#输出如下: 
Created symlink /etc/systemd/system/sockets.target.wants/tftp.socket → /usr/lib/systemd/system/tftp.socket.
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code.
See "systemctl status dhcpd.service" and "journalctl -xeu dhcpd.service" for details.

二、导入镜像

2.1创建光盘挂载点

复制代码
mkdir /mnt/rocky9.5
mkdir /mnt/centos7

2.2导入镜像到root

复制代码
Rocky-9.5-x86_64-minimal.iso
CentOS-7-x86_64-DVD-2009.iso

2.3挂载光盘到目录

复制代码
# 挂载Rocky9.5
mount /root/Rocky-9.5-x86_64-minimal.iso /mnt/rocky9.5/
#输出如下: 
mount: /mnt/rocky9.5: WARNING: source write-protected, mounted read-only.

# 挂载Centos7.9
mount /root/CentOS-7-x86_64-DVD-2009.iso /mnt/centos7/
#输出如下: 
mount: /mnt/centos7: WARNING: source write-protected, mounted read-only.

2.4导入镜像

复制代码
# 导入Rocky9.5
cobbler import --name=Rocky-9.5-x86_64 --path=/mnt/rocky9.5 --arch=x86_64
#输出如下: (最后几行)
https://cobbler.github.io\nTIMEOUT 200\nTOTALTIMEOUT 6000\nONTIMEOUT local\n\nLABEL local\n        MENU LABEL (local)\n        MENU DEFAULT\n        LOCALBOOT -1\n\nLABEL Rocky-9.5-x86_64\n\tMENU LABEL Rocky-9.5-x86_64\n\tkernel /images/Rocky-9.5-x86_64/vmlinuz\n\tappend initrd=/images/Rocky-9.5-x86_64/initrd.img  inst.ks.sendmac inst.ks=http://192.168.174.10/cblr/svc/op/autoinstall/profile/Rocky-9.5-x86_64 inst.repo=http://192.168.174.10/cblr/links/Rocky-9.5-x86_64\n\tipappend 2\nMENU end\n', 'grub': {}}'
*** TASK COMPLETE ***

# 导入Centos7
cobbler import --name=centos7 --path=/mnt/centos7 --arch=x86_64
#输出如下: (最后几行)
Writing template files for centos7-x86_64
Writing template files for centos7-x86_64
https://cobbler.github.io\nTIMEOUT 200\nTOTALTIMEOUT 6000\nONTIMEOUT local\n\nLABEL local\n        MENU LABEL (local)\n        MENU DEFAULT\n        LOCALBOOT -1\n\nLABEL Rocky-9.5-x86_64\n\tMENU LABEL Rocky-9.5-x86_64\n\tkernel /images/Rocky-9.5-x86_64/vmlinuz\n\tappend initrd=/images/Rocky-9.5-x86_64/initrd.img  inst.ks.sendmac inst.ks=http://192.168.174.10/cblr/svc/op/autoinstall/profile/Rocky-9.5-x86_64 inst.repo=http://192.168.174.10/cblr/links/Rocky-9.5-x86_64\n\tipappend 2\nLABEL centos7-x86_64\n\tMENU LABEL centos7-x86_64\n\tkernel /images/centos7-x86_64/vmlinuz\n\tappend initrd=/images/centos7-x86_64/initrd.img  inst.ks.sendmac inst.ks=http://192.168.174.10/cblr/svc/op/autoinstall/profile/centos7-x86_64 inst.repo=http://192.168.174.10/cblr/links/centos7-x86_64\n\tipappend 2\nMENU end\n', 'grub': {}}'
*** TASK COMPLETE ***

2.5检查镜像导入信息

复制代码
cobbler distro list
#输出如下: 
Rocky-9.5-x86_64
centos7-x86_64

cobbler profile list
#输出如下: 
Rocky-9.5-x86_64
centos7-x86_64

2.6重启cobbler,同步配置

复制代码
systemctl restart cobblerd
cobbler sync
#输出如下:(最后几行)
Writing template files for centos7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
processing boot_files for distro: Rocky-9.5-x86_64
processing boot_files for distro: centos7-x86_64
cleaning link caches
running post-sync triggers
running: ['dhcpd', '-t', '-q']
received on stdout: 
running: ['systemctl', 'restart', 'dhcpd']
received on stdout: 
*** TASK COMPLETE ***

三、自定义应答文件

3.1Rocky9.5应答文件

复制代码
cat>/var/lib/cobbler/templates/rocky9.ks<<LW
# 基础配置
ignoredisk --only-use=nvme0n1
clearpart --all --initlabel
text
url --url=$tree
reboot

# 本地化设置
keyboard --vckeymap=cn --xlayouts='cn'
lang en_US.UTF-8
timezone Asia/Shanghai --isUtc --nontp

# 安全与网络配置
selinux --disabled
firewall --disabled
network --bootproto=static --ip=192.168.174.20 --netmask=255.255.255.0 --gateway=192.168.174.2 --nameserver=8.8.8.8

# 用户与服务配置
rootpw --iscrypted $6$RlxFkgfdBGPu16N1$2k1u73/UbFJjD3FpGA7PaHHsw2V6./WYfmr1fxmEre2SE39wL609S75hPfD49UeuOLGKyllGKXFH46r.PVG6P.
firstboot --enable
skipx
services --disabled="chronyd"

# 磁盘分区
zerombr
part /boot --fstype="ext4" --ondisk=nvme0n1 --size=1024
part /swap --fstype="swap" --ondisk=nvme0n1 --size=2048
part / --fstype="xfs" --ondisk=nvme0n1 --grow --size=1

# 软件包与附加组件
%packages
@^minimal-environment
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

# 安装后脚本(% post)
%post
#!/bin/sh
echo "PermitRootLogin yes" >>/etc/ssh/sshd_config
systemctl restart sshd
%end

# 密码策略(% anaconda)
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
LW 

3.2应答文件分析

1.基础配置

  • ignoredisk --only-use=nvme0n1 :仅使用 NVMe 磁盘(nvme0n1)进行安装,忽略其他磁盘
  • clearpart --all --initlabel:清除所有分区并初始化磁盘标签(相当于重新分区)
  • text:使用文本模式安装,而非图形界面
  • url --url=$tree :从指定 URL 安装系统,$tree 是 kickstart 变量,通常指向安装源路径
  • reboot:安装完成后自动重启

2.本地化设置

  • keyboard --vckeymap=cn --xlayouts='cn':键盘布局设为中文(CN)
  • lang en_US.UTF-8:系统语言为英文
  • timezone Asia/Shanghai --isUtc --nontp:设置为上海时区(UTC+8),启用 UTC 时间,禁用 NTP 自动同步

3.安全与网络配置

  • selinux --disabled:禁用 SELinux
  • firewall --disabled:防火墙(生产环境需谨慎)
  • network --bootproto=static --ip=192.168.174.20 --netmask=255.255.255.0 --gateway=192.168.174.2 --nameserver=8.8.8.8:静态 IP 配置(IP:192.168.174.20,网关:192.168.174.2,DNS:8.8.8.8

4.用户与服务配置

  • rootpw --iscrypted $6$RlxFkgfdBGPu16N1$...:Root密码使用加密后的密码(已通过 openssl passwd -6 生成)
  • firstboot --enable:启用 Anaconda 初始配置向导(firstboot
  • skipx:跳过 X Window 系统配置(无桌面环境)
  • services --disabled="chronyd":禁用 chronyd(时间同步服务,因时区设置中禁用了 NTP)

5.磁盘分区

  • zerombr:清除主引导记录(MBR)
  • part /boot --fstype="ext4" --ondisk=nvme0n1 --size=1024:boot分区1GB,ext4 文件系统(启动分区)
  • part /swap --fstype="swap" --ondisk=nvme0n1 --size=2048:swap分区2GB(交换空间,用于虚拟内存)
  • part / --fstype="xfs" --ondisk=nvme0n1 --grow --size=1:根分区,XFS 文件系统,--grow 表示使用剩余全部空间

6.软件包与附加组件

  • %packages

  • @^minimal-environment

  • %end

  • 软件包组 :仅安装 minimal-environment(最小化环境,无桌面)

  • %addon com_redhat_kdump --enable --reserve-mb='auto'

  • %end

  • kdump:启用内核崩溃转储(kdump),自动预留内存用于捕获内核崩溃信息

7.安装后脚本(% post)

  • %post

  • #!/bin/sh

  • echo "PermitRootLogin yes" >>/etc/ssh/sshd_config

  • 允许 root 用户通过 SSH 登录,并重启 SSH 服务

  • systemctl restart sshd

  • %end

  • SSH 服务重启

8.密码策略(% anaconda)

  • %anaconda
  • pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
  • pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
  • pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
  • %end
  • 密码策略
    • root:密码最小长度 6,质量要求低,禁止修改,不能为空。
    • user:允许空密码(emptyok),其他同 root。
    • luks:加密分区密码策略,要求同 root。

3.3Centos7应答文件

复制代码
cat >/var/lib/cobbler/templates/centos7.ks <<LW

四、自动化安装Rocky9.5(Centos7同理)

  • 唯一区别就是在创建虚拟机时选择不同

4.1创建新的虚拟机进行安装

4.2主机需设置为4G运行内存

  • 不选择4G会报错如下:
  • 客户机操作系统已禁用 CPU。请关闭或重置虚拟机。

4.3选择好4G内存启动

4.4选择我们自己创建的镜像

4.5等待安装完成即可

  • 如果出现报错、进度卡主、查看应答文件,出错肯定是应答文件写错所导致
  • 基本上到下面↓的步骤就已经安装成功了