干货 | PXE+kickstart无人值守批量装机(实战部署)
在这里,白嫖有罪。把它转给有你身边的朋友。共同进步。
安装环境
安装dhcp服务器
[root@localhost ~]# yum -y install dhcp
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.180 10.0.0.200;
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
next-server 10.0.0.61; # 这里是tfp服务器的地址
filename "/pxelinux.0";
}
(这一步一般不用做,除非你有特殊要求)如果有多个网卡,默认监听第一个,当然也可以指定监听网卡:
[root@localhost ~]# vim /etc/sysconfig/dhcpd
……
DHCPDARGS=ens34 # 指定监听网卡
启动dhcp服务
[root@localhost ~]# systemctl start dhcpd
[root@localhost ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2022-06-26 17:19:25 CST; 4s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 1724 (dhcpd)
Status: "Dispatching packets..."
CGroup: /system.slice/dhcpd.service
└─1724 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: No subnet declaration for ens34 (172.16.1.3).
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: ** Ignoring requests on ens34. If this is not what
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: you want, please write a subnet declaration
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: in your dhcpd.conf file for the network segment
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: to which interface ens34 is attached. **
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]:
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: Listening on LPF/ens33/00:0c:29:12:6c:14/10.0.0.0/24 # 如果你的机器有多块网卡,注意这里dhcp是监听的哪块网卡。
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: Sending on LPF/ens33/00:0c:29:12:6c:14/10.0.0.0/24
Jun 26 17:19:25 localhost.localdomain dhcpd[1724]: Sending on Socket/fallback/fallback-net
Jun 26 17:19:25 localhost.localdomain systemd[1]: Started DHCPv4 Server Daemon.
再次验证dhcp服务是否启动
[root@localhost ~]# ss -lntup | grep 67
udp UNCONN 0 0 *:67 *:* users:(("dhcpd",pid=1724,fd=7))
[root@localhost ~]# tail -f /var/log/messages
Jun 26 17:19:25 localhost dhcpd: No subnet declaration for ens34 (172.16.1.3).
Jun 26 17:19:25 localhost dhcpd: ** Ignoring requests on ens34. If this is not what
Jun 26 17:19:25 localhost dhcpd: you want, please write a subnet declaration
Jun 26 17:19:25 localhost dhcpd: in your dhcpd.conf file for the network segment
Jun 26 17:19:25 localhost dhcpd: to which interface ens34 is attached. **
Jun 26 17:19:25 localhost dhcpd:
Jun 26 17:19:25 localhost dhcpd: Listening on LPF/ens33/00:0c:29:12:6c:14/10.0.0.0/24 # 再次确认dhcp用的是哪块网卡
Jun 26 17:19:25 localhost dhcpd: Sending on LPF/ens33/00:0c:29:12:6c:14/10.0.0.0/24
Jun 26 17:19:25 localhost dhcpd: Sending on Socket/fallback/fallback-net
Jun 26 17:19:25 localhost systemd: Started DHCPv4 Server Daemon. t
安装tftp服务器
[root@localhost ~]# yum -y install tftp-server xinetd
[root@localhost ~]# vim /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no #开启tftp服务
per_source = 11
cps = 100 2
flags = IPv4
}
[root@localhost ~]# systemctl start xinetd # 启动ftp服务
[root@localhost ~]# systemctl enable xinetd
安装http服务
# 把光盘连接到服务器上,然后:
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mkdir -p /var/www/html/CentOS7
[root@localhost ~]# mount /dev/cdrom /var/www/html/CentOS7
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
准备预启动文件
[root@localhost ~]# cd /var/lib/tftpboot
[root@localhost tftpboot]# yum -y install syslinux # 为了得到pxelinux.0预启动文件
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
[root@localhost tftpboot]# ls
pxelinux.0
配置default引导文件:
[root@localhost ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
#把安装镜像里的引导文件拷贝过来,方便修改。注意,pxelinux.cfg/default这个相对路径和名称不能修改,因为他是pxelinux.0定义好的。
[root@localhost ~]# cp /var/www/html/CentOS7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
# 拷贝其他引导文件
[root@localhost ~]# cp /var/www/html/CentOS7/isolinux/* /var/lib/tftpboot/
确认http是否部署成功
查看http://10.0.0.61/CentOS7/如下
使用curl也可以正确返回:
curl http://10.0.0.61/CentOS7/
修改引导文件
[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default linux
timeout 600
~
display boot.msg
~
# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear7L, 247C 1,1 All
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13
# Border Area
menu color border * #00000000 #00000000 none
# Selected item
menu color sel 0 #ffffffff #00000000 none
# Title bar
menu color title 0 #ff7ba3d0 #00000000 none
# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none
# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none
# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none
# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none
# Help text
menu color help 0 #ffffffff #00000000 none
# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none
# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
menu label ^Install CentOS 7
menu default
kernel vmlinuz
append initrd=initrd.img inst.stage2=http://10.0.0.61/CentOS7/ inst.ks=http://10.0.0.61/ks_config/CentOS7-ks.cfg quiet
# 其余label全部删除
menu separator # insert an empty line
menu separator # insert an empty line
menu separator # insert an empty line
menu end
准备自动应答文件
[root@localhost ~]# cd /var/www/html/
# 下面这个ks_config目录可以不创建,但是创建的ks文件放在哪,要和default文件中inst.ks配置项保持一致并且保证可以通过http访问到。
[root@localhost html]# mkdir ks_config
[root@localhost html]# cd ks_config/
# 生成密码
[root@localhost ks_config]# python -c 'import crypt; print(crypt.crypt("123456"))'
$6$UoRe81QPnOaqc5Yi$E7OL7FF6ez0QHqg2ZVuBgPZ/wdzywUwkjs2jZ0uwZw8/ISlp6E92tPlUcF3Ac3EmxXh46EHB7KV1DTVu.mtyX.
# 编辑kickstart文件。(根据root目录的anaconda-ks.cfg文件改写)
[root@m01 ks_config]# cat CentOS7-ks.cfg
install
url --url="http://10.0.0.61/CentOS7/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet net.ifnames=0 biosdevname=0"
network --bootproto=dhcp --device=ens33 --onboot=yes --hostname=pxetest --activate
#network --bootproto=static --device=ens34 --ip=10.0.0.120 --netmask=255.255.255.0 --activate
#network --hostname=Cobbler
#network --bootproto=dhcp --device=eth1 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw --iscrypted $6$UoRe81QPnOaqc5Yi$E7OL7FF6ez0QHqg2ZVuBgPZ/wdzywUwkjs2jZ0uwZw8/ISlp6E92tPlUcF3Ac3EmxXh46EHB7KV1DTVu.mtyX. # 设置root用户的密码
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@^minimal
@compat-libraries
@debugging
@development
tree
nmap
net-tools
iproute
telnet
wget
vim
bash-completion
%end
%post
echo "export PS1='[\u@\h \w]\$ '" >>/etc/profile
systemctl disable postfix.service
systemctl disable NetworkManager
%end
到此,pxe+kickstart环境全部搭建完成。
此时,创建一个空的虚拟机(至少2GB内存,如果报错,尝试调大内存后再测试),开机即可自动完成系统安装。
错误解决:
https://bugzilla.redhat.com/show_bug.cgi?id=1595369
在安装新系统的时候会报错
93 342M 93 319M 0 0 4955k 0 0:01:10 0:01:06 0:00:04 3326k
[ 117.030748] dracut-initqueue[629]: curl: (23) Failed writing body (10176 != 16176)
[ 118.551988] dracut-initqueue[629]: mount: wrong fs type, bad option, bad superblock on /dev/loop0,
[ 118.553728] dracut-initqueue[629]: missing codepage or helper program, or other error
[ 118.557054] dracut-initqueue[629]: In some cases useful info is found in syslog - try
[ 118.558610] dracut-initqueue[629]: dmesg | tail or so.
[ 118.572160] dracut-initqueue[629]: umount: /run/initramfs/squashfs: not mounted
[ 118.607805] dracut-initqueue[629]: /lib/anaconda-lib.sh: line 110: printf: write error: No space left on device
解决办法:
调高虚拟机的内存,至少2GB。
自行练习:
自动化安装时,显示图形界面。
参考文档:
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html-single/installation_guide/#chap-kickstart-installations
https://www.linuxprobe.com/chapter-19.html
https://www.zyops.com/autoinstall-kickstart/
https://docs.centos.org/en-US/centos/install-guide/Kickstart2/#sect-kickstart-syntax
干货 | PXE+kickstart无人值守批量装机(原理与架构)
ifconfig已淘汰,ip登场
Linux 网络状态工具 ss 命令详解
这次终于搞明白VLAN技术了
除每周二、四、六定期更新的《Linux云计算一站式教程》以外,其余时间雷哥会推送一些工作中遇到的小知识、实战经验总结的文章。后续都会收录在“实战经验”合集中。