PXE高效批量网络装机

一、PXE概述

1.PXE(Preboot eXcution Environment)

PXE(预启动执行环境,在操作系统之前运行)是由Intel公司开发的网络引导技术,工作在Client/Server模式,允许客户机通过网络从远程服务器下载引导镜像,并加载安装文件或者整个操作系统。

2.PXE批量部署的优点

规模化:同时装配多台服务器

自动化 :安装系统、配置各种服务

远程实现:不需要光盘、U盘等安装介质

  3.搭建PXE网络体系的前提条件

(1)客户端:
客户机的网卡要支持PXE协议(集成BOOTROM芯片)

主板支持网络引导;一般大多数服务器主机都支持,只需在BIOS设置中允许从 Network 或

LAN 启动即可。

(2)服务端:
网络中有一台 DHCP 服务器以便为客户机自动分配地址、指定引导文件位置。

服务器要通过 TFTP服务(简单文件传输协议)来提供系统内核和引导镜像文件的下载。

4.搭建 PXE 远程安装服务器 

(1)安装并启用 TFTP 服务

[root@localhost yum.repos.d]# yum install -y xinetd tftp-server
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 tftp-server.x86_64.0.5.2-22.el7 将被 安装
---> 软件包 xinetd.x86_64.2.2.3.15-14.el7 将被 安装
--> 解决依赖关系完成
.............................中间省略................................................
  正在安装    : 2:xinetd-2.3.15-14.el7.x86_64                                              1/2 
  正在安装    : tftp-server-5.2-22.el7.x86_64                                              2/2 
  验证中      : tftp-server-5.2-22.el7.x86_64                                              1/2 
  验证中      : 2:xinetd-2.3.15-14.el7.x86_64                                              2/2 
 
已安装:
  tftp-server.x86_64 0:5.2-22.el7                 xinetd.x86_64 2:2.3.15-14.el7                
 
完毕!

 (2)修改TFTP服务的配置文件

[root@localhost ~]# cd /etc/xinetd.d/
[root@localhost xinetd.d]# ls
chargen-dgram   daytime-dgram   discard-dgram   echo-dgram   tcpmux-server  time-dgram
chargen-stream  daytime-stream  discard-stream  echo-stream  tftp           time-stream
[root@localhost xinetd.d]# vim 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        #TFTP默认使用UDP协议
        wait                    = no           #no表示客户机可以多台一起连接,                                       
        user                    = root         #yes表示客户机只能一台一台连接
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot   #指定TFTP根目录(引导文件的存储路径)
        disable                 = no                     #no表示开启TFTP服务
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

保存退出之后设置服务开机自启

[root@localhost xinetd.d]# systemctl enable --now xinetd
[root@localhost xinetd.d]# systemctl enable --now tftp
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.

(3)安装并启用 DHCP 服务 

[root@localhost xinetd.d]# yum install -y dhcp
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 dhcp.x86_64.12.4.2.5-83.el7.centos.1 将被 安装
  > 解决依赖关系完成
.............................中间省略.....................................
  正在安装    : 12:dhcp-4.2.5-83.el7.centos.1.x86_64                                       1/1 
  验证中      : 12:dhcp-4.2.5-83.el7.centos.1.x86_64                                       1/1 
 
已安装:
  dhcp.x86_64 12:4.2.5-83.el7.centos.1                                                         
 
完毕!

复制官方模板去指定目录

[root@localhost xinetd.d]# cd /usr/share/doc/dhcp-4.2.5/
[root@localhost dhcp-4.2.5]# ls
dhcpd6.conf.example  dhcpd.conf.example  ldap
 
[root@localhost dhcp-4.2.5]# \cp -f dhcpd.conf.example /etc/dhcp/dhcpd.conf 
[root@localhost dhcp-4.2.5]# cd /etc/dhcp/
[root@localhost dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts

(4)修改DHCP服务的配置文件 

[root@localhost dhcp]# vim dhcpd.conf 
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
 
# option definitions common to all supported networks...
#option domain-name "example.org";                             打上注释#
#option domain-name-servers ns1.example.org, ns2.example.org;  打上注释#
 
default-lease-time 600;
max-lease-time 7200;
 
# Use this to enble / disable dynamic dns updates globally.
ddns-update-style none;          #取消注释#禁用DNS动态更新
 
next-server 192.168.10.10;       #指定 TFTP 服务器的地址
filename "pxelinux.0";           #指定要下载的 PXE 引导程序的文件
 
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
 
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
 
# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
 
subnet 10.152.187.0 netmask 255.255.255.0 {
}
 
# This is a very basic subnet declaration.
 
subnet 192.168.10.0 netmask 255.255.255.0 {    #声明要分配的网段地址
  range 192.168.10.100 192.168.10.200;         #设置地址池
  option routers 192.168.10.10;         #默认网关地址指向TFTP服务器的IP地址
}
 
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
 
subnet 10.254.239.32 netmask 255.255.255.224 {
  range dynamic-bootp 10.254.239.40 10.254.239.60;
  option broadcast-address 10.254.239.31;
  option routers rtr-239-32-1.example.org;
"dhcpd.conf" 107L, 3278C 已写入 

保存退出之后设置服务开机自启即可

[root@localhost dhcp]# systemctl enable --now dhcpd

(5)准备 PXE 引导程序 和 准备 Linux 内核、初始化镜像文件 

[root@localhost ~]#yum install -y syslinux
[root@localhost ~]# cd /usr/share/selinux/
[root@localhost selinux]# ls
packages  targeted
[root@localhost selinux]# cd ..
[root@localhost share]# cd /usr/share/syslinux/
#PXE引导程序由软件包 syslinux 提供
 
[root@localhost syslinux]# ls
 
   pxelinux.0 
内容省略,只留下需要的内容
 
[root@localhost syslinux]# cp pxelinux.0 /var/lib/tftpboot/
#复制 PXE引导程序 到TFTP根目录下
 
[root@localhost syslinux]# ls /var/lib/tftpboot/
pxelinux.0
[root@localhost syslinux]# cd /mnt/
[root@localhost mnt]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@localhost mnt]# cd images/
[root@localhost images]# ls
efiboot.img  pxeboot  TRANS.TBL
[root@localhost images]# cd pxeboot/
[root@localhost pxeboot]# ls
initrd.img  TRANS.TBL  vmlinuz
 
[root@localhost pxeboot]# cp vmlinuz initrd.img /var/lib/tftpboot/
#复制 Linux系统的内核文件 和 初始化镜像文件(linux引导加载模块)到TFTP根目录下
 
[root@localhost pxeboot]# ls /var/lib/tftpboot/
initrd.img  pxelinux.0  vmlinuz

(6)安装FTP服务,准备CentOS 7 安装源
 

[root@localhost dhcp]# yum install -y vsftpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-29.el7_9 将被 安装
--> 解决依赖关系完成
 
依赖关系解决
..................................中间省略..................................
  正在安装    : vsftpd-3.0.2-29.el7_9.x86_64                                               1/1 
  验证中      : vsftpd-3.0.2-29.el7_9.x86_64                                               1/1 
 
已安装:
  vsftpd.x86_64 0:3.0.2-29.el7_9                                                               
 
完毕!
[root@localhost dhcp]# systemctl enable --now vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@localhost dhcp]# cd /var/ftp/
[root@localhost ftp]# mkdir centos7
[root@localhost ftp]# ls /mnt/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@localhost ftp]# mount /dev/sr0 /var/ftp/centos7/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ftp]# cd /var/ftp/centos7/
[root@localhost centos7]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

(7)配置启动菜单文件

[root@localhost centos7]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# ls
initrd.img  pxelinux.0  vmlinuz
[root@localhost tftpboot]# mkdir pxelinux.cfg
[root@localhost tftpboot]# ls
initrd.img  pxelinux.0  pxelinux.cfg  vmlinuz
[root@localhost tftpboot]# cd pxelinux.cfg/
[root@localhost pxelinux.cfg]# touch default
[root@localhost pxelinux.cfg]# ls
default
[root@localhost pxelinux.cfg]# vim default
 
default auto           #指定默认入口名称
prompt 1               #设置是否等待用户选择,“1”表示等待用户控制
 
label auto         #图形安装(默认)引导入口,label 用来定义启动项
kernel vmlinuz     #kernel 和 append用来定义引导参数
append initrd=initrd.img method=ftp://192.168.10.10/centos7
 
label text                 #文本安装引导入口
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.10.10/centos7
 
label rescue              #救援模式引导入口
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.10.10/centos7

(8)验证 PXE 网络安装

首先启动 tftp-server  xinetd  dhcpd  vsftpd 四个服务,然后使用 VMware创建的虚拟机进行测试,

虚拟机内存至少需要 2GB,否则在启动安装时可能会报错。

由于没有加载光盘,会自动采用网络引导启动

开启虚拟机,在提示字符串“boot:”后直接按Enter键(或执行“auto”命令),将会自动通过网络下载安装文件,并进入默认的图形安装入口; 

 

 

 

搭建PXE安装服务器完全自动安装 

(1)安装system-config-kickstart 工具

[root@localhost ~]# yum install -y system-config-kickstart

之后有两种方法可以进入kickstart 工具界面

或者

(2)配置kickstart选项

 

 

 (3)配置 ks.cfg 文件

[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  ks.cfg  
公共  模板  视频  图片  文档  下载  音乐  桌面
 
          #其中 anaconda-ks.cfg 是系统的应答文件
 
[root@localhost ~]# vim ks.cfg     #查看文件内容

 获取软件包配置需要到系统的应答文件中复制一些配置到 ks.cfg文件中。

系统应答文件中的相关配置
 
%packages                             @起点位置标识
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents                        中间为自动化装载需要的软件包配置
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools
 
%end                                 #终点位置标识

将保存修改后的ks.cfg  文件移动到ftp中

[root@localhost ~]# cp ks.cfg /var/ftp/
[root@localhost ~]# cd /var/ftp/
[root@localhost ftp]# ls
centos7  ks.cfg  pub 

 (4)再次修改 defaults 文件配置,让ks.cfg文件配置生效

[root@localhost ftp]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# ls
initrd.img  pxelinux.0  pxelinux.cfg  vmlinuz
[root@localhost tftpboot]# cd pxelinux.cfg/
[root@localhost pxelinux.cfg]# ls
default
[root@localhost pxelinux.cfg]# vim default
default auto
prompt 0                #将1改成 0 可以不用再按Enter或写入auto
 
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.10.10/centos7 ks=ftp://192.168.10.10/ks.cfg
                                #每行增加ks=ftp://192.168.10.10/ks.cfg配置
label text
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.10.10/centos7 ks=ftp://192.168.10.10/ks.cfg
 
label rescue
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.10.10/centos7 ks=ftp://192.168.10.10/ks.cfg

(5)新建虚拟机进行测试

 

    #在新建的虚拟机中查看,在kickstart配置界面写入的命令
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
local.repo  repo.bak
[root@localhost yum.repos.d]# cat local.repo 
[local]
name=local
baseurl=ftp://192.168.80.30/centos7
enabled=1
gpgcheck=0
[root@localhost yum.repos.d] 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值