PXE(预启动执行环境,在操作系统之前运行)是由Intel公司开发的网络引导技术,c/s架构,允许客户机通过网络从远程服务器下载引导镜像,并加载安装文件或者整个操作系统。
客户端
客户机的网卡要支持PXE协议(集成BOOTROM芯片),且主板支持网络引导。一般大多数服务器主机都支持,只需在BIOS设置中允许从 Network 或 LAN 启动即可。
服务端
网卡需要查找相关的dhcp服务器(获取地址时间)
--> 找到后,dhcp服务器提供ip地址和引导程序(boot loader)的地址,还提供给客户机TFTPserver地址(dhcp本身不提供tftp服务)
--> 网卡使用tftp客户端把引导程序加载到内存中
--> bios执行引导程序
--> 引导程序会去TFTP去查找配置文件
--> 根据配置文件去引导安装系统
PXE 远程安装服务器集成了 CentOS 7 安装源、TFTP 服务、DHCP 服务,能够向客户机裸机发送 PXE引导程序、Linux 内核、启动菜单等数据,以及提供安装文件。
(1)关闭防火墙和selinux
- [root@localhost1 ~]#systemctl stop firewalld.service
- [root@localhost1 ~]#setenforce 0
(2)搭建本地yum仓库用于安装所需软件
- [root@localhost1 ~]#mount /dev/sr0 /mnt
- mount: /dev/sr0 写保护,将以只读方式挂载
- [root@localhost1 ~]#cd /etc/yum.repos.d/
- [root@localhost1 yum.repos.d]#ls
- CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
- CentOS-CR.repo CentOS-Media.repo CentOS-x86_64-kernel.repo
- CentOS-Debuginfo.repo CentOS-Sources.repo
- [root@localhost1 yum.repos.d]#mkdir repo.bak
- [root@localhost1 yum.repos.d]#mv *.repo repo.bak/
- [root@localhost1 yum.repos.d]#ls
- repo.bak
- [root@localhost1 yum.repos.d]#vim local.repo
- [root@localhost1 yum.repos.d]#yum clean all
- [root@localhost1 yum.repos.d]#yum makecache
(3)安装并启用 DHCP 服务
- //下载dhcp服务
- [root@localhost1 ~]#yum install -y dhcp
-
- //将dhcp配置模板文件拷贝到dhcp配置文件中
- [root@localhost1 ~]#cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
-
- //修改dhcp配置文件
- [root@localhost1 ~]#vim /etc/dhcp/dhcpd.conf
- ddns-update-style none; --禁用 DNS 动态更新
- next-server 192.168.116.10; --指定 TFTP 服务器的地址
- filename"pxelinux.0"; --指定 PXE 引导程序的文件名
-
- subnet 192.168.116.0 netmask 255.255.255.0 { --声明要分配的网段地址
- range 192.168.116.100 192.168.116.200; --设置ip地址池
- option routers 192.168.116.10; --默认网关地址指向TFTP服务器的IP地址
- }
-
- //开启dhcp服务
- [root@localhost1 ~]#systemctl start dhcpd
(4)安装并启用 TFTP 服务
TFTP(简单文件传输协议):传输内核和引导文件
是一种基于UDP协议实现的用于在客户机和服务器之间进行简单文件传输的协议,适合于小型文件传输的应用场合。TFTP服务默认由xinetd服务进行管理,使用UDP69端口。
- //下载tftp服务相关软件
- [root@localhost1 ~]#yum install -y tftp-server
- [root@localhost1 ~]#yum install -y xinetd.x86_64
-
- //查找并修改TFTP服务配置文件 /etc/xinetd.d/tftp
- [root@localhost1 ~]#cd /etc/xinetd.d/
- [root@localhost1 xinetd.d]#ls
- chargen-dgram daytime-stream echo-dgram tftp
- chargen-stream discard-dgram echo-stream time-dgram
- daytime-dgram discard-stream tcpmux-server time-stream
- [root@localhost1 ~]#vim /etc/xinetd.d/tftp
- service tftp
- {
- socket_type = dgram
- protocol = udp --TFTP使用UDP协议
- wait = no --no表示客户机可以多台一起连接,yes表示客户机只能逐台连接
- user = root
- 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@localhost1 xinetd.d]#systemctl start tftp
- [root@localhost1 xinetd.d]#systemctl start xinetd
(5)准备 Linux 内核、初始化镜像文件
- //光盘中存放pxe启动文件的位置在 images/pxeboot/ 下
- [root@localhost1 ~]#cd /mnt
- [root@localhost1 mnt]#ls
- CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
- EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
- EULA isolinux repodata TRANS.TBL
- [root@localhost1 mnt]#cd images/
- [root@localhost1 images]#ls
- efiboot.img pxeboot TRANS.TBL
- [root@localhost1 images]#cd pxeboot/
- [root@localhost1 pxeboot]#ls
- initrd.img TRANS.TBL vmlinuz
- ^ ^
- 初始化镜像文件 Linux内核
-
- //复制初始化镜像文件和Linux内核文件到TFTP根目录下
- [root@localhost1 pxeboot]#cp vmlinuz /var/lib/tftpboot/
- [root@localhost1 pxeboot]#cp initrd.img /var/lib/tftpboot/
(6)安装syslinux,准备pxe引导程序
syslinux是一个功能强大的引导加载程序,而且兼容各种介质。它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。它的安装很简单,一旦安装syslinux好之后,sysLinux启动盘就可以引导各种基于DOS的工具,以及MS-DOS/Windows或者任何其它操作系统。
- //安装syslinux
- [root@localhost1 pxeboot]#yum install -y syslinux
-
- //把pxe引导程序文件放到/var/lib/tftpboot/下
- [root@localhost1 pxeboot]#cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
(7)安装FTP服务,准备CentOS 7 安装源
- //安装ftp服务
- [root@localhost1 pxeboot]#yum install -y vsftpd
- [root@localhost1 pxeboot]#cd /var/ftp
- [root@localhost1 ftp]#ls
- pub
-
- //在ftp根目录/var/ftp目录下创建一个目录centos7用于放Linux镜像文件
- [root@localhost1 ftp]#mkdir centos7
- [root@localhost1 ftp]#cp -rf /mnt/* /var/ftp/centos7/ --事先将镜像挂载在了mnt目录下
-
- //启动ftp服务
- [root@localhost1 ftp]#systemctl start vsftpd
(8)配置启动菜单文件(默认的启动菜单文件在TFTP根目录的 pxelinux.cfg子目录下,文件名为default)
- [root@localhost1 ftp]#cd /var/lib/tftpboot/
- [root@localhost1 tftpboot]#ls
- initrd.img pxelinux.0 vmlinuz
- [root@localhost1 tftpboot]#mkdir pxelinux.cfg
- [root@localhost1 tftpboot]#cd pxelinux.cfg/
-
- //编写配置
- [root@localhost1 pxelinux.cfg]#vim default
- default auto --指定默认入口名称
- prompt 1 --设置是否等待用户选择,“1”表示等待用户控制
-
- label auto --图形安装(默认)引导入口,label 用来定义启动项
- kernel vmlinuz --指定内核
- append initrd=initrd.img method=ftp://192.168.116.10/centos7 --指定初始化镜像文件(ftp源)
-
- label linux text --文本安装引导入口
- kernel vmlinuz
- append text initrd=initrd.img method=ftp://192.168.116.10/centos7
-
- label linux rescue --急救模式引导入口
- kernel vmlinuz
- append rescue initrd=initrd.img method=ftp://192.168.116.10/centos7
(9)测试安装
新键一台虚拟机并打开

在提示字符串boot: 后直接按Enter键(或执行“auto”命令),将会自动通过网络下载安装文件,并进入默认的图形安装入口;
若执行“linux text”命令,则进入文本安装入口;
若执行 “linux rescue”命令,则进入救援模式。

pxe安装仍然需要我们手动对新虚拟机进行配置,pxe服务器使用kickstrat,可以实现按指定需求自动安装新机。
- //安装system-config-kickstart 工具
- [root@localhost1 ~]#yum install -y system-config-kickstart
- //打开“Kickstart 配置程序”窗口
- [root@localhost1 ~]#system-config-kickstart
以下操作仅跟本人需求配置, 仅供参考,可自定义
(1) 基本配置

(2)安装方法

(3)引导装载程序选项

(4)分区信息


(5)网络配置

(6)防火墙配置

(7)安装后执行的脚本
(8)保存文件
选择“Kickstart 配置程序”窗口的“文件”-->“保存”命令,选择指定保存位置,默认文件名为ks.cfg ,保存在/root/ks.cfg

(9)配置需要安装的软件包
可以根据需要将/root/anaconda-ks.cfg 的软件包安装脚本复制到 ks.cfg文件中,只需要复制%packages 到%end 部分即可。
- //查看文件中的软件包字段
- [root@localhost1 ~]#cat anaconda-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
-
- //添加图形化界面安装
- [root@localhost1 tftpboot]#vim /var/ftp/ks.cfg
- %packges
- @^gnome-desktop-environment
- %end
(10)编辑引导菜单文件 default,添加 ks 引导参数,使自定义配置生效
- [root@localhost1 ~]#vim /var/lib/tftpboot/pxelinux.cfg/default
- default auto
- prompt 0
-
- label auto
- kernel vmlinuz
- append initrd=initrd.img method=ftp://192.168.116.10/centos7 ks=ftp://192.168.116.10/ks.cfg
(11)测试安装
当客户机每次以PXE方式引导时,将自动下载ks.cfg应答配置文件,然后根据其中的设置安装CentOS 7 系统,而无须手工干预。过程全自动化,不再演示。