ubuntu 24 PXE Server (bios+uefi) 批量部署系统

pxe server 前言

PXE(Preboot eXecution Environment,预启动执行环境)是一种网络启动协议,允许计算机通过网络启动而不是使用本地硬盘。PXE服务器是实现这一功能的服务器,它提供了启动镜像和引导加载程序,使得客户端计算机可以通过网络启动并安装操作系统或运行其他软件。

在Debian系统中,要设置一个PXE服务器,您需要以下几个组件:

  • TFTP服务器:用于提供启动镜像和引导加载程序。
  • DHCP服务器:用于分配IP地址给客户端计算机。
  • NFS或HTTP服务器:用于提供操作系统镜像和其他文件。

在Debian中,可以使用以下软件包来设置PXE服务器:

  • atftpd:一个轻量级的TFTP服务器,可以用于提供启动镜像和引导加载程序。
  • dnsmasq:一个轻量级的DHCP和DNS服务器,可以用于分配IP地址给客户端计算机。
  • nfs-kernel-server:用于提供NFS服务,以便客户端可以访问操作系统镜像和其他文件。

ubuntu download

|-----------------------------------------------------|-------------------------------------------------------------|------------------------------------------------------------------|---------------------------------------------------|--------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|----------------------------------------------------|
| ubuntu amd | ubuntu 其它 | ubuntu 历史版 | ubuntu 生命周期 | ubuntu 自动化编排user-data | debian dhcp 配置 | ubuntu 全球镜像站 |
| download | download | download | 参考 | 参考 | 参考 | 参考 |
| download | download | download | 参考 | 参考 | 参考 | 参考 |
| download | download | download | 参考 | 参考 | 参考 | 参考 |

创建一键部署pxe server 环境脚本

  • 安装完成用户密码ubuntu/1234.com(root强制更新密码ubuntu24.04,默认密码1234.com

  • dhcp /etc/dhcp/dhcpd.conf 配置文件

  • tftp 配置文件/etc/default/tftpd-hpa

  • tftp 69

  • apache2 配置文件/etc/apache2/apache2.conf

  • apache2 端口80

  • syslinux 文件 /usr/lib/syslinux/modules/bios/ #需要安装syslinux

  • pxelinux 文件 /usr/lib/PXELINUX/ #需要安装pxelinux

  • /var/lib/tftp/pxelinux.cfg/default (bios 支持)配置文件

  • /var/lib/tftp/grub/grub.cfg (uefi 支持)配置文件

  • /var/www/html/ubuntu24.04/user-data #cloud-init #自动安装配置,user-data 配置增加了,root允许远程连接,禁止系统待机,禁止安装弹出窗口,开启系统默认使用vim,系统默认使用sh,分区结构LVM

  • /var/www/html/ubuntu24.04/meta-data 空文件创建

  • mkpasswd -m sha-512 生成密钥 安装whois

  • /etc/apt/soures.list.d/ubuntu.sources 在线源生成在此位置

  • 脚本自动化部署兼容 ubuntu 22,23,24

  • 17-52 行变量内容详细注意,更改自己需要的,自动化脚本仔细看

  • ds=nocloud-net;s= Bios启动特殊字符去除\不需要转义

  • ds=nocloud-net\;s= UEFI启动grub将;识别为了特殊字符要在;前加\转义

    vim /ubuntu_pxe_server_bios_uefi.sh

    #!/bin/bash

    -- coding: utf-8 --

    Author: make.han

    Email: CIASM@CIASM

    Date: 2024/07/19

    install PXE Server ubuntu 22 23 24

    <<!
    #自动安装简介
    https://canonical-subiquity.readthedocs-hosted.com/en/latest/intro-to-autoinstall.html
    https://canonical-subiquity.readthedocs-hosted.com/en/latest/howto/autoinstall-quickstart.html

    #user-data 配置
    https://canonical-subiquity.readthedocs-hosted.com/en/latest/reference/autoinstall-reference.html
    !

    #tftp variable configuration
    tftp_port=69
    tftp_user=tftp
    tftp_catalogue=/var/lib/tftp

    #dhcp ip address variable configuration
    nic_network_name=ifconfig -s | awk 'NR>1 && !/^lo/ && !/^idrac/ && !/^br/ && !/^veth/ && !/^docker/{print $1; exit}'
    host_IP=ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2}' | awk 'NR==1'
    IP="192.168.11.179"
    MASK="255.255.255.0"
    BROADCAST_ADDRESS="192.168.11.255"
    ROUTERS="192.168.11.1"
    SUBNET="192.168.11.0"
    DNS="8.8.8.8"
    RANGE="192.168.11.50 192.168.11.80"

    user automated scripts user-data

    root用户,普通用户 密码使用crypt(3)哈希进行加密(apt install -y whois) (mkpasswd -m sha-512 生成密钥)

    root_user='root'
    root_password='$6$4QaHWOfkEYxT2Dv6$3h5T/4AT/vkINa.R.9tToEUwro5YqD2UjulKRQ8k8ZMjqJOhib23nZ/fnKKgRh5TRaTg6I1mIa8VWJxmKQR7o0'
    ubunutu_user='ubuntu'
    ubunutu_hostname='ubuntu'
    ubuntu_password='$6$4QaHWOfkEYxT2Dv6$3h5T/4AT/vkINa.R.9tToEUwro5YqD2UjulKRQ8k8ZMjqJOhib23nZ/fnKKgRh5TRaTg6I1mIa8VWJxmKQR7o0'
    timezone='Asia/Shanghai'

    apache2 variable configuration

    apache_port=80
    apache_catalogue=/var/www/html

    ubuntu download variable configuration

    ubuntu22_download_url=https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/22.04/ubuntu-22.04.4-live-server-amd64.iso
    ubuntu24_download_url=https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/24.04/ubuntu-24.04-live-server-amd64.iso
    ubuntu22_iso=ubuntu-22.04.4-live-server-amd64.iso
    ubuntu24_iso=ubuntu-24.04-live-server-amd64.iso
    ubuntu22_catalogue=ubuntu22.04
    ubuntu24_catalogue=ubuntu24.04

    install_pxe_server (){

    if ! [ -x "$(command -v dhcpd)" ]; then

      if [ $? -eq 0 ];then
    

    echo "install firewalld curl"
    apt install -y firewalld curl
    firewall-cmd --zone=public --add-port=tftp_port/tcp --permanent && firewall-cmd --reload firewall-cmd --zone=public --add-port=tftp_port/udp --permanent && firewall-cmd --reload

    echo "install whois mkpasswd"
    apt install -y whois

    echo "install dhcp"
    apt install -y isc-dhcp-server

    echo "isc-dhcp-server add nic"
    sed -i "s/^INTERFACESv4=.*/INTERFACESv4=\"nic_network_name"/" /etc/default/isc-dhcp-server

    echo "configuration dhcpd.conf"
    rm -rf /etc/dhcp/dhcpd.conf
    cat >> /etc/dhcp/dhcpd.conf << EOF
    option domain-name "$DNS";
    option domain-name-servers $DNS;
    default-lease-time 2592000;
    max-lease-time 2592000;
    authoritative;

    add follows

    option space pxelinux;
    option pxelinux.magic code 208 = string;
    option pxelinux.configfile code 209 = text;
    option pxelinux.pathprefix code 210 = text;
    option pxelinux.reboottime code 211 = unsigned integer 32;
    option architecture-type code 93 = unsigned integer 16;

    subnet $SUBNET netmask $MASK {
    range dynamic-bootp $RANGE;
    option broadcast-address $BROADCAST_ADDRESS;
    option routers $ROUTERS;

      #add follows
      class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          # PXE servers hostname or IP address
          next-server $IP;
          if option architecture-type = 00:07 {
              filename "bootx64.efi";
          }
          else {
              filename "pxelinux.0";
          }
      }
    

    }
    EOF

    echo "restart dhcp"
    systemctl enable isc-dhcp-server
    systemctl restart isc-dhcp-server

    echo "install tftpd-hpa"
    apt install -y tftpd-hpa

    echo "configuration tftpd-hpa"
    rm -rf /etc/default/tftpd-hpa
    cat <<EOF>>/etc/default/tftpd-hpa
    TFTP_USERNAME="tftp_user" TFTP_DIRECTORY="tftp_catalogue/"
    TFTP_ADDRESS=":$tftp_port"
    TFTP_OPTIONS="--secure"
    EOF

    echo "Create a new tftp directory"
    mkdir -p $tftp_catalogue

    echo "start tftpd-hpa"
    systemctl enable tftpd-hpa
    systemctl start tftpd-hpa

    echo "install apache2"
    apt install -y apache2

    echo "Delete the default apache2 page"
    rm -rf $apache_catalogue/index.html

    echo "restart apache2"
    systemctl enable apache2
    systemctl restart apache2

    echo "apache2 firewall"
    firewall-cmd --zone=public --add-port=$apache_port/tcp --permanent && firewall-cmd --reload

    echo "mkdir catalogue ubuntu22.04 ubuntu24.04"
    mkdir -p apache_catalogue/{ubuntu22_catalogue,$ubuntu24_catalogue}

    echo "download ubuntu22.04 ubuntu24.04"
    curl -o apache_catalogue/ubuntu24_catalogue/$ubuntu24_iso $ubuntu24_download_url
    curl -o apache_catalogue/ubuntu22_catalogue/$ubuntu22_iso $ubuntu22_download_url

    echo "mount ubuntu22.04 ubuntu24.04"
    mkdir -p /mnt/{ubuntu22_catalogue,ubuntu24_catalogue}
    mount -t iso9660 -o loop,ro apache_catalogue/ubuntu24_catalogue/ubuntu24_iso /mnt/ubuntu24_catalogue
    mount -t iso9660 -o loop,ro apache_catalogue/ubuntu22_catalogue/ubuntu22_iso /mnt/ubuntu22_catalogue

    echo "syslinux pxelinux"
    apt install -y syslinux pxelinux

    echo "copy ubuntu22.04 ubuntu24.04"
    mkdir -p tftp_catalogue/{ubuntu22_catalogue,ubuntu24_catalogue} cp /mnt/ubuntu24_catalogue/casper/{vmlinuz,initrd} tftp_catalogue/ubuntu24_catalogue
    cp /mnt/$ubuntu24_catalogue/casper/{vmlinuz,initrd} tftp_catalogue/ubuntu22_catalogue

    echo "copy syslinux"
    cp /usr/lib/syslinux/modules/bios/* $tftp_catalogue/
    #cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libutil.c32,menu.c32,vesamenu.c32} $tftp_catalogue/
    cp /usr/lib/PXELINUX/{lpxelinux.0,pxelinux.0} $tftp_catalogue/

    echo "mkdir pxelinux.cfg"
    mkdir -p tftp_catalogue/pxelinux.cfg cat <>tftp_catalogue/pxelinux.cfg/default

    change like follows

    #Official page display
    default vesamenu.c32

    #Simple page display
    #default menu.c32

    #Set the home page timeout period according to project requirements timeout 30
    timeout 300

    #/var/lib/tftpboot/下,命名为you.png
    #menu background ubunutu.png

    menu title ########## PXE Boot Menu #########
    display boot.msg

    label Auto Install ubuntu 22.04
    menuentry ^Auto Install ubuntu22.04
    menu default
    kernel $ubuntu22_catalogue/vmlinuz
    initrd ubuntu22_catalogue/initrd append ip=dhcp url=http://{host_IP}/ubuntu22_catalogue/ubuntu22_iso autoinstall ds=nocloud-net;s=http://${host_IP}/$ubuntu22_catalogue/

    label Auto Install ubuntu 24.04
    menuentry ^Auto Install ubuntu24.04
    menu default
    kernel $ubuntu24_catalogue/vmlinuz
    initrd ubuntu24_catalogue/initrd append ip=dhcp url=http://{host_IP}/ubuntu24_catalogue/ubuntu24_iso autoinstall ds=nocloud-net;s=http://${host_IP}/$ubuntu24_catalogue/

    label Manual ubuntu 22.04
    menu label ^Manual Install ubuntu 22.04
    kernel $ubuntu22_catalogue/vmlinuz
    initrd ubuntu22_catalogue/initrd append ip=dhcp url=http://{host_IP}/ubuntu22_catalogue/ubuntu22_iso

    label Manual ubuntu 24.04
    menu label ^Manual Install ubuntu 24.04
    kernel $ubuntu24_catalogue/vmlinuz
    initrd ubuntu24_catalogue/initrd append ip=dhcp url=http://{host_IP}/ubuntu24_catalogue/ubuntu24_iso

    label local
    #menu default
    com32 chain.c32
    menu label Boot from ^local drive
    localboot 0xffff
    menu end
    EOF

    echo "creation grub.cfg"
    mkdir -p tftp_catalogue/grub cat <>tftp_catalogue/grub/grub.cfg

    ubuntu 24

    set default="3"

    set timeout=10
    set gfxpayload=keep
    set color_normal=white/black

    function load_video {
    insmod video_bochs
    insmod video_cirrus
    insmod all_video
    }

    load_video
    insmod gzio
    insmod part_msdos
    insmod part_gpt
    insmod ext2
    insmod xfs
    insmod png
    insmod gfxterm
    insmod gfxmenu
    terminal_output gfxterm
    background_image -m stretch bg.png

    menuentry 'EFI Firmware System Setup' 'uefi-firmware' {
    fwsetup
    }

    menuentry 'Reboot System' {
    reboot
    }

    menuentry 'Shutdown System' {
    halt
    }

    menuentry 'Auto Install ubuntu22.04' {
    linux ubuntu22_catalogue/vmlinuz ip=dhcp url=http://{host_IP}/ubuntu22_catalogue/ubuntu22_iso autoinstall ds=nocloud-net;s=http://${host_IP}/$ubuntu22_catalogue/
    initrd $ubuntu22_catalogue/initrd
    }

    menuentry 'Auto Install ubuntu24.04' {
    linux ubuntu24_catalogue/vmlinuz ip=dhcp url=http://{host_IP}/ubuntu24_catalogue/ubuntu24_iso autoinstall ds=nocloud-net;s=http://${host_IP}/$ubuntu24_catalogue/
    initrd $ubuntu24_catalogue/initrd
    }

    menuentry 'Manual Install ubuntu 22.04' {
    linux ubuntu22_catalogue/vmlinuz ip=dhcp url=http://{host_IP}/ubuntu22_catalogue/ubuntu22_iso
    initrd $ubuntu22_catalogue/initrd
    }

    menuentry 'Manual Install ubuntu 24.04' {
    linux ubuntu24_catalogue/vmlinuz ip=dhcp url=http://{host_IP}/ubuntu24_catalogue/ubuntu24_iso
    initrd $ubuntu24_catalogue/initrd
    }
    EOF

    echo "add ubuntu 24.04 user-data"
    cat <<EOF>>apache_catalogue/ubuntu24_catalogue/user-data
    #cloud-config
    autoinstall:
    version: 1
    apt:
    primary:
    - arches: [i386, amd64]
    uri: http://mirrors.aliyun.com/ubuntu
    - arches: [s390x, arm64, armhf, powerpc, ppc64el, riscv64]
    uri: https://mirrors.aliyun.com/ubuntu-ports/
    user-data:
    timezone: $timezone
    disable_root: false
    chpasswd:
    list: |
    root_user:root_password
    identity:
    realname: ubuntu user
    hostname: $ubunutu_hostname
    password: $ubuntu_password
    username: $ubunutu_user
    keyboard: {layout: us, variant: ''}
    locale: en_US.UTF-8
    network:
    version: 2
    ethernets:
    eth0:
    critical: true
    dhcp-identifier: mac
    dhcp4: true
    nameservers:
    addresses: [8.8.8.8,9.9.9.9]
    ssh:
    install-server: true

    storage:
      grub:
        reorder_uefi: False
      layout:
        name: lvm
        sizing-policy: all
    
    updates: security
    
    late-commands:
    - curtin in-target --target=/target -- sed -ir '/GRUB_CMDLINE_LINUX=.*$/c GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"' /etc/default/grub
    - curtin in-target --target=/target -- grub-mkconfig -o /boot/grub/grub.cfg
    - curtin in-target --target=/target -- sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    - curtin in-target --target=/target -- ln -sf bash /bin/sh 
    - curtin in-target --target=/target -- systemctl restart sshd 
    - curtin in-target --target=/target -- systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target 
    - curtin in-target --target=/target -- sed -i '$ a\export DEBIAN_FRONTEND=noninteractive' /etc/profile
    - curtin in-target --target=/target -- sed -i '$ a\export EDITOR=vim' /etc/profile
    

    EOF

    echo "copy user-data up ubuntu22.04 "
    cp -rf apache_catalogue/ubuntu24_catalogue/user-data apache_catalogue/ubuntu22_catalogue/

    echo "add meta-data up ubuntu24.04"
    cat <<'EOF'>>apache_catalogue/ubuntu24_catalogue/meta-data
    instance-id: focal-autoinstall
    EOF

    echo "cp meta-data up ubuntu22.04"
    cp -rf apache_catalogue/ubuntu24_catalogue/meta-data apache_catalogue/ubuntu22_catalogue/

    echo "shim-signed"
    cd /
    apt download shim-signed
    dpkg -x shim-signed*deb shim
    cp /shim/usr/lib/shim/shimx64.efi.signed.latest $tftp_catalogue/bootx64.efi

    echo "grub-efi-amd64-signed"
    cd /
    apt download grub-efi-amd64-signed
    dpkg -x grub-efi-amd64-signed*deb grub
    cp /grub/usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed $tftp_catalogue/grubx64.efi

    echo "grub-common"
    cd /
    apt download grub-common
    dpkg -x grub-common*deb grub-common
    cp /grub-common/usr/share/grub/unicode.pf2 $tftp_catalogue/

    echo "rm shim grub grub-common"
    rm -rf /{shim,grub,grub-common}
    rm -rf /{shim-signeddeb,grub-efi-amd64-signeddeb,grub-common*deb}

    echo "umount ubuntu22.04 ubuntu24.04"
    umount /mnt/ubuntu24_catalogue umount /mnt/ubuntu22_catalogue

    echo "restart tftpd-hpa isc-dhcp-server apache2"
    systemctl restart tftpd-hpa isc-dhcp-server apache2

     echo -e "\033[32mThe ubuntu pxe server Install Sussess...\033[0m" 
    else
     echo -e "\033[33mThe ubunutu pxe server Install Failed...\033[0m" 
      exit 1
     fi
    else
     echo -e "\033[31mThe ubuntu pxe server Install already...\033[0m"
    

    fi
    }

    main (){
    install_pxe_server
    }

    main

一键部署pxe server 环境

bash /ubuntu_pxe_server_bios_uefi.sh

pxe 网络启动,bios自动安装

pxe 网络启动,UEFI自动安装

ubuntu 22.04 24.04 bios uefi 自动化部署一样

  • 以下自动化部署展示 24.04
  • 24.04 root 首次登录密码强制更新,root预设值默认密码1234.com ubuntu/1234.com
  • 22.04 用户名密码 root/1234.com ubuntu/1234.com

自动化部署完成后在线源默认位置

相关推荐
矛取矛求2 小时前
Linux如何更优质调节系统性能
linux
内核程序员kevin3 小时前
在Linux环境下使用Docker打包和发布.NET程序并配合MySQL部署
linux·mysql·docker·.net
kayotin4 小时前
Wordpress博客配置2024
linux·mysql·docker
Ztiddler4 小时前
【Linux Shell命令-不定期更新】
linux·运维·服务器·ssh
小小不董5 小时前
Oracle OCP认证考试考点详解082系列16
linux·运维·服务器·数据库·oracle·dba
IPdodo全球网络5 小时前
如何利用静态住宅IP优化Facebook商城的网络稳定性与运营效率
运维·服务器
a1denzzz5 小时前
Linux系统的网络设置
linux·服务器·网络
运维&陈同学5 小时前
【模块一】kubernetes容器编排进阶实战之k8s基础概念
运维·docker·云原生·容器·kubernetes·云计算
ac.char6 小时前
在CentOS下安装RabbitMQ
linux·centos·rabbitmq
m0_519523106 小时前
Linux——简单认识vim、gcc以及make/Makefile
linux·运维·vim