AlexRomeo
发布于 2025-04-29 / 32 阅读
0
2

Ubuntu部署PXE-Server

一、环境准备

  1. 服务器配置要求

    • Ubuntu Server系统(推荐20.04/22.04 LTS)
    • 双网卡(外网+内网)或单网卡绑定静态IP,文档编写时,环境为双网卡。
  2. 软件包安装

    sudo apt update && sudo apt install -y isc-dhcp-server tftpd-hpa tftp-hpa apache2 syslinux pxelinux syslinux-common
    
  3. 系统镜像包选择:

    官方最新版本的桌面版或server(文档所用版本为22.04的Server版)

  4. 配置网卡的静态ip地址信息

    Ubuntu不同于centos系统,网卡的配置文件及路径均不同,由于文档编写时所用的系统版本未使用Ubuntu的Desktop版,而是22.04 Server版,网络中的文档可能大部分是都是告知修改配置文件/etc/netplan/00-installer-config.yaml,但Server版本使用的是50-cloud-init.yaml(也许我是个特例),Netpaln的配置文件按照字母顺序加载,00-*的优先级会低于50-*,高序号文件可能会覆盖地序号配置。

    备份原文件

    sudo cp /etc/netplan/50-cloud-init.yaml  /etc/netplan/50-cloud-init.yaml.bak
    

    编辑配置文件

    sudo vim /etc/netplan/50-cloud-init.yaml
    

    配置文件内容

    # This file is generated from information provided by the datasource.  Changes
    # to it will not persist across an instance reboot.  To disable cloud-init's
    # network configuration capabilities, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    network:
        ethernets:
            ens160:
                dhcp4: no
                addresses: [10.1.215.254/24]
            ens192:
                dhcp4: true
        version: 2
    

    应用配置文件

    # 语法检查(避免配置文件格式错误)
    sudo netplan generate
    
    # 生效配置
    sudo netplan apply
    
  5. 关闭防火墙

    sudo systemctl stop ufw.service
    sudo systemctl disable ufw.service
    

二、服务配置

1. 搭建HTTP服务器

  • 拷贝镜像

    # 创建存放的目录
    sudo mkdir /var/www/html/ubuntu_22.04
    
    ## 两种方式,如果镜像是通过官方网站下载到本机的硬盘中,可以直接拷贝过去
    sudo cp /{path}/ubuntu你的系统镜像全称.iso /var/www/html/ubuntu_22.04/
    
    ## 如果是通过光驱挂载的镜像,执行下面的命令,一定要记得给文件名称,否则后续找不到文件
    sudo cp /dev/cdrom /var/www/html/ubuntu_22.04/ubuntu_24.04_live_server_amd64_lts.iso
    sudo chmod 644 /var/www/html/ubuntu_22.04/ubuntu_24.04_live_server_amd64_lts.iso
    
  • 将http服务(apache2)设置为开机启动

    sudo systemctl enable apache2
    

2. DHCP服务配置

  • 指定/etc/default/isc-dhcp-server要监听的网卡,打开配置文件

    # Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)
    
    # Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
    #DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
    #DHCPDv6_CONF=/etc/dhcp/dhcpd6.conf
    
    # Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
    #DHCPDv4_PID=/var/run/dhcpd.pid
    #DHCPDv6_PID=/var/run/dhcpd6.pid
    
    # Additional options to start dhcpd with.
    #       Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
    #OPTIONS=""
    
    # On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
    #       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
    INTERFACESv4="ens160"
    # INTERFACESv6=""
    
  • 编辑配置文件/etc/dhcp/dhcpd.conf

    subnet 10.1.215.0 netmask 255.255.255.0 {
      range 10.1.215.1 10.1.215.253;
      option routers 10.1.215.254;
      option domain-name-servers 192.168.215.248;
      filename "pxelinux.0";
      next-server 10.1.215.254; 
    }
    
  • 配置解析:

    subnet 10.1.215.0 netmask 255.255.255.0 {		# 定义一个 IPv4 子网范围,指定 DHCP 服务的作用域为 10.1.215.0/24 网段
      range 10.1.215.100 10.1.215.200;				# 设置 DHCP 可动态分配的 IP 地址范围
      option routers 10.1.215.254;					# 为客户机指定默认网关(通常为路由器或三层交换机 IP),也可以是本机IP地址
      option domain-name-servers 192.168.215.248;	# 为客户机配置 DNS 解析服务器地址
      filename "pxelinux.0";						# 指定 PXE(预启动执行环境)客户端的引导文件名
      next-server 10.1.215.254; 					# 指定提供 PXE 引导文件的服务器的 IP 地址
    }
    
  • 重启服务及设置开机自启

    sudo systemctl restart isc-dhcp-server 
    sudo systemctl enable isc-dhcp-server
    
  • 查看状态

    sudo systemctl status isc-dhcp-server
    

    注意:一般正常情况下dhcp是可以正常启动的,但如果失败的话,查看状态中的信息比较少,不足以支撑排查小白排查错误,可以通过命令sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf来排查文件中的错误,如下面的信息中为配置文件最后一行next-server少了一个字母r,错写成了next-sever导致dhcp服务无法启动。

    Internet Systems Consortium DHCP Server 4.4.3-P1
    Copyright 2004-2022 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/
    /etc/dhcp/dhcpd.conf line 118: semicolon expected.
      next-sever 10.
                  ^
    Configuration file errors encountered -- exiting
    
    If you think you have received this message due to a bug rather
    than a configuration issue please read the section on submitting
    bugs on either our web page at www.isc.org or in the README file
    before submitting a bug.  These pages explain the proper
    process and the information we find helpful for debugging.
    

3. 自动安装客户端

  • 创建自动配置安装文件及目录

    sudo mkdir /var/www/html/autoinstall
    sudo touch /var/www/html/autoinstall/meta-data
    sudo vim /var/www/html/autoinstall/user-data
    
  • 编写user-data配置文件

    #cloud-config
    autoinstall:
      version: 1
      # 设置语言
      locale: en_US.UTF-8
      # 设置时区
      timezone: Asia/Shanghai
      keyboard:
        layout: us
      # 安装ubuntu服务器版,最小化选择 ubuntu-mini
      source:
        id: ubuntu-server
        search_drivers: false
      # 网络配置,根据具体接口名称修改。
      network:
        version: 2
        ethernets:
          ens160:
            dhcp4: true
            optional: false
          ens192:
            dhcp4: true
            optional: false
      # 是否安装第三方驱动
      drivers:
        install: false
      # 内核安装,linux-generic 通用内核
      kernel:
        package: linux-generic
      # 设置存储库,使用阿里云
      apt:
        fallback: offline-install
        mirror-selection:
          primary:
          - arches: [amd64]
            uri: http://mirrors.aliyun.com/ubuntu/
      # 配置存储,分区
      storage:
        config:
          - type: disk
            id: disk-sda
            path: /dev/sda
            ptable: gpt
            wipe: superblock-recursive
            grub_device: true
    
          - type: partition
            id: partition-bios
            device: disk-sda
            size: 1M
            flag: bios_grub
    
          - type: partition
            id: partition-boot
            device: disk-sda
            size: 300M
            wipe: superblock
            flag: boot
    
          - type: partition
            id: partition-root
            device: disk-sda
            size: -1
    
          - type: format
            id: format-boot
            volume: partition-boot
            fstype: ext4
    
          - type: format
            id: format-root
            volume: partition-root
            fstype: ext4
    
          - type: mount
            id: mount-boot
            device: format-boot
            path: /boot
    
          - type: mount
            id: mount-root
            device: format-root
            path: /
      # 配置账号信息
      identity:
        hostname: ubuntu
        username: ubuntu
        password: $6$txi43/WSfR1MfO15$HKIhytAE/ObTrte0TVjVx228GoewYXiT15DaiUZKk/Nl4OqT0T8djKoXpumpdf/O/QdUs9BX03TnLRXeYlizD0
      # ssh配置
      ssh:
        allow-pw: true
        install-server: true
    

    上面配置文件中的密码为哈希加密后的密文,明文为ubuntu,如果希望自己修改密码可以自行修改

  • 安装密码哈希加密工具

    sudo apt-get install whois
    
  • 生成密码

    # 命令执行后输入想要的密码则会返回哈希加密后的密文
    mkpasswd -m sha-512
    
  • 验证文件完整性及正确性

    sudo apt install cloud-init
    cloud-init schema --config-file /var/www/html/autoinstall/user-data
    

4. TFTP服务配置

  • 创建目录结构

    sudo mkdir -p /srv/tftp
    
  • 挂载镜像获取所需文件

    sudo mount /dev/cdrom /var/www/html/ubuntu_22.04/
    
  • 拷贝文件

    sudo cp /var/www/html/ubuntu_22.04/casper/{vmlinuz,initrd} /srv/tftp/
    sudo cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libutil.c32,menu.c32,vesamenu.c32} /srv/tftp/
    sudo cp /usr/lib/PXELINUX/{lpxelinux.0,pxelinux.0} /srv/tftp/
    
  • 取消挂载,避免影响读取iso文件

    sudo umount -lf /var/www/html/ubuntu_22.04
    
  • 创建pxelinux配置文件

    sudo mkdir /srv/tftp/pxelinux.cfg
    sudo vim /srv/tftp/pxelinux.cfg/default
    
  • 配置文件内容

    DEFAULT menu.c32
    MENU TITLE Ubuntu AutoInstall PXE SERVER - By AlwxRomeo - Ver 1.0
    PROMPT 0
    TIMEOUT 5
    
    MENU COLOR TABMSG  37;40  #ffffffff #00000000
    MENU COLOR TITLE   37;40  #ffffffff #00000000
    MENU COLOR SEL      7     #ffffffff #00000000
    MENU COLOR UNSEL    37;40 #ffffffff #00000000
    MENU COLOR BORDER   37;40 #ffffffff #00000000
    
    LABEL Ubuntu Server 22.04 ISO IPv4
        kernel /vmlinuz
        initrd /initrd
        APPEND root=/dev/ram0 ramdisk_size=1024 ip=dhcp url=http://10.1.215.254/ubuntu_22.04/ubuntu_24.04_live_server_amd64_lts.iso autoinstall ds=nocloud-net;s=http://10.1.215.254/autoinstall/ cloud-config-url=/dev/null
        reboot=y
    

    Tips:url需要和append在同一行,长度可能会因为文档的问题导致换行。

  • 编写TFTP配置文件/etc/default/tftpd-hpa

    TFTP_USERNAME="tftp"
    TFTP_DIRECTORY="/srv/tftp"
    TFTP_ADDRESS="0.0.0.0:69"
    TFTP_OPTIONS="--secure"
    

    修改配置文件,重启服务

    # 重启
    sudo systemctl restart tftpd-hpa 
    # 设置开机
    sudo systemctl enable tftpd-hpa.service
    

5. 静态路由配置(可选)

因为双网卡的缘故,可能会导致PXE的客户端无法正确读取到PXE服务端的配置文件及镜像文件。处理的方法有两种,一种是将客户端的非PXE网络网卡禁掉,后续手动启动,另一种方法就是在路由器中(企业网络可能会有三层交换)进行配置静态路由。

比如我当前的网络中10.1.215.0/24的网段,而另一个192.168.100.0/24的网段,10网段用于访问pxe环境,192网段用于正常的互联网访问,此时就需要在配置一条静态路由,访问10段的所有网络全部都交给PXE Server机器的192的ip地址作为网关,否则会出现通过http访问失败。


评论