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 生命周期注意
ubuntu amd | ubuntu 其它 | ubuntu 历史版 | ubuntu 生命周期 | ubuntu 自动化编排user-data | debian dhcp 配置 | ubuntu 全球镜像站 |
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.04install_pxe_server (){if ! [ -x "$(command -v dhcpd)" ]; thenif [ $? -eq 0 ];thenecho "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 --reloadecho "install whois mkpasswd"
apt install -y whoisecho "install dhcp"
apt install -y isc-dhcp-serverecho "isc-dhcp-server add nic"
sed -i "s/^INTERFACESv4=.*$/INTERFACESv4=\"$nic_network_name\"/" /etc/default/isc-dhcp-serverecho "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 followsclass "pxeclients" {match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";# PXE servers hostname or IP addressnext-server $IP;if option architecture-type = 00:07 {filename "bootx64.efi";}else {filename "pxelinux.0";}}
}
EOFecho "restart dhcp"
systemctl enable isc-dhcp-server
systemctl restart isc-dhcp-serverecho "install tftpd-hpa"
apt install -y tftpd-hpaecho "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"
EOFecho "Create a new tftp directory"
mkdir -p $tftp_catalogueecho "start tftpd-hpa"
systemctl enable tftpd-hpa
systemctl start tftpd-hpaecho "install apache2"
apt install -y apache2echo "Delete the default apache2 page"
rm -rf $apache_catalogue/index.htmlecho "restart apache2"
systemctl enable apache2
systemctl restart apache2echo "apache2 firewall"
firewall-cmd --zone=public --add-port=$apache_port/tcp --permanent && firewall-cmd --reloadecho "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_urlecho "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_catalogueecho "syslinux pxelinux"
apt install -y syslinux pxelinuxecho "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_catalogueecho "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 <<EOF>>$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.pngmenu title ########## PXE Boot Menu #########
display boot.msglabel Auto Install ubuntu 22.04menuentry ^Auto Install ubuntu22.04menu defaultkernel $ubuntu22_catalogue/vmlinuzinitrd $ubuntu22_catalogue/initrdappend 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.04menuentry ^Auto Install ubuntu24.04menu defaultkernel $ubuntu24_catalogue/vmlinuzinitrd $ubuntu24_catalogue/initrdappend ip=dhcp url=http://${host_IP}/$ubuntu24_catalogue/$ubuntu24_iso autoinstall ds=nocloud-net;s=http://${host_IP}/$ubuntu24_catalogue/label Manual ubuntu 22.04menu label ^Manual Install ubuntu 22.04kernel $ubuntu22_catalogue/vmlinuzinitrd $ubuntu22_catalogue/initrdappend ip=dhcp url=http://${host_IP}/$ubuntu22_catalogue/$ubuntu22_isolabel Manual ubuntu 24.04menu label ^Manual Install ubuntu 24.04kernel $ubuntu24_catalogue/vmlinuzinitrd $ubuntu24_catalogue/initrdappend ip=dhcp url=http://${host_IP}/$ubuntu24_catalogue/$ubuntu24_isolabel local#menu defaultcom32 chain.c32menu label Boot from ^local drivelocalboot 0xffff
menu end
EOFecho "creation grub.cfg"
mkdir -p $tftp_catalogue/grub
cat <<EOF>>$tftp_catalogue/grub/grub.cfg
# ubuntu 24
set default="3"set timeout=10
set gfxpayload=keep
set color_normal=white/blackfunction load_video {insmod video_bochsinsmod video_cirrusinsmod 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.pngmenuentry '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_isoinitrd $ubuntu22_catalogue/initrd
}menuentry 'Manual Install ubuntu 24.04' {linux $ubuntu24_catalogue/vmlinuz ip=dhcp url=http://${host_IP}/$ubuntu24_catalogue/$ubuntu24_isoinitrd $ubuntu24_catalogue/initrd
}
EOFecho "add ubuntu 24.04 user-data"
cat <<EOF>>$apache_catalogue/$ubuntu24_catalogue/user-data
#cloud-config
autoinstall:version: 1apt: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: $timezonedisable_root: falsechpasswd:list: |$root_user:$root_passwordidentity:realname: ubuntu userhostname: $ubunutu_hostnamepassword: $ubuntu_passwordusername: $ubunutu_userkeyboard: {layout: us, variant: ''}locale: en_US.UTF-8network:version: 2ethernets:eth0:critical: truedhcp-identifier: macdhcp4: truenameservers:addresses: [8.8.8.8,9.9.9.9]ssh:install-server: truestorage:grub:reorder_uefi: Falselayout:name: lvmsizing-policy: allupdates: securitylate-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
EOFecho "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
EOFecho "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.efiecho "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.efiecho "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-signed*deb,grub-efi-amd64-signed*deb,grub-common*deb}echo "umount ubuntu22.04 ubuntu24.04"
umount /mnt/$ubuntu24_catalogue
umount /mnt/$ubuntu22_catalogueecho "restart tftpd-hpa isc-dhcp-server apache2"
systemctl restart tftpd-hpa isc-dhcp-server apache2echo -e "\033[32mThe ubuntu pxe server Install Sussess...\033[0m" elseecho -e "\033[33mThe ubunutu pxe server Install Failed...\033[0m" exit 1fielseecho -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