• debian 12 PXE Server 批量部署系统


    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服务,以便客户端可以访问操作系统镜像和其他文件。

    debian 相关下载,文档参考

    • debian 基础系统部署,源参考
    debiandebian 历史版debian 发行版debian 安装debian preseed.cfg自动化编排debian dhcp 配置netboot 下载

    Debian 全球镜像站

    downloaddownload参考参考参考参考debian11debian12download

    创建一键安装tftp dhcp 实现批量部署debian

    • 实现自动化安装debian(bios+uefi),lvm 分区xfs
    • debian 构建内部镜像资源站,参考
      1. d-i mirror/http/hostname string 192.168.11.70 内网镜像挂载安装方式配置参考
      2. d-i mirror/http/directory string /debian12
      3. d-i mirror/http/proxy string
    • dhcp 配置文件位置/etc/dhcp/dhcpd.conf
    • tftp 配置文件/etc/default/tftpd-hpa
    • tftp 文件位置/var/lib/tftp
    • apache2 配置文件 /etc/apache2/apache2.conf
    • apache2 目录位置/var/www/html
    • debian11 netboot 位置/var/lib/tftp/debian11.10/debian-installer
    • debian12 netboot 位置/var/lib/tftp/debian12.6/debian-installer
    • 基于bios 启动配置文件/var/lib/tftp/pxelinux.cfg/default
    • 基于UEFI启动配置文件/var/lib/tftp/grub/grub.cfg
    • debian 自动化编排脚本位置/var/lib/tftp/preseed/debian-11-12-preseed.cfg (脚本兼容bios+uefi)
    • 执行以下脚本执行前 ln -sf bash /bin/sh
    • root/1234.com 以下自动化安装完成的用户密码
    • 18-45行变量,根据自己的实际情况进行变更
    • 使用crypt(3)哈希进行加密(apt install -y whois) (mkpasswd -m sha-512 生成密钥)
    • debian 11 12 实现bios+uefi
    vim /debian_pxe_server_bios_uefi_install.sh
    1. #!/bin/bash
    2. # -*- coding: utf-8 -*-
    3. # Author: make.han
    4. # Email: CIASM@CIASM
    5. # Date: 2024/07/12
    6. # install PXE Server debian 12
    7. # preseed.cfg 编排
    8. #https://www.debian.org/releases/stable/amd64/apbs04.zh-cn.html#ftn.idm3455
    9. # TFTP 网络引导准备文件
    10. #https://www.debian.org/releases/stable/amd64/ch04s05.zh-cn.html
    11. # 安装手册
    12. #https://www.debian.org/releases/stable
    13. #dhcp ip address
    14. nic_network_name=`ifconfig -s | awk 'NR>1 && !/^lo/ && !/^idrac/ && !/^br/ && !/^veth/ && !/^docker/{print $1; exit}'`
    15. host_IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2}' | awk 'NR==1'`
    16. IP="192.168.11.69"
    17. MASK="255.255.255.0"
    18. BROADCAST_ADDRESS="192.168.11.255"
    19. ROUTERS="192.168.11.1"
    20. SUBNET="192.168.11.0"
    21. DNS="8.8.8.8"
    22. RANGE="192.168.11.56 192.168.11.250"
    23. #tftp variable configuration
    24. tftp_port=69
    25. tftp_user=tftp
    26. tftp_catalogue=/var/lib/tftp
    27. #debian catalogue
    28. debian_11_catalogue=debian11.10
    29. debian_12_catalogue=debian12.6
    30. debian_iso=debian_iso
    31. debian_cfg=debian_cfg
    32. #netboot downlaod
    33. netboot_11_url_download=https://mirrors.ustc.edu.cn/debian/dists/Debian11.10/main/installer-amd64/current/images/netboot/netboot.tar.gz
    34. netboot_12_url_download=https://mirrors.ustc.edu.cn/debian/dists/Debian12.6/main/installer-amd64/current/images/netboot/netboot.tar.gz
    35. netboot_gz=netboot.tar.gz
    36. # root用户,普通用户 密码使用crypt(3)哈希进行加密(apt install -y whois) (mkpasswd -m sha-512 生成密钥)
    37. root_password='$6$5vShu8v/wRoByWOr$0uGqOl9W40u.hXXZwLBrP6jGFubcw.UM3JE13eOkdm7RsfcnseVsCe1YBR6VawPtFH4rNROi2sJ35X98dNO.C/'
    38. host_name=debian
    39. common_user=debian
    40. common_password='$6$5vShu8v/wRoByWOr$0uGqOl9W40u.hXXZwLBrP6jGFubcw.UM3JE13eOkdm7RsfcnseVsCe1YBR6VawPtFH4rNROi2sJ35X98dNO.C/'
    41. time_zone="Asia/Shanghai"
    42. mirror="ftp.cn.debian.org"
    43. # apache2 variable configuration
    44. apache_port=80
    45. apache_catalogue=/var/www/html
    46. #debian download
    47. debian12_download_url=https://cdimage.debian.org/debian-cd/12.6.0/amd64/iso-dvd/debian-12.6.0-amd64-DVD-1.iso
    48. debian11_download_url=https://cdimage.debian.org/cdimage/archive/11.10.0/amd64/iso-dvd/debian-11.10.0-amd64-DVD-1.iso
    49. debian12_iso=debian-12.6.0-amd64-DVD-1.iso
    50. debian11_iso=debian-11.10.0-amd64-DVD-1.iso
    51. install_pxe_server (){
    52. if ! [ -x "$(command -v dhcpd)" ]; then
    53. if [ $? -eq 0 ];then
    54. echo "install firewalld"
    55. apt install -y firewalld curl
    56. firewall-cmd --zone=public --add-port=$tftp_port/tcp --permanent && firewall-cmd --reload
    57. firewall-cmd --zone=public --add-port=$tftp_port/udp --permanent && firewall-cmd --reload
    58. echo "install whois mkpasswd"
    59. apt install -y whois
    60. echo "install dhcp"
    61. apt install -y isc-dhcp-server
    62. echo "isc-dhcp-server add nic"
    63. sed -i "s/^INTERFACESv4=.*$/INTERFACESv4=\"$nic_network_name\"/" /etc/default/isc-dhcp-server
    64. echo "configuration dhcpd.conf"
    65. rm -rf /etc/dhcp/dhcpd.conf
    66. cat <<EOF>>/etc/dhcp/dhcpd.conf
    67. option domain-name "$DNS";
    68. option domain-name-servers $DNS;
    69. default-lease-time 2592000;
    70. max-lease-time 2592000;
    71. authoritative;
    72. # add follows
    73. option space pxelinux;
    74. option pxelinux.magic code 208 = string;
    75. option pxelinux.configfile code 209 = text;
    76. option pxelinux.pathprefix code 210 = text;
    77. option pxelinux.reboottime code 211 = unsigned integer 32;
    78. option architecture-type code 93 = unsigned integer 16;
    79. subnet $SUBNET netmask $MASK {
    80. range dynamic-bootp $RANGE;
    81. option broadcast-address $BROADCAST_ADDRESS;
    82. option routers $ROUTERS;
    83. #add follows
    84. class "pxeclients" {
    85. match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
    86. # PXE servers hostname or IP address
    87. next-server $host_IP;
    88. if option architecture-type = 00:07 {
    89. filename "bootx64.efi";
    90. }
    91. else {
    92. filename "pxelinux.0";
    93. }
    94. }
    95. }
    96. EOF
    97. echo "install tftpd-hpa"
    98. apt install -y tftpd-hpa
    99. echo "configuration tftpd-hpa"
    100. rm -rf /etc/default/tftpd-hpa
    101. cat <<EOF>>/etc/default/tftpd-hpa
    102. TFTP_USERNAME="$tftp_user"
    103. TFTP_DIRECTORY="$tftp_catalogue/"
    104. TFTP_ADDRESS=":$tftp_port"
    105. TFTP_OPTIONS="--secure"
    106. EOF
    107. echo "Create a new tftp directory"
    108. mkdir -p $tftp_catalogue/{$debian_11_catalogue,$debian_12_catalogue}
    109. echo "start tftpd-hpa"
    110. systemctl start tftpd-hpa
    111. echo "install apache2"
    112. apt install -y apache2
    113. echo "Delete the default apache2 page"
    114. rm -rf $apache_catalogue/index.html
    115. echo "restart apache2"
    116. systemctl restart apache2
    117. echo "apache2 firewall"
    118. firewall-cmd --zone=public --add-port=$apache_port/tcp --permanent && firewall-cmd --reload
    119. # debian 11 12 Image download mount
    120. <<!
    121. echo "mkdir catalogue debian 11 12"
    122. mkdir -p $apache_catalogue/{$debian_11_catalogue,$debian_12_catalogue}
    123. echo "download debian 11 12"
    124. mkdir -p $apache_catalogue/$debian_iso
    125. curl -o $apache_catalogue/$debian_iso/$debian11_iso $debian11_download_url
    126. curl -o $apache_catalogue/$debian_iso/$debian12_iso $debian12_download_url
    127. echo "mount debian12_iso"
    128. mount -t iso9660 -o loop,ro $apache_catalogue/$debian_iso/$debian11_iso $apache_catalogue/$debian_11_catalogue
    129. mount -t iso9660 -o loop,ro $apache_catalogue/$debian_iso/$debian12_iso $apache_catalogue/$debian_12_catalogue
    130. echo "Mount an image on startup"
    131. sed -i "$ a $apache_catalogue/$debian_iso/$debian11_iso $apache_catalogue/$debian_11_catalogue/ iso9660 defaults,loop,ro 0 0" /etc/fstab
    132. sed -i "$ a $apache_catalogue/$debian_iso/$debian12_iso $apache_catalogue/$debian_12_catalogue/ iso9660 defaults,loop,ro 0 0" /etc/fstab
    133. !
    134. echo "downlaod netboot"
    135. curl -o $tftp_catalogue/$debian_11_catalogue/$netboot_gz $netboot_11_url_download
    136. curl -o $tftp_catalogue/$debian_12_catalogue/$netboot_gz $netboot_12_url_download
    137. echo "decompression netboot"
    138. tar -xf $tftp_catalogue/$debian_11_catalogue/$netboot_gz -C $tftp_catalogue/$debian_11_catalogue
    139. tar -xf $tftp_catalogue/$debian_12_catalogue/$netboot_gz -C $tftp_catalogue/$debian_12_catalogue
    140. echo "bios configuration default"
    141. mkdir -p $tftp_catalogue/pxelinux.cfg
    142. cat <<EOF | tee $tftp_catalogue/pxelinux.cfg/default
    143. default vesamenu.c32
    144. #自定义图片路径位置
    145. #menu background /debian.png
    146. menu hshift 13
    147. menu width 49
    148. menu margin 8
    149. menu tabmsg
    150. timeout 100
    151. menu title Installer boot menu
    152. label Auto Install Debian 11.10
    153. menu label ^Auto Install Debian 11.10
    154. menu default
    155. kernel $debian_11_catalogue/debian-installer/amd64/linux
    156. append auto=true priority=critical vga=788 initrd=$debian_11_catalogue/debian-installer/amd64/initrd.gz url=http://${host_IP}/$debian_cfg/debian-11-12-preseed.cfg
    157. menu end
    158. label Auto Install Debian 12.6
    159. menu label ^Auto Install Debian 12.6
    160. menu default
    161. kernel $debian_12_catalogue/debian-installer/amd64/linux
    162. append auto=true priority=critical vga=788 initrd=$debian_12_catalogue/debian-installer/amd64/initrd.gz url=http://${host_IP}/$debian_cfg/debian-11-12-preseed.cfg
    163. menu end
    164. label local
    165. #menu default
    166. com32 chain.c32
    167. menu label Boot from ^local drive
    168. localboot 0xffff
    169. menu end
    170. EOF
    171. echo "syslinux pxelinux"
    172. apt install -y syslinux pxelinux
    173. cp /usr/lib/syslinux/modules/bios/* $tftp_catalogue/
    174. #cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libutil.c32,menu.c32,vesamenu.c32} $tftp_catalogue/
    175. cp /usr/lib/PXELINUX/{lpxelinux.0,pxelinux.0} $tftp_catalogue/
    176. echo "shim-signed"
    177. cd /
    178. apt download shim-signed
    179. dpkg -x shim-signed*deb shim
    180. cp /shim/usr/lib/shim/shimx64.efi.signed $tftp_catalogue/bootx64.efi
    181. echo "grub-efi-amd64-signed"
    182. cd /
    183. apt download grub-efi-amd64-signed
    184. dpkg -x grub-efi-amd64-signed*deb grub
    185. cp /grub/usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed $tftp_catalogue/grubx64.efi
    186. echo "grub-common"
    187. cd /
    188. apt download grub-common
    189. dpkg -x grub-common*deb grub-common
    190. cp /grub-common/usr/share/grub/unicode.pf2 $tftp_catalogue/
    191. echo "rm shim grub grub-common"
    192. rm -rf /{shim,grub,grub-common}
    193. rm -rf /{shim-signed*deb,grub-efi-amd64-signed*deb,grub-common*deb}
    194. echo "uefi configuration grub.cfg"
    195. mkdir -p $tftp_catalogue/grub
    196. cat <<EOF>>$tftp_catalogue/grub/grub.cfg
    197. set default=4
    198. set timeout=10
    199. set gfxpayload=keep
    200. set menu_color_highlight=cyan/black
    201. set menu_color_normal=white/black
    202. set color_normal=white/black
    203. function load_video {
    204. insmod efi_gop
    205. insmod efi_uga
    206. insmod video_bochs
    207. insmod video_cirrus
    208. insmod all_video
    209. }
    210. load_video
    211. insmod gzio
    212. insmod part_msdos
    213. insmod part_gpt
    214. insmod ext2
    215. insmod xfs
    216. insmod png
    217. insmod gfxterm
    218. insmod gfxmenu
    219. terminal_output gfxterm
    220. background_image -m stretch bg.png
    221. menuentry 'EFI Firmware System Setup' 'uefi-firmware' {
    222. fwsetup
    223. }
    224. menuentry 'Reboot System' {
    225. reboot
    226. }
    227. menuentry 'Shutdown System' {
    228. halt
    229. }
    230. menuentry 'Auto Install debian 11.10' {
    231. linuxefi $debian_11_catalogue/debian-installer/amd64/linux ip=dhcp auto=true priority=critical vga=788 url=http://${host_IP}/$debian_cfg/debian-11-12-preseed.cfg
    232. initrdefi $debian_11_catalogue/debian-installer/amd64/initrd.gz
    233. }
    234. menuentry 'Auto Install debian 12.6' {
    235. linuxefi $debian_12_catalogue/debian-installer/amd64/linux ip=dhcp auto=true priority=critical vga=788 url=http://${host_IP}/$debian_cfg/debian-11-12-preseed.cfg
    236. initrdefi $debian_12_catalogue/debian-installer/amd64/initrd.gz
    237. }
    238. EOF
    239. echo "bios + uefi establish debian 11 12 cfg"
    240. mkdir -p $apache_catalogue/$debian_cfg
    241. cat <<EOF>>$apache_catalogue/$debian_cfg/debian-11-12-preseed.cfg
    242. # 地区设置语言、国家和地区
    243. d-i debian-installer/locale string en_US
    244. d-i debian-installer/language string en
    245. d-i debian-installer/country string CN
    246. d-i debian-installer/locale string en_GB.UTF-8
    247. d-i localechooser/supported-locales multiselect en_US.UTF-8, zh_CN.UTF-8
    248. # 配置键盘
    249. d-i keyboard-configuration/xkb-keymap select us
    250. # 配置自动配置网络DHCP
    251. d-i netcfg/choose_interface select auto
    252. # 配置hostname和domain
    253. d-i netcfg/get_hostname string $host_name
    254. d-i netcfg/get_domain string $host_name
    255. # 指定软件包镜像源的设置
    256. d-i mirror/country string manual
    257. d-i mirror/protocol string http
    258. d-i mirror/http/hostname string $mirror
    259. d-i mirror/http/directory string /debian
    260. d-i mirror/http/proxy string
    261. # 内网软件包镜像源的设置
    262. #d-i mirror/http/hostname string 192.168.11.70
    263. #d-i mirror/http/directory string /debian12
    264. #d-i mirror/http/proxy string
    265. # 选择软件包,标准系统实用程序
    266. tasksel tasksel/first multiselect standard ssh-server
    267. #开启root登录并设置root密码,关闭普通用户创建
    268. d-i passwd/root-login boolean true
    269. d-i passwd/make-user boolean false
    270. # Root密码,可以是明文
    271. #d-i passwd/root-password password $root_password
    272. #d-i passwd/root-password-again password $root_password
    273. # root用户密码使用crypt(3)哈希进行加密(apt install -y whois) (mkpasswd -m sha-512 生成密钥)
    274. d-i passwd/root-password-crypted password $root_password
    275. # 普通用户创建
    276. #d-i passwd/make-user boolean true
    277. #d-i passwd/user-fullname string Debian User
    278. #d-i passwd/username string $common_user
    279. # 普通用户密码,可以是明文
    280. #d-i passwd/user-password password $common_password
    281. #d-i passwd/user-password-again password $common_password
    282. # 普通用户密码使用crypt(3)哈希进行加密(apt install -y whois) (mkpasswd -m sha-512 生成密钥)
    283. #d-i passwd/user-password-crypted password $common_password
    284. #允许弱密码在用户账户创建时被接受
    285. d-i user-setup/allow-password-weak boolean true
    286. # 时钟与时区设置
    287. d-i clock-setup/utc boolean true
    288. d-i time/zone string $time_zone
    289. d-i clock-setup/ntp boolean false
    290. #d-i clock-setup/ntp-server string ntp.example.com
    291. # uefi引导,强制使用gpt分区表
    292. d-i partman-efi/non_efi_system boolean true
    293. d-i partman-partitioning/choose_label string gpt
    294. d-i partman-partitioning/default_label string gpt
    295. # 配置磁盘 LVM xfs
    296. d-i partman-auto/method string lvm
    297. d-i partman-auto/disk string /dev/sda
    298. d-i partman-lvm/device_remove_lvm boolean true
    299. d-i partman-lvm/confirm boolean true
    300. d-i partman-lvm/confirm_nooverwrite boolean true
    301. d-i partman-auto/choose_recipe select atomic
    302. d-i partman/default_filesystem string xfs
    303. # partman在没有确认的情况下自动分区
    304. d-i partman-partitioning/confirm_write_new_label boolean true
    305. d-i partman/choose_partition select finish
    306. d-i partman/confirm boolean true
    307. d-i partman/confirm_nooverwrite boolean true
    308. # 基本系统安装
    309. #d-i base-installer/kernel/p_w_picpath string linux-server
    310. #设置apt
    311. #d-i apt-setup/security_host string mirrors.aliyun.com
    312. #d-i apt-setup/security_path string /ubuntu
    313. #d-i debian-installer/allow_unauthenticated string false
    314. #d-i pkgsel/upgrade select safe-upgrade
    315. #d-i pkgsel/language-packs multiselect
    316. #d-i pkgsel/update-policy select none
    317. #d-i pkgsel/updatedb boolean trueb
    318. # 禁止在安装的时候弹出CD/DVD扫描提示
    319. d-i apt-setup/non-free boolean true
    320. d-i apt-setup/contrib boolean true
    321. d-i apt-setup/cdrom/set-first boolean false
    322. d-i apt-setup/cdrom/set-next boolean false
    323. d-i apt-setup/cdrom/set-failed boolean false
    324. # 安装额外的软件包,不更新系统
    325. d-i pkgsel/include string openssh-server vim vim-tiny sudo whois git firewalld curl
    326. d-i pkgsel/upgrade select none
    327. # grub安装
    328. d-i grub-installer/only_debian boolean true
    329. d-i grub-installer/with_other_os boolean true
    330. d-i grub-installer/bootdev string default
    331. d-i grub-installer/skip boolean false
    332. d-i grub-installer/bootdev string /dev/sda
    333. #d-i lilo-installer/skip boolean false
    334. # 安装完成之后不要弹出安装完成的界面,直接重启
    335. d-i finish-install/reboot_in_progress note
    336. # 允许ssh服务使用root用户登录
    337. d-i preseed/late_command string in-target sed -i '$ a\PermitRootLogin yes' /etc/ssh/sshd_config
    338. EOF
    339. echo "enable tftp dhcp"
    340. systemctl enable tftpd-hpa isc-dhcp-server apache2
    341. echo "restart tftpd-hpa isc-dhcp-server"
    342. systemctl restart tftpd-hpa isc-dhcp-server apache2
    343. echo -e "\033[32mThe pxe server Install Sussess...\033[0m"
    344. else
    345. echo -e "\033[33mThe pxe server Install Failed...\033[0m"
    346. exit 1
    347. fi
    348. else
    349. echo -e "\033[31mThe pxe server Install already...\033[0m"
    350. fi
    351. }
    352. main (){
    353. install_pxe_server
    354. }
    355. main

    执行一键安装

     bash /debian_pxe_server_bios_uefi_install.sh

    pxe 网络启动,bios

    pxe 网络启动,UEFI

    自动化安装(支持debian11 12)bios+uefi

    安装完成,查看

  • 相关阅读:
    四种强大且隐秘的缓存
    【元宇宙欧米说】数藏的社交与艺术收藏价值探讨
    Springboot楼盘在线销售平台7txdy计算机毕业设计-课程设计-期末作业-毕设程序代做
    大学排行榜 : qs全球世界 大学排行榜
    Vivado IP中Generate Output Products的设置说明
    成为会带团队的技术人 架构设计:治理好系统复杂度才最务实
    10-09 周一 图解机器学习之深度学习感知机学习
    SpringBoot如何避免SQL注入漏洞呢?
    HarmonyOS/OpenHarmony原生应用开发-华为Serverless认证服务说明(二)
    6.Tomcat概述与部署
  • 原文地址:https://blog.csdn.net/hanzheng260561728/article/details/140377556