实验:PXE无人值守批量部署系统

椰子ya Linux 2020-11-07

通过vm模拟实验环境,均要设置为仅主机模式,且关闭vm虚拟机的dhcp服务。
一、配置PXE服务器
1、安装DHCP、tftp服务

[root@localhost /]# yum -y install dhcp-server 
[root@localhost /]# yum -y install xinetd tftp-server 

2、配置dhcp服务器
配置文件位于/etc/dhcp/dhcpd.conf

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
next-server 192.168.1.253;   #tftp地址
filename "pxelinux.0";       #文件名


subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.52 192.168.1.60;
  option domain-name-servers 119.29.29.29;
  option domain-name "internal.example.org";
  option routers 192.168.1.1;
  option broadcast-address 192.168.1.255;
  default-lease-time 600;
  max-lease-time 7200;
}

3、配置tftp,由于tftp由xinetd代管,所以要安装xinetd,并在/etc/xinetd.d/tftp创建启动文件

cd /etc/xinetd.d/
vim tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args              = -s /var/lib/tftpboot  #根目录
        disable                 = no
        per_source              = 11
        cps                    = 100 2
        flags                   = IPv4

}

4、关闭selinux和防火墙
查看selinux状态

[root@localhost ~]# getenforce 
Enforcing

临时关闭

setenforce 0

永久关闭

vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  #disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

5、获取pxelinux.0网络引导程序
通过 yum provides "*/"命令查找pxelinux.0所在哪个包

 yum provides "*/pxelinux.0"
上次元数据过期检查:0:29:53 前,执行于 2020年11月07日 星期六 19时50分04秒。
syslinux-nonlinux-6.04-4.el8.noarch : SYSLINUX modules which aren't run from
                                    : linux.
仓库        :@System
匹配来源:
文件名    :/usr/share/syslinux/pxelinux.0

syslinux-nonlinux-6.04-4.el8.noarch : SYSLINUX modules which aren't run from
                                    : linux.
仓库        :BaseOS
匹配来源:
文件名    :/usr/share/syslinux/pxelinux.0

syslinux-tftpboot-6.04-4.el8.noarch : SYSLINUX modules in /tftpboot, available
                                    : for network booting
仓库        :BaseOS
匹配来源:
文件名    :/tftpboot/pxelinux.0

安装
yum -y install syslinux-nonlinux-6.04-4.el8.noarch

拷贝文件至/var/lib/tftpboot目录,也就是tftp根目录中

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
``
6、挂载系统光盘

mkdir /media
mount /dev/cdrom /media
7、拷贝内核iso.linux.cfg至/var/lib/tftpboot/pxelinux.cfg/文件夹下

mkdir /var/lib/tftpboot/pxelinux.cfg
cp /media/isolinux/isolinux.cfg  /var/lib/tftpboot/pxelinux.cfg/default  
cd /media/isolinux
cp * /var/lib/tftpboot

isolinux.cgf或者default文件详解
到此处已经可以自动进入启动项。
然而现在还不能算自动部署,还需要进行设置。
下面开始无人值守
1、安装system-config-kickstart
由于CentOS8.0中没有system-config-kickstart包,所以无法通过工具生成ks文件,需要手动生成,例子中root用户和新建的gongjh用户的密码都为‘`123qwe’

[root@localhost ~]# vim /var/www/html/ks.cfg
#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information
clearpart --all            #删除所有分区
# Use graphical install
graphical
url     --url="http://192.168.1.253/RHEL/"            #指定安装URL
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8
# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=localhost.localdomain
# Root password    “`123qwe”            #root用户的密码"`123qwe"
rootpw --iscrypted $6$lYV8xqOfEWbJZ/8Y$YXDve/YNwljyh6BnrMHkKm.18kyUqZkHBpA4DHA/nEhXckJfNPLpclgNcQUS64XQSDZRAdlWzjPsI8sCe1dPo0
# X Window System configuration information
xconfig  --startxonboot
# Run the Setup Agent on first boot
#firstboot disable        #初次启动设置
firstboot --disable
# System services
services --enabled="chronyd"
# System timezone
timezone America/New_York --isUtc
#Reboot after installation    #安装完成后自动重启
reboot
# License agreement        #同意授权协议
eula --agreed
#Add a new user named tyschool password “`123qwe” group whell            #普通用户名字为tyschool密码"`123qwe"
user --groups=wheel --name=tyschool --password=$6$GnUoYHa8rYy7XfrK$0OSNtZwn7.mq4mTeEXQvebx8AEKMV7/PYBS0qfLXUUMJFQozCVMGZY3c0gYcfaKhkCDVQAcxCpi01A5f6a9XC. --iscrypted --gecos="tyschool_com_cn"
%packages
@^graphical-server-environment
%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%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@localhost ~]# chmod 777 ks.cfg

安装并启动httpd服务

yum -y install dhcpd
service httpd start

在httpd根目录新建一个文件夹,文件夹名为上面文件定义的,将光盘或镜像文件拷贝至该文件夹

mount /dev/cdrom RHEl/

拷贝自动化脚本至httpd

[root@localhost html]# mkdir /var/www/html/ks
[root@localhost ~]# cp ks.cfg /var/www/html/ks/

结合脚本与default形成自动化
编辑default文件

[root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
label RHEL
  menu label ^Install REHL8.0
  menu default   #光标默认此启动项
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.1.253/ks/ks.cfg

邹圣林大神PXE视频

PREV
实验:使mysql监听IPv4端口,并允许远程登录
NEXT
isolinux.cfg文件详解