在RHEL/CentOS 7上设置用于多个Linux发行版安装的“PXE网络启动服务器“

本文详细介绍了如何在RHEL/CentOS7系统上配置PXE服务器,利用DNSMASQ和本地CentOS7DVDISO镜像,为网络上的客户端提供无介质安装服务。步骤包括安装和配置DNSMASQ,安装syslinux引导加载器,设置TFTP服务器,配置PXE服务器,添加CentOS7引导镜像,启动和启用相关服务,以及开放防火墙端口。此外,还展示了客户端如何从网络引导并安装CentOS7。
摘要由CSDN通过智能技术生成

PXE服务器--预执行环境--直接从一个网络接口指导一个客户端计算机启动、运行或者安装一个操作系统,消除了烧录CD/DVD或者使用物理介质的需求,或者能够在你网络基础设施上减轻同时在多台机器上安装Linux发行版的工作。

 

这篇文章将解释如何在DNSMASQ服务器帮助下用镜像的本地安装库(由CentOS7 DVD ISO镜像提供)在RHEL/CentOS7 x64位上安装和配置一个PXE服务器。

DNSMASQ服务器提供了DNS和DHCP服务,syslinux包提供了用于网络启动的bootloaders,TFTP-Server使用TFTP协议通过网络使得可引导镜像能被下载,而HTTP服务器将托管本地挂载的镜像的DVD镜像,它将作为官方RHEL/CentOS 7镜像安装库,安装程序将从此处提取它所需的包。

第一步:安装和配置DNSMASQ服务器

1) 如果你的服务器有多块网卡,你其中一个网卡接口必须被配置位静态IP地址,此地址来自将提供PXE服务的网段的相同IP范围。

在你完成配置你的静态IP地址后,更新你的系统并且执行其它初始化设置,使用以下命令安装DNSMASQ守护程序。

[root@localhost areadetector]# yum install dnsmasq
Loaded plugins: fastestmirror, langpacks
...

Updated:
  dnsmasq.x86_64 0:2.76-17.el7_9.3

Complete!

2) DNSMASQ主配置文件位于/etc目录,由于它详细的解释,它是很明白的,但要编辑非常困难。

首先,确定备份这个文件,以防你需要在之后回看它,接着使用你习惯的文本编辑器创建一个配置文件:

[root@localhost areadetector]# mv /etc/dnsmasq.conf  /etc/dnsmasq.conf.bak
[root@localhost areadetector]# vim /etc/dnsmasq.conf

3) 现在,在dnsmasq.conf文件上复制以下配置,并且确认你相应地更改了以下解释地语句来匹配你网络设置。

interface=br4,lo
bind-interfaces
domain=centos7.lan
# DHCP range-leases
dhcp-range= br4,192.168.3.200,192.168.3.250,255.255.255.0,1h
# PXE
dhcp-boot=pxelinux.0,pxeserver,192.168.3.40
# Gateway
dhcp-option=3,192.168.3.40
# DNS
dhcp-option=6,192.168.50.1, 8.8.8.8
server=8.8.4.4
# Broadcast Address
dhcp-option=28,192.168.3.255
# NTP Server
dhcp-option=42,0.0.0.0

pxe-prompt="Press F8 for menu.", 60
pxe-service=x86PC, "Install CentOS 7 from network server 192.168.3.40", pxelinux
enable-tftp
tftp-root=/var/lib/tftpboot

你需要更改的语句如下:

  • interface:服务器应该监听和提供服务的接口。
  • bind-interface:取消注释只对这个接口绑定。
  • domain:用你的域名替代它。
  • dhcp-range:有在这个段上你网络掩码定义的IP范围替代它。
  • dhcp-boot:用你的接口IP地址替代这个IP声明。
  • dhcp-option=3,192.168.3.40:用你的网络段网关替代这个IP地址。
  • dhcp-option=6,192.168.3.40:用你DNS服务器IP替换这个IP地址,能够定义若干个DNS IPs。
  • server=8.8.4.4:放置你的DNS转发IPs地址
  • dhcp-option=28,10.0.0.255:用网络广播地址替代这个IP地址--可选的。
  • dhcp-option=42,0.0.0.0:放置你网络时间服务器--可选的(0.0.0.0地址对应自引用)
  • pxe-prompt:保留为缺省--表示按F8键进入60秒等待时间菜单。
  • pxe=service:对32位/64位架构使用x86PC,并且进入一个在字符串引号下的菜单描述提示。其它值类型可以是:PC98, IA64_EFI, Alpha, Arc_x86, Intel_Lean_Client, IA32_EFI, BC_EFI, Xscale_EFI and X86-64_EFI。
  • enable-tftp:启用内建的TFTP服务器。
  • tftp-root:使用/var/lib/tftpboot -- 所有网络引导文件的位置。

第二步:安装syslinux bootloaders

4) 在你编辑和保存了DNSMASQ主配置文件后,通过发出以下命令继续并且按照syslinux PXE bootloader包。

[root@localhost areadetector]# yum install syslinux

5) PXE bootloader文件存在于/usr/share/syslinux绝对系统路径,所以你可以通过列出这个路径内容来检查它。这部是可选的,但你需要指导这个路径,因为在下一步,我们将复制所有其内容到TFTP服务器路径。

[root@localhost areadetector]# ls /usr/share/syslinux/
altmbr.bin     ethersel.c32  isohdpfx.bin        mbr_c.bin     sanboot.c32
altmbr_c.bin   gfxboot.c32   isohdpfx_c.bin      mbr_f.bin     sdi.c32
altmbr_f.bin   gptmbr.bin    isohdpfx_f.bin      memdisk       sysdump.c32
cat.c32        gptmbr_c.bin  isohdppx.bin        memdump.com   syslinux64.exe
chain.c32      gptmbr_f.bin  isohdppx_c.bin      meminfo.c32   syslinux.com
cmd.c32        gpxecmd.c32   isohdppx_f.bin      menu.c32      syslinux.exe
config.c32     gpxelinux.0   isolinux.bin        pcitest.c32   ver.com
cpuid.c32      gpxelinuxk.0  isolinux-debug.bin  pmload.c32    vesainfo.c32
cpuidtest.c32  hdt.c32       kbdmap.c32          poweroff.com  vesamenu.c32
diag           host.c32      linux.c32           pwd.c32       vpdtest.c32
disk.c32       ifcpu64.c32   ls.c32              pxechain.com  whichsys.c32
dmitest.c32    ifcpu.c32     lua.c32             pxelinux.0    zzjson.c32
dosutil        ifplop.c32    mboot.c32           reboot.c32
elf.c32        int18.com     mbr.bin             rosh.c32

第三步:按照TFTP-Server并且用Syslinux bootloader填充它

6) 现在,我们进入下一步并且按照TFTP-Server,并且接着,通过发出以下命令从以上列出的位置复制由syslinux包提供的所有bootloader文件到/var/lib/tftpboot路径。

[root@localhost areadetector]# yum install tftp-server
[root@localhost areadetector]# cp -r /usr/share/syslinux/* /var/lib/tftpboot/

第四步:设置PXE服务器配置文件

7) PXE服务器一般从托管在一个称为pxelinux.cfg文件夹中一组特定文件读取它的配置,这个文件夹必须位于来自DNSMASQ主配置文件的tftp-root声明指定的目录中。

通过发送以下命令创建所需目录pxelinux.cfg并且用一个default文件填充它。

[root@localhost areadetector]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@localhost areadetector]# touch /var/lib/tftpboot/pxelinux.cfg/default

8) 现在用有效的Linux发行版按照选项编辑PXE配置文件。也注意在这个文件中使用的所有路径必须相对于/var/lib/tftpboot目录。

以下你可以见到一个你能够使用的示例配置文件,但修改按照镜像(kernel和initrd文件),协议(FTP, HTTP, HTTPS, NFS)以及反映你网络安装源仓库的IPs和路径。

[root@localhost areadetector]# vim /var/lib/tftpboot/pxelinux.cfg/default

添加以下摘要到这个文件: 

default menu.c32
prompt 0
timeout 300
ONTIMEOUT local

menu title ########## PXE Boot Menu ##########

label 1
menu label ^1) Install CentOS 7 x64 with Local Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://192.168.3.254/CentOS7 devfs=nomount

label 2
menu label ^2) Install CentOS 7 x64 with http://mirror.centos.org Repo
kernel centos7/vmlinuz
append initrd=centos7/initrd.img method=http://mirror.centos.org/centos/7/os/x86_64/ devfs=nomount ip=dhcp

label 3
menu label ^3) Install CentOS 7 x64 with Local Repo using VNC
kernel centos7/vmlinuz
append  initrd=centos7/initrd.img method=http://192.168.3.254/CentOS7 devfs=nomount inst.vnc inst.vncpassword=password

label 4
menu label ^4) Boot from local drive

如你所见,CentOS7引导镜像(kernel和initrd)位于一个名为centos7相对于/var/lib/tftboot的目录中(在一个绝对系统路径上,这是/var/lib/tftpboot/centos7)并且通过在网络192.168.3.254/CentOS7位置上使用HTTP协议到达安装程序仓库--在这里由于IP地址与PXE服务区地址不同,远程托管了仓库。

如果你在一个无显示器客户端上安装,菜单label 3指定应该通过VNC从一个远程位置进行客户端安装,菜单label 2指定安装源为一个CentOS 7官方Internet镜像(这种方式在客户端上需要一个通过DHCP和NAT的Interne连接)。

重要:如你在以上配置中所见,我们使用CentOS 7用于演示目的,但你也可以定义RHEL 7镜像,并且以下的整个介绍和配置只基于CentOS 7,所以在选择发行版时要仔细。

第五步:添加CentOS 7 引导镜像到PXE服务器

9) 对于这步,需要CentOS内核和initrd文件。要获取CentOS 7 DVD镜像。下载CentOS 7镜像,放置它在你的DVD驱动器并且挂载它到http服务器/var/www/html/CentOS7挂载点。

[root@areadetector blctrl]# mount -t iso9660 -o ro /mnt/harddisk/CentOS-7-x86_64-Everything-2009.iso /var/www/html/CentOS7
[root@areadetector blctrl]# ls /var/www/html/CentOS7/
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL

如果你的机器没有DVD驱动器,你也可以使用wget或curl下载CentOS 7 DVD ISO。

10) 在DVD内容可用后,创建centos7目录并且从http服务器下载CentOS 7可引导内核和initrd镜像。

[root@localhost areadetector]# cd /var/lib/
[root@localhost lib]# cd tftpboot/
[root@localhost tftpboot]# mkdir centos7
[root@localhost tftpboot]# cd centos7/
[root@localhost centos7]# wget http://192.168.3.254/CentOS7/images/pxeboot/vmlinuz
--2022-07-20 16:49:49--  http://192.168.3.254/CentOS7/images/pxeboot/vmlinuz
Connecting to 192.168.3.254:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6769256 (6.5M)
Saving to: ‘vmlinuz’

100%[====================================================>] 6,769,256   --.-K/s   in 0.06s

2022-07-20 16:49:50 (115 MB/s) - ‘vmlinuz’ saved [6769256/6769256]

[root@localhost centos7]# wget http://192.168.3.254/CentOS7/images/pxeboot/initrd.img
--2022-07-20 16:50:02--  http://192.168.3.254/CentOS7/images/pxeboot/initrd.img
Connecting to 192.168.3.254:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 55129656 (53M) [application/octet-stream]
Saving to: ‘initrd.img’

100%[====================================================>] 55,129,656   113MB/s   in 0.5s

2022-07-20 16:50:02 (113 MB/s) - ‘initrd.img’ saved [55129656/55129656]

[root@localhost centos7]# ls
initrd.img  vmlinuz

使用这种方法的原因是:之后你能够在/var/lib/tftpboot路径中创建新的独立目录并且添加其它Linux发行版到PXE菜单而不会弄乱整个目录结构。

第六步:启动和使能整个系统的守护程序

11) 现在最终完成了PXE服务器配置,启动DNSMASQ服务器(192.168.3.40)和HTTP服务器(192.168.3.254),并且通过运行以下命令确认它们的状态并且全系统使能它,在每次系统重启后自动启动。

## DNSMASQ服务器(192.168.3.40)
[root@localhost areadetector]# systemctl start dnsmasq
[root@localhost areadetector]# systemctl enable dnsmasq
Created symlink from /etc/systemd/system/multi-user.target.wants/dnsmasq.service to /usr/lib/systemd/system/dnsmasq.service.
[root@localhost areadetector]# systemctl status dnsmasq.service
● dnsmasq.service - DNS caching server.
   Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-07-21 09:14:54 CST; 2s ago
 Main PID: 181227 (dnsmasq)
    Tasks: 1
   CGroup: /system.slice/dnsmasq.service
           └─181227 /usr/sbin/dnsmasq -k

Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: started, version 2.76 cachesize 150
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: compile time options: IPv6 GNU-geto...y
Jul 21 09:14:54 localhost.localdomain dnsmasq-dhcp[181227]: DHCP, IP range 192.168.3.200 -...h
Jul 21 09:14:54 localhost.localdomain dnsmasq-dhcp[181227]: DHCP, sockets bound exclusivel...4
Jul 21 09:14:54 localhost.localdomain dnsmasq-tftp[181227]: TFTP root is /var/lib/tftpboot
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: using nameserver 8.8.4.4#53
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: reading /etc/resolv.conf
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: using nameserver 8.8.4.4#53
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: using nameserver 192.168.50.1#53
Jul 21 09:14:54 localhost.localdomain dnsmasq[181227]: read /etc/hosts - 3 addresses
Hint: Some lines were ellipsized, use -l to show in full.
## HTTP服务器(192.168.3.254)
[root@areadetector blctrl]# systemctl start httpd
[root@areadetector blctrl]# systemctl enable httpd
[root@areadetector blctrl]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-07-18 13:55:26 CST; 2 days ago
     Docs: man:httpd.service(8)
 Main PID: 734280 (httpd)
   Status: "Total requests: 6775; Idle/Busy workers 100/0;Requests/sec: 0.0277; Bytes served/sec:  27KB/sec"
    Tasks: 278 (limit: 820636)
   Memory: 60.1M
   CGroup: /system.slice/httpd.service
           ├─734280 /usr/sbin/httpd -DFOREGROUND
           ├─734282 /usr/sbin/httpd -DFOREGROUND
           ├─734283 /usr/sbin/httpd -DFOREGROUND
           ├─734284 /usr/sbin/httpd -DFOREGROUND
           ├─734285 /usr/sbin/httpd -DFOREGROUND
           └─825284 /usr/sbin/httpd -DFOREGROUND

Jul 18 13:55:26 areadetector systemd[1]: Starting The Apache HTTP Server...
Jul 18 13:55:26 areadetector systemd[1]: Started The Apache HTTP Server.
Jul 18 13:55:26 areadetector httpd[734280]: Server configured, listening on: port 80

第七步:开放防火墙并且测试HTTP安装源

12) 为了客户端机器达到并且从PXE服务器引导,在你的防火墙上开放所需的端口,运行netstat命令并且添加CentOS 7防火墙规则到dnsmasq和http监听端口。

[root@localhost areadetector]# netstat -ltpn  # 查看正在监听的tcp端口与进程
[root@localhost areadetector]# netstat -lupn  # 查看正在监听的udp端口与进程
[root@localhost areadetector]# firewall-cmd --permanent --add-service=dns
success
[root@localhost areadetector]# firewall-cmd --permanent --add-service=dhcp
success
# for TFTP server
[root@localhost areadetector]# firewall-cmd --permanent --add-port=69/udp
success
[root@localhost areadetector]# firewall-cmd --permanent --add-port=4011/udp
success
[root@areadetector blctrl]# firewall-cmd --permanent --add-service=http  # 192.168.3.254
success
[root@localhost areadetector]# firewall-cmd --reload
success

13) 要测试HTTP服务器安装源网络路径,在一台计算机上打开一个浏览器并且输入HTTP服务器的IP地址和安装源的URL路径。

 14) 在实时模式中调试PXE服务器,运行以下命令:

[root@localhost areadetector]# tailf /var/log/messages
Jul 21 10:20:54 localhost org.a11y.Bus: Successfully activated service 'org.a11y.atspi.Registry'
Jul 21 10:20:54 localhost org.a11y.atspi.Registry: SpiRegistry daemon is running with well-known name - org.a11y.atspi.Registry
Jul 21 10:20:59 localhost kvm: 4 guests now active
Jul 21 10:20:59 localhost kvm: 3 guests now active
Jul 21 10:21:08 localhost dnsmasq-dhcp[184008]: DHCPREQUEST(br4) 192.168.3.210 c0:3e:ba:61:92:72
Jul 21 10:21:08 localhost dnsmasq-dhcp[184008]: DHCPACK(br4) 192.168.3.210 c0:3e:ba:61:92:72 DESKTOP-KA25I52
Jul 21 10:22:46 localhost dnsmasq-dhcp[184008]: DHCPDISCOVER(br4) 192.168.122.176 52:54:00:91:aa:90
Jul 21 10:22:46 localhost dnsmasq-dhcp[184008]: DHCPOFFER(br4) 192.168.3.240 52:54:00:91:aa:90
Jul 21 10:22:46 localhost dnsmasq-dhcp[184008]: DHCPREQUEST(br4) 192.168.3.240 52:54:00:91:aa:90
Jul 21 10:22:46 localhost dnsmasq-dhcp[184008]: DHCPACK(br4) 192.168.3.240 52:54:00:91:aa:90

第九步:配置客户端从网络引导

15) 现在通过从机器的系统BIOS或者在BIOS POST操作过程中按主板手册指定敲击指定的键配置网络引导作为首要引导设备,你的客户端可以引导并且在机器上安装CentOS 7。

先创建一个选择了Network boot(PXE)的虚拟机:

选择操作系统:

 设置CPU数目和内存大小:

 设置硬盘大小:

 选择网络:

 Display spice选项中spice服务器选择为VNC server,其它保持默认,点击应用以及Begin installation开始安装。

 弹出此窗口后,按下F8,进入系统安装选择界面:

用向下箭头 选择第三项,按回车:

 进入以下界面:

 用VNC客户端软件,连接192.168.3.212,端口为5901,密码为password:

 

 然后按照提示步骤,进行系统安装。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值