• PXE网络批量装机(centos7)


    目录

    前言

    一、实验拓扑图

    二、PXE的组件

    三、配置PXE装机服务器

    1、设置防火墙、selinux

    2.安装、启动vsftp

    3、拷贝系统文件到/var/ftp用于装机

    4、配置tftp

    5、准备pxelinx.0文件、引导文件、内核文件

    6、配置本机IP

    7、配置DHCP服务

    8、创建default文件

    四、配置中继

    1.添加网卡

    2、配置网卡

    3、添加路由功能

    4、测试pxe 与中继的通联

    五、新建测试主机用来测试装机效果

    1、新建一台网卡2网段主机

    2、新建一台网卡3网段的主机

    六、配置无人值守的pxe装机

    1、图形化配置

    拷贝:从/root/anaconda-ks.cfg文件中拷贝软件安装字段到ks.cfg

    2、修改default文件

    3、验证

    总结



    前言

    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的组件

    1. vsftpd/httpd/nfs负责提供系统的安装文件
    2. tftp负责提供系统安装前的引导文件与内核文件
    3. dhcp负责提供客户端的IP地址分配与pxe引导文件,及pxe服务器地址

    三、配置PXE装机服务器

    1、设置防火墙、selinux
    1. systemctl stop firewalld.service
    2. systemctl enable firewalld.service
    3. setenforce 0
    2.安装、启动vsftp
    1. ######配置本地yum
    2. cd /etc/yum.repos.d
    3. mkdir back
    4. mv CentOS-* back
    5. vim local.repo
    6. ###插入
    7. [local]
    8. name=local
    9. baseurl=file:///mnt
    10. enabled=1
    11. gpgcheck=0
    12. ###挂载sr0,安装vsftpd
    13. mount /dev/sr0 /mnt
    14. yum -y install vsftpd
    15. systemctl start vsftpd
    3、拷贝系统文件到/var/ftp用于装机
    1. cd /var/ftp
    2. mkdir centos7
    3. cp -r /mnt/* /var/ftp/centos7
    4. sync
    4、配置tftp
    1. ###安装
    2. yum install -y tftp-server
    3. ###修改配置文件
    4. vim /etc/xinit.d/tftp
    5. ###修改处
    6. disable=no
    7. ###启动
    8. systemctl start tftp
    5、准备pxelinx.0文件、引导文件、内核文件
    1. ###准备pxelinux.0文件
    2. yum install -y syslinux
    3. cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
    4. ###准备引导文件、内核文件
    5. cd /mnt/images/pxeboot
    6. cp initrd.img vmlinuz /var/lib/tftpboot
    6、配置本机IP

    1. vim /etc/sysconfig/network-scripts/ifcfg-ens33
    2. ###改为
    3. TYPE=Ethernet
    4. BOOTPROTO=static
    5. NAME=ens33
    6. DEVICE=ens33
    7. ONBOOT=yes
    8. IPADDR=192.168.100.253
    9. PREFIX=24
    10. GATEWAY=192.168.100.254
    11. ###保存退出,重启网络、ip a 查看
    12. systemctl restart network
    13. ip a
    7、配置DHCP服务
    1. yum -y install dhcp
    2. cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
    3. vim /etc/dhcp/dhcpd.conf
    4. ##删除前3段的subnet字段,修改剩下的字段
    5. subnet 192.168.100.0 netmask 255.255.255.0 {
    6. range 192.168.100.1 192.168.100.252;
    7. #option domain-name-servers ns1.internal.example.org;
    8. #option domain-name "internal.example.org";
    9. option routers 192.168.100.254;
    10. option broadcast-address 192.168.100.255;
    11. default-lease-time 600;
    12. max-lease-time 7200;
    13. next-server 192.168.100.253;
    14. filename "pxelinux.0";
    15. }
    16. subnet 192.168.200.0 netmask 255.255.255.0 {
    17. range 192.168.200.1 192.168.200.252;
    18. #option domain-name-servers ns1.internal.example.org;
    19. #option domain-name "internal.example.org";
    20. option routers 192.168.200.254;
    21. option broadcast-address 192.168.200.255;
    22. default-lease-time 600;
    23. max-lease-time 7200;
    24. next-server 192.168.100.253;
    25. filename "pxelinux.0";
    26. }
    27. ###启动DHCP服务
    28. systemctl start dhcpd
    8、创建default文件
    1. cd /var/lib/tftpboot
    2. mkdir pxelinux.cfg
    3. cd pxelinux.cfg
    4. vim default
    5. ###插入内容
    6. default auto #默认安装标签
    7. prompt 1 #等待用户确认,1表示等待,0表示不等待
    8. label auto #定义标签
    9. kernel vmlinuz #指定内核
    10. append initrd=initrd.img method=ftp://192.168.100.253/centos7 #指定引导镜像文件与系统安装文件

    有人值守的方式已经基本配置好了

    四、配置中继

    1.添加网卡

    2、配置网卡
    1. ###设置防火墙、selinux
    2. systemctl stop firewalld.service
    3. setenforce 0
    4. ###
    5. cd /etc/sysconfig/network-scripts
    6. vim ifcfg-ens33
    7. ##改为
    8. TYPE=Ethernet
    9. BOOTPROTO=static
    10. NAME=ens33
    11. DEVICE=ens33
    12. ONBOOT=yes
    13. IPADDR=192.168.100.254
    14. PREFIX=24
    15. ###############################################
    16. vim ifcfg-ens37
    17. ##改为
    18. OTPROTO=static
    19. NAME=ens37
    20. DEVICE=ens37
    21. ONBOOT=yes
    22. IPADDR=192.168.200.254
    23. PREFIX=24
    3、添加路由功能
    1. ####安装DHCP
    2. yum -y install dhcp
    3. dhcrelay 192.168.100.253
    4. vim /etc/sysctl.conf
    5. ####文末插入
    6. net.ipv4.ip_forward = 1
    7. ###保存退出
    8. sysctl -p
    4、测试pxe 与中继的通联

    五、新建测试主机用来测试装机效果

    1、新建一台网卡2网段主机

    都选下一步

    使用网卡2

    开机,连接成功,按下回车开始安装

    2、新建一台网卡3网段的主机

    创建方式同上,我就不啰嗦了。这里把网卡改成3就好

    开机验证

    回车+耐心等待

    测试完毕,结果ok

    六、配置无人值守的pxe装机

    1、图形化配置
    1. ##使用图形界面配置
    2. yum install -y system-config-kickstart.noarch
    3. system-config-kickstart

    执行完上述命令后会出现图形化界面

    下面开始配置

    脚本看你的需求

    保存

    查看保存文件的位置

    拷贝:从/root/anaconda-ks.cfg文件中拷贝软件安装字段到ks.cfg
    1. vim anaconda-ks.cfg
    2. ##复制以下字段插入到ks.cfg
    3. %packages
    4. @^graphical-server-environment
    5. @base
    6. @core
    7. @desktop-debugging
    8. @development
    9. @dial-up
    10. @fonts
    11. @gnome-desktop
    12. @guest-agents
    13. @guest-desktop-agents
    14. @hardware-monitoring
    15. @input-methods
    16. @internet-browser
    17. @multimedia
    18. @print-client
    19. @x11
    20. chrony
    21. kexec-tools
    22. %end

    这是一个centos7最小安装的ks.cfg

    1. #platform=x86, AMD64, 或 Intel EM64T
    2. #version=DEVEL
    3. # Install OS instead of upgrade
    4. install
    5. # Keyboard layouts
    6. keyboard 'us'
    7. # Root password
    8. rootpw --plaintext 123.com
    9. # System language
    10. lang zh_CN
    11. # System authorization information
    12. auth --useshadow --passalgo=sha512
    13. # Use graphical install
    14. graphical
    15. firstboot --disable
    16. # SELinux configuration
    17. selinux --enforcing
    18. # Firewall configuration
    19. firewall --disabled
    20. # Reboot after installation
    21. reboot
    22. # System timezone
    23. timezone Asia/Shanghai
    24. # Use network installation
    25. url --url="ftp://你的ip/centos7"
    26. # System bootloader configuration
    27. bootloader --location=mbr
    28. # Partition clearing information
    29. clearpart --all
    30. # Disk partitioning information
    31. part /boot --asprimary --fstype="xfs" --size=200
    32. part / --asprimary --fstype="xfs" --grow --size=1
    33. %packages --nobase
    34. @core
    35. %end

    拷贝

    cp ks.cfg /var/ftp
    2、修改default文件
    1. vim /var/lib/tftpboot/pxelinux.cfg/default
    2. ###修改
    3. default auto
    4. prompt 0
    5. label auto
    6. kernel vmlinuz
    7. 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服务器地址

    1. #/bin/bash
    2. #hy
    3. ###挂载sr0,安装vsftpd
    4. mount /dev/sr0 /mnt
    5. yum -y install vsftpd
    6. systemctl start vsftpd
    7. cd /var/ftp
    8. mkdir centos7
    9. cp -r /mnt/* /var/ftp/centos7
    10. ###安装
    11. yum install -y tftp-server
    12. ###修改处
    13. sed -i 's/disable = yes/disable = no/g' /etc/xinetd.d/tftp
    14. ###启动
    15. systemctl restart tftp
    16. ###准备pxelinux.0文件
    17. yum install -y syslinux
    18. cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
    19. ###准备引导文件、内核文件
    20. cd /mnt/images/pxeboot
    21. cp initrd.img vmlinuz /var/lib/tftpboot
    22. ######
    23. yum -y install dhcp
    24. rm -rf /etc/dhcp/dhcpd.conf
    25. ##配置DHCP
    26. cat <> /etc/dhcp/dhcpd.conf
    27. # dhcpd.conf
    28. #
    29. # Sample configuration file for ISC dhcpd
    30. #
    31. # option definitions common to all supported networks...
    32. option domain-name "example.org";
    33. option domain-name-servers ns1.example.org, ns2.example.org;
    34. default-lease-time 600;
    35. max-lease-time 7200;
    36. # Use this to enble / disable dynamic dns updates globally.
    37. #ddns-update-style none;
    38. # If this DHCP server is the official DHCP server for the local
    39. # network, the authoritative directive should be uncommented.
    40. #authoritative;
    41. # Use this to send dhcp log messages to a different log file (you also
    42. # have to hack syslog.conf to complete the redirection).
    43. log-facility local7;
    44. # No service will be given on this subnet, but declaring it helps the
    45. # DHCP server to understand the network topology.
    46. # This is a very basic subnet declaration.
    47. # This declaration allows BOOTP clients to get dynamic addresses,
    48. # which we don't really recommend.
    49. # A slightly different configuration for an internal subnet.
    50. subnet 192.168.100.0 netmask 255.255.255.0 {
    51. range 192.168.100.3 192.168.100.252;
    52. #option domain-name-servers ns1.internal.example.org;
    53. #option domain-name "internal.example.org";
    54. option routers 192.168.100.100;
    55. option broadcast-address 192.168.100.255;
    56. default-lease-time 600;
    57. max-lease-time 7200;
    58. next-server 192.168.100.100;
    59. filename "pxelinux.0";
    60. }
    61. # Hosts which require special configuration options can be listed in
    62. # host statements. If no address is specified, the address will be
    63. # allocated dynamically (if possible), but the host-specific information
    64. # will still come from the host declaration.
    65. host passacaglia {
    66. hardware ethernet 0:0:c0:5d:bd:95;
    67. filename "vmunix.passacaglia";
    68. server-name "toccata.fugue.com";
    69. }
    70. # Fixed IP addresses can also be specified for hosts. These addresses
    71. # should not also be listed as being available for dynamic assignment.
    72. # Hosts for which fixed IP addresses have been specified can boot using
    73. # BOOTP or DHCP. Hosts for which no fixed address is specified can only
    74. # be booted with DHCP, unless there is an address range on the subnet
    75. # to which a BOOTP client is connected which has the dynamic-bootp flag
    76. # set.
    77. host fantasia {
    78. hardware ethernet 08:00:07:26:c0:a5;
    79. fixed-address fantasia.fugue.com;
    80. }
    81. # You can declare a class of clients and then do address allocation
    82. # based on that. The example below shows a case where all clients
    83. # in a certain class get addresses on the 10.17.224/24 subnet, and all
    84. # other clients get addresses on the 10.0.29/24 subnet.
    85. class "foo" {
    86. match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
    87. }
    88. shared-network 224-29 {
    89. subnet 10.17.224.0 netmask 255.255.255.0 {
    90. option routers rtr-224.example.org;
    91. }
    92. subnet 10.0.29.0 netmask 255.255.255.0 {
    93. option routers rtr-29.example.org;
    94. }
    95. pool {
    96. allow members of "foo";
    97. range 10.17.224.10 10.17.224.250;
    98. }
    99. pool {
    100. deny members of "foo";
    101. range 10.0.29.10 10.0.29.230;
    102. }
    103. }
    104. EOF
    105. systemctl restart dhcpd
    106. ##default配置
    107. mkdir /var/lib/tftpboot/pxelinux.cfg
    108. cat <> /var/lib/tftpboot/pxelinux.cfg/default
    109. default auto
    110. prompt 0
    111. label auto
    112. kernel vmlinuz
    113. append initrd=initrd.img method=ftp://192.168.100.100/centos7 ks=ftp://192.168.100.100/ks.cfg
    114. EOF
    115. rm -rf /var/ftp/ks.cfg
    116. ##ks.cfg配置
    117. cat <> /var/ftp/ks.cfg
    118. #platform=x86, AMD64, 或 Intel EM64T
    119. #version=DEVEL
    120. # Install OS instead of upgrade
    121. install
    122. # Keyboard layouts
    123. keyboard 'us'
    124. # Root password
    125. rootpw --plaintext 123.com
    126. #密码123456
    127. # System language
    128. lang zh_CN
    129. # System authorization information
    130. auth --useshadow --passalgo=sha512
    131. # Use graphical install
    132. graphical
    133. firstboot --disable
    134. # SELinux configuration
    135. selinux --enforcing
    136. # Firewall configuration
    137. firewall --disabled
    138. # Reboot after installation
    139. reboot
    140. # System timezone
    141. timezone Asia/Shanghai
    142. # Use network installation
    143. url --url="ftp://192.168.100.100/centos7"
    144. # System bootloader configuration
    145. bootloader --location=mbr
    146. # Partition clearing information
    147. clearpart --all
    148. # Disk partitioning information
    149. part /boot --asprimary --fstype="xfs" --size=200
    150. part / --asprimary --fstype="xfs" --grow --size=1
    151. %packages --nobase
    152. @core
    153. %end
    154. EOF

  • 相关阅读:
    计算机网络---TCP/UDP
    mysql操作 sql语句中的完整性约束有哪些,主键约束、外键约束、引用完整性约束,主键外键、唯一性
    Java】实现图片验证码2.0【详细代码】
    高薪程序员&面试题精讲系列131之Eureka如何实现高可用?自我保护机制是怎么回事?
    单链表的介绍和内存布局 [数据结构][Java]
    LeetCode617. Merge Two Binary Trees
    【蓝桥】数树数
    (181)Verilog HDL:设计一个计数器count_clock
    为什么C4D能成为电商设计的王者?
    外卖App点菜页-两个tableView联动【1】
  • 原文地址:https://blog.csdn.net/2302_78534730/article/details/132602513