前言
PXE(Preboot Execution Environment)装机是一种通过网络引导和安装操作系统的方法。它允许计算机在没有本地存储设备(如硬盘或光盘驱动器)的情况下,通过网络从远程服务器或网络共享位置加载操作系统安装文件并完成安装过程。
PXE装机通常用于大规模部署和远程管理计算机,特别适用于服务器和客户机环境。它可以大大简化操作系统的安装和配置过程,提高部署效率和一致性,并减少人工操作的需求。
PXE装机的基本工作原理如下:
1. 客户机(待安装的计算机)通过网络启动,并发送DHCP请求以获取IP地址和其他配置信息。
2. DHCP服务器回应并提供一个IP地址和PXE启动服务的相关配置。
3. 客户机使用TFTP(Trivial File Transfer Protocol)从PXE服务器下载引导程序(如pxelinux.0)。
4. 引导程序加载并启动,提供菜单和选项,允许用户选择所需的操作系统安装。
5. 客户机选择安装选项后,引导程序从PXE服务器下载适当的操作系统安装文件(如内核、初始化内存盘(initrd)和安装程序)。
6. 客户机使用下载的文件进行操作系统安装过程。
PXE装机的配置包括设置和维护PXE服务器、创建引导文件、设置DHCP服务器和TFTP服务器等。它通常与其他自动化工具(如Kickstart文件)结合使用,以实现自动化和批量化的操作系统部署。
一、实验拓扑图
条件:按照上述要求我们准备好设备,设置防火墙、selinux、添加各自的网卡
目的:实现不同网段的有人值守与无人值守装机
二、PXE的组件
- vsftpd/httpd/nfs负责提供系统的安装文件
- tftp负责提供系统安装前的引导文件与内核文件
- dhcp负责提供客户端的IP地址分配与pxe引导文件,及pxe服务器地址
三、配置PXE装机服务器
1、设置防火墙、selinux
1. systemctl stop firewalld.service 2. systemctl enable firewalld.service 3. setenforce 0
2.安装、启动vsftp
######配置本地yum cd /etc/yum.repos.d mkdir back mv CentOS-* back vim local.repo ###插入 [local] name=local baseurl=file:///mnt enabled=1 gpgcheck=0 ###挂载sr0,安装vsftpd mount /dev/sr0 /mnt yum -y install vsftpd systemctl start vsftpd
3、拷贝系统文件到/var/ftp用于装机
cd /var/ftp mkdir centos7 cp -r /mnt/* /var/ftp/centos7 sync
4、配置tftp
###安装 yum install -y tftp-server ###修改配置文件 vim /etc/xinit.d/tftp ###修改处 disable=no ###启动 systemctl start tftp
5、准备pxelinx.0文件、引导文件、内核文件
###准备pxelinux.0文件 yum install -y syslinux cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot ###准备引导文件、内核文件 cd /mnt/images/pxeboot cp initrd.img vmlinuz /var/lib/tftpboot
6、配置本机IP
vim /etc/sysconfig/network-scripts/ifcfg-ens33 ###改为 TYPE=Ethernet BOOTPROTO=static NAME=ens33 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.253 PREFIX=24 GATEWAY=192.168.100.254 ###保存退出,重启网络、ip a 查看 systemctl restart network ip a
7、配置DHCP服务
yum -y install dhcp cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf vim /etc/dhcp/dhcpd.conf ##删除前3段的subnet字段,修改剩下的字段 subnet 192.168.100.0 netmask 255.255.255.0 { range 192.168.100.1 192.168.100.252; #option domain-name-servers ns1.internal.example.org; #option domain-name "internal.example.org"; option routers 192.168.100.254; option broadcast-address 192.168.100.255; default-lease-time 600; max-lease-time 7200; next-server 192.168.100.253; filename "pxelinux.0"; } subnet 192.168.200.0 netmask 255.255.255.0 { range 192.168.200.1 192.168.200.252; #option domain-name-servers ns1.internal.example.org; #option domain-name "internal.example.org"; option routers 192.168.200.254; option broadcast-address 192.168.200.255; default-lease-time 600; max-lease-time 7200; next-server 192.168.100.253; filename "pxelinux.0"; } ###启动DHCP服务 systemctl start dhcpd
8、创建default文件
cd /var/lib/tftpboot mkdir pxelinux.cfg cd pxelinux.cfg vim default ###插入内容 default auto #默认安装标签 prompt 1 #等待用户确认,1表示等待,0表示不等待 label auto #定义标签 kernel vmlinuz #指定内核 append initrd=initrd.img method=ftp://192.168.100.253/centos7 #指定引导镜像文件与系统安装文件
有人值守的方式已经基本配置好了
四、配置中继
1.添加网卡
2、配置网卡
###设置防火墙、selinux systemctl stop firewalld.service setenforce 0 ### cd /etc/sysconfig/network-scripts vim ifcfg-ens33 ##改为 TYPE=Ethernet BOOTPROTO=static NAME=ens33 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.254 PREFIX=24 ############################################### vim ifcfg-ens37 ##改为 OTPROTO=static NAME=ens37 DEVICE=ens37 ONBOOT=yes IPADDR=192.168.200.254 PREFIX=24
3、添加路由功能
####安装DHCP yum -y install dhcp dhcrelay 192.168.100.253 vim /etc/sysctl.conf ####文末插入 net.ipv4.ip_forward = 1 ###保存退出 sysctl -p
4、测试pxe 与中继的通联
五、新建测试主机用来测试装机效果
1、新建一台网卡2网段主机
都选下一步
使用网卡2
开机,连接成功,按下回车开始安装
2、新建一台网卡3网段的主机
创建方式同上,我就不啰嗦了。这里把网卡改成3就好
开机验证
回车+耐心等待
测试完毕,结果ok
六、配置无人值守的pxe装机
1、图形化配置
##使用图形界面配置 yum install -y system-config-kickstart.noarch system-config-kickstart
执行完上述命令后会出现图形化界面
下面开始配置
脚本看你的需求
保存
查看保存文件的位置
拷贝:从/root/anaconda-ks.cfg文件中拷贝软件安装字段到ks.cfg
vim anaconda-ks.cfg ##复制以下字段插入到ks.cfg %packages @^graphical-server-environment @base @core @desktop-debugging @development @dial-up @fonts @gnome-desktop @guest-agents @guest-desktop-agents @hardware-monitoring @input-methods @internet-browser @multimedia @print-client @x11 chrony kexec-tools %end
这是一个centos7最小安装的ks.cfg
#platform=x86, AMD64, 或 Intel EM64T #version=DEVEL # Install OS instead of upgrade install # Keyboard layouts keyboard 'us' # Root password rootpw --plaintext 123.com # System language lang zh_CN # System authorization information auth --useshadow --passalgo=sha512 # Use graphical install graphical firstboot --disable # SELinux configuration selinux --enforcing # Firewall configuration firewall --disabled # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Use network installation url --url="ftp://你的ip/centos7" # System bootloader configuration bootloader --location=mbr # Partition clearing information clearpart --all # Disk partitioning information part /boot --asprimary --fstype="xfs" --size=200 part / --asprimary --fstype="xfs" --grow --size=1 %packages --nobase @core %end
拷贝
cp ks.cfg /var/ftp
2、修改default文件
vim /var/lib/tftpboot/pxelinux.cfg/default ###修改 default auto prompt 0 label auto kernel vmlinuz append initrd=initrd.img method=ftp://192.168.100.253/centos7 ks=ftp://192.168.100.253/ks.cfg
3、验证
创建一台192.168.100.0网段的新主机开机
创建一台192.168.200.0网段的新主机开机
等待一会它已经自己连接上了,开始装系统了。我们什么也不用做,等待就好
终于好了,输入我们在图形化设置中设置的密码登录root账户
查看192.168.100.0段的新主机
查看192.168.200.0段的新主机
总结
本次实验成功的对不同网段的新主机进行了有人值守的PXE装机和无人值守的PXE装机,通过实验结果来看基本达到了预期的目的。本次实验中的步骤大致分为配置PXE服务器和中继设备,最主要的就是我们pex服务器的设置:
vsftpd/httpd/nfs负责提供系统的安装文件
tftp负责提供系统安装前的引导文件与内核文件
dhcp负责提供客户端的IP地址分配与pxe引导文件,及pxe服务器地址
#/bin/bash #hy ###挂载sr0,安装vsftpd mount /dev/sr0 /mnt yum -y install vsftpd systemctl start vsftpd cd /var/ftp mkdir centos7 cp -r /mnt/* /var/ftp/centos7 ###安装 yum install -y tftp-server ###修改处 sed -i 's/disable = yes/disable = no/g' /etc/xinetd.d/tftp ###启动 systemctl restart tftp ###准备pxelinux.0文件 yum install -y syslinux cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot ###准备引导文件、内核文件 cd /mnt/images/pxeboot cp initrd.img vmlinuz /var/lib/tftpboot ###### yum -y install dhcp rm -rf /etc/dhcp/dhcpd.conf ##配置DHCP cat <<EOF>> /etc/dhcp/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; # 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. # This is a very basic subnet declaration. # This declaration allows BOOTP clients to get dynamic addresses, # which we don't really recommend. # A slightly different configuration for an internal subnet. subnet 192.168.100.0 netmask 255.255.255.0 { range 192.168.100.3 192.168.100.252; #option domain-name-servers ns1.internal.example.org; #option domain-name "internal.example.org"; option routers 192.168.100.100; option broadcast-address 192.168.100.255; default-lease-time 600; max-lease-time 7200; next-server 192.168.100.100; filename "pxelinux.0"; } # Hosts which require special configuration options can be listed in # host statements. If no address is specified, the address will be # allocated dynamically (if possible), but the host-specific information # will still come from the host declaration. host passacaglia { hardware ethernet 0:0:c0:5d:bd:95; filename "vmunix.passacaglia"; server-name "toccata.fugue.com"; } # Fixed IP addresses can also be specified for hosts. These addresses # should not also be listed as being available for dynamic assignment. # Hosts for which fixed IP addresses have been specified can boot using # BOOTP or DHCP. Hosts for which no fixed address is specified can only # be booted with DHCP, unless there is an address range on the subnet # to which a BOOTP client is connected which has the dynamic-bootp flag # set. host fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com; } # You can declare a class of clients and then do address allocation # based on that. The example below shows a case where all clients # in a certain class get addresses on the 10.17.224/24 subnet, and all # other clients get addresses on the 10.0.29/24 subnet. class "foo" { match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; } shared-network 224-29 { subnet 10.17.224.0 netmask 255.255.255.0 { option routers rtr-224.example.org; } subnet 10.0.29.0 netmask 255.255.255.0 { option routers rtr-29.example.org; } pool { allow members of "foo"; range 10.17.224.10 10.17.224.250; } pool { deny members of "foo"; range 10.0.29.10 10.0.29.230; } } EOF systemctl restart dhcpd ##default配置 mkdir /var/lib/tftpboot/pxelinux.cfg cat <<EOF>> /var/lib/tftpboot/pxelinux.cfg/default default auto prompt 0 label auto kernel vmlinuz append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg EOF rm -rf /var/ftp/ks.cfg ##ks.cfg配置 cat <<EOF>> /var/ftp/ks.cfg #platform=x86, AMD64, 或 Intel EM64T #version=DEVEL # Install OS instead of upgrade install # Keyboard layouts keyboard 'us' # Root password rootpw --plaintext 123.com #密码123456 # System language lang zh_CN # System authorization information auth --useshadow --passalgo=sha512 # Use graphical install graphical firstboot --disable # SELinux configuration selinux --enforcing # Firewall configuration firewall --disabled # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Use network installation url --url="ftp://192.168.100.100/centos7" # System bootloader configuration bootloader --location=mbr # Partition clearing information clearpart --all # Disk partitioning information part /boot --asprimary --fstype="xfs" --size=200 part / --asprimary --fstype="xfs" --grow --size=1 %packages --nobase @core %end EOF