暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Deploy PXE for Centos7

Linux技术宅 2023-10-25
76

点击上方:Linux技术宅,关注我!!!

Every Day

滁州西涧

[作者] 韦应物 [朝代] 唐

独怜幽草涧边生,上有黄鹂深树鸣。

春潮带雨晚来急,野渡无人舟自横。

PXE介绍

PXE(Preboot Execution Environment)是一种网络引导协议,用于通过以太网从远程服务器加载操作系统镜像并启动计算机。它最初由Intel开发,现在已经成为许多现代计算机的基础组件之一,尤其是在无盘工作站和网络启动方面。


PXE工作原理:

  • 计算机开机时,BIOS(基本输入输出系统)会检查计算机的启动顺序。

  • BIOS会从启动顺序中的第一个设备(通常是硬盘)加载引导程序。

  • 引导程序负责加载位于启动设备上的操作系统内核和初始化硬件设备。

  • 果引导程序发现计算机通过网络连接到另一台计算机或服务器,它将尝试使用PXE协议从远程计算机加载操作系统镜像。

  • PXE协议使用TFTP(Trivial File Transfer Protocol)协议从远程服务器下载文件。这些文件包括用于启动操作系统的引导加载程序和操作系统内核。

  • 引导加载程序将操作系统内核加载到内存中,然后控制系统将控制权交给操作系统内核,开始执行操作系统启动过程。


PXE的优点:

  • 远程启动:通过PXE协议,计算机可以从远程服务器加载操作系统,从而实现远程启动。这对于无盘工作站和网络启动非常有用,可以减少硬件维护和管理成本。

  • 灵活性:PXE使用TFTP协议下载文件,因此可以轻松地更改操作系统镜像和其他引导文件的位置和名称。这使得管理员可以灵活地管理和部署不同的操作系统版本和配置。

  • 安全:PXE协议使用数字签名来验证从远程服务器加载的文件的完整性。这可以防止恶意软件或损坏的文件被加载到计算机上。

环境

  • IP:192.168.10.10

  • 系统:Centos7.5

  • 硬件:vmware workstation虚拟机

关闭防火墙和SELinux

  • 关闭防火墙

    [root@wyh21 ~]# systemctl disable firewalld --now
    [root@wyh21 ~]# iptables -F
    • 关闭SELinux

      • 将/etc/selinux/config中的SELINUX的值修改为“disabled”。重启生效。

      [root@wyh21 ~]# cat etc/selinux/config


      # This file controls the state of SELinux on the system.
      # SELINUX= can take one of these three values:
      # enforcing - SELinux security policy is enforced.
      # permissive - SELinux prints warnings instead of enforcing.
      # disabled - No SELinux policy is loaded.
      SELINUX=disabled
      # SELINUXTYPE= can take one of three two values:
      # targeted - Targeted processes are protected,
      # minimum - Modification of targeted policy. Only selected processes are protected.
      # mls - Multi Level Security protection.
      SELINUXTYPE=targeted


      # 验证selinux是否关闭
      [root@wyh21 ~]# getenforce
      Disabled


      # 配置立即生效
      [root@wyh21 ~]# setenforce 0

      挂在光盘,配置yum源

        [root@wyh21 ~]# mkdir -p var/www/html/centos7.5
        [root@wyh21 ~]# ls var/www/html/
        centos7.5
        [root@wyh21 ~]# mount dev/sr0 var/www/html/centos7.5/
        mount: dev/sr0 写保护,将以只读方式挂载
        [root@wyh21 ~]# df -h
        文件系统 容量 已用 可用 已用% 挂载点
        /dev/sr0 4.2G 4.2G 0 100% var/www/html/centos7.5


        # 配置yum源
        [root@wyh21 ~]# cat etc/yum.repos.d/centos7.repo
        [centos7]
        name=centos 7
        baseurl=file:///var/www/html/centos7.5
        enabled=1
        gpgcheck=0


        # 检查yum源是否成功
        [root@wyh21 ~]# yum repolist
        已加载插件:fastestmirror, langpacks
        Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
        Determining fastest mirrors
        源标识 源名称 状态
        !centos7 centos 7 3,971
        repolist: 3,971

        安装DHCP、Tftp、http、syslinux服务

          [root@wyh21 ~]# yum install -y dhcp httpd xinetd tftp-server syslinux


          # 结果如下:
          已安装:
          dhcp.x86_64 12:4.2.5-68.el7.centos
          httpd.x86_64 0:2.4.6-80.el7.centos
          syslinux.x86_64 0:4.05-13.el7
          tftp-server.x86_64 0:5.2-22.el7
          xinetd.x86_64 2:2.3.15-13.el7
          作为依赖被安装:
          apr.x86_64 0:1.4.8-3.el7_4.1
          apr-util.x86_64 0:1.5.2-6.el7
          httpd-tools.x86_64 0:2.4.6-80.el7.centos
          mailcap.noarch 0:2.1.41-2.el7
          完毕!

          配置DHCP

            [root@wyh21 ~]# vi etc/dhcp/dhcpd.conf


            #该文件中没有参数,可使用
            :r usr/share/doc/dhcp*/dhcpd.conf.example


            #导入相关参数进行修改
            [root@wyh21 ~]# cat etc/dhcp/dhcpd.conf
            #
            # dhcpd.conf


            subnet 192.168.10.0 netmask 255.255.255.224 {
            range 192.168.10.21 192.168.10.30;
            option domain-name-servers 192.168.10.10;
            option routers 192.168.10.1;
            option broadcast-address 192.168.10.255;
            default-lease-time 21600;
            max-lease-time 43200;
            next-server 192.168.10.10;
            filename "pxelinux.0";
            }
            [root@wyh21 ~]# systemctl enable dhcpd --now
            Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.

            配置Tftp

              #编辑配置文件/etc/xinetd.d/tftp,disable参数yes改为no
              [root@wyh21 ~]# vi /etc/xinetd.d/tftp
              disable = no
              [root@wyh21 ~]# systemctl enable tftp --now
              Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.

              配置http搭建yum源

                [root@wyh21 ~]# systemctl enable httpd --now
                Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
                [root@wyh21 ~]# cat /etc/yum.repos.d/centos7.repo
                [centos7]
                name=centos 7
                baseurl=http://192.168.10.10/centos7.5
                enabled=1
                gpgcheck=0


                #检查yum是否正常。
                [root@wyh21 ~]# yum repolist

                所需文件的复制与修改

                • 拷贝所需文件到tftp服务默认的共享目录/var/lib/tftpboot/

                  [root@wyh21 ~]# cd /var/lib/tftpboot/
                  [root@wyh21 tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
                  [root@wyh21 tftpboot]# cp /var/www/html/centos7.5/images/pxeboot/{initrd.img,vmlinuz} .
                  [root@wyh21 tftpboot]# cp /var/www/html/centos7.5/isolinux/{splash.png,vesamenu.c32,boot.msg} .
                  • 创建目录pxelinux.cfg,并拷贝isolinux.cfg拷贝到pxelinux.cfg目录下命名为default。

                    [root@wyh21 tftpboot]# mkdir pxelinux.cfg
                    [root@wyh21 tftpboot]# cp /var/www/html/centos7.5/isolinux/isolinux.cfg pxelinux.cfg/default
                    [root@wyh21 tftpboot]# ll /var/lib/tftpboot/pxelinux.cfg/
                    -rw-r--r-- 1 root root 3032 821 23:28 default
                    [root@wyh21 tftpboot]# ll
                    总用量 57924
                    -rw-r--r-- 1 root root 84 821 23:25 boot.msg
                    -rw-r--r-- 1 root root 52893200 821 23:22 initrd.img
                    -rw-r--r-- 1 root root 26764 821 23:21 pxelinux.0
                    drwxr-xr-x 2 root root 21 821 23:28 pxelinux.cfg
                    -rw-r--r-- 1 root root 186 821 23:25 splash.png
                    -rw-r--r-- 1 root root 152976 821 23:25 vesamenu.c32
                    -rwxr-xr-x 1 root root 6224704 821 23:22 vmlinuz
                    [root@wyh21 tftpboot]#
                    • 修改/var/lib/tftpboot/pxelinux.cfg/default文件。

                        1 default linux
                      2 timeout 600
                      3
                      61 label linux
                      62 menu label ^Install CentOS 7
                      63 kernel vmlinuz
                       64   append initrd=initrd.img inst.repo=http://192.168.10.10/centos7.5 inst.ks=http://192.168.10.10/ks.cfg quiet
                      • 延申:append参数

                        inst.stage2=hd:LABEL=CentOS\x207\x20x86_64
                        inst.stage2=ftp://192.168.10.10
                        ks=ftp://192.168.10.10/pub/ks.cfg

                        ks.cfg

                        生成自动应答文件ks.cfg,并将其拷贝到/var/www/html/目录下

                        第一种:copy文件修改生成

                          [root@wyh21 tftpboot]# cp ~/anaconda-ks.cfg /var/www/html/ks.cfg
                          [root@wyh21 tftpboot]# chmod +r /var/www/html/ks.cfg
                          [root@wyh21 tftpboot]# vi /var/www/html/ks.cfg
                          4 # Use CDROM installation media
                          5 url --url=http://192.168.10.10/centos7.5

                          16 # Network information
                          17 network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto
                          18 network --hostname=wangyanhui

                          24 # System timezone
                          25 timezone Asia/Shanghai --isUtc


                          30 # Partition clearing information
                              31 clearpart --all --initlabel

                          第二种:配置生成

                          • 安装system-config-kickstart软件包。

                          • 通过命令system-config-kickstart开始配置自动应答文件,这个过程是全图形操作。

                            [root@wyh21 ~]# yum install -y system-config-kickstart
                            已安装:
                            system-config-kickstart.noarch 0:2.9.7-1.el7
                            完毕!
                            [root@wyh21 ~]# system-config-kickstart


                            • 根据需求配置完成后,点击“文件”-“保存”到相应目录,然后关闭配置框即可。

                            参考链接

                            https://blog.51cto.com/pjp5945/1266539




                            分享、在看与点赞

                            只要你点,我们就是胖友

                            ​​​

                            文章转载自Linux技术宅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                            评论