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 基礎系統部署,源參考
debian | debian 歷史版 | debian 發行版 | debian 安裝 | debian preseed.cfg自動化編排 | debian dhcp 配置 | netboot 下載 | Debian 全球鏡像站 |
download | download | 參考 | 參考 | 參考 | 參考 | download | download |
創建一鍵安裝tftp dhcp 實現批量部署debian
- 實現自動化安裝debian,lvm 分區,xfs?
- debian 自動化部署需要連接互聯網,原因需要你建立一個 Debian 內網倉庫的鏡像,耗時較長,文件較大,根據自身情況進行選擇,參考
- dhcp 配置文件位置/etc/dhcp/dhcpd.conf
- tftp 配置文件/etc/default/tftpd-hpa
- tftp 文件位置/srv/tftp/
- netboot 位置/srv/tftp/debian-installer
- 使用crypt(3)哈希進行加密(apt install -y whois) (mkpasswd -m sha-512 生成密鑰)
- 基于bios 啟動配置文件/srv/tftp/debian-installer/amd64/pxelinux.cfg/default
- debian 自動化編排腳本位置/srv/tftp/preseed/debian-12-preseed.cfg
- 執行以下腳本執行前 ln -sf bash /bin/sh
- root/1234.com 以下自動化安裝完成的用戶密碼
- 18-35行變量,根據自己的實際情況進行變更
- 目前實現bios,uefi目前沒有實現,后續實現后更新
vim /pxe_server_install.sh
#!/bin/bash
# -*- coding: utf-8 -*-
# Author: make.han
# Email: CIASM@CIASM
# Date: 2024/07/12
# install PXE Server debian 12# preseed.cfg 編排
#https://www.debian.org/releases/stable/amd64/apbs04.zh-cn.html#ftn.idm3455# TFTP 網絡引導準備文件
#https://www.debian.org/releases/stable/amd64/ch04s05.zh-cn.html# 安裝手冊
#https://www.debian.org/releases/stable#dhcp ip address
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.69"
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.56 192.168.11.250"# root用戶,普通用戶 密碼使用crypt(3)哈希進行加密(apt install -y whois) (mkpasswd -m sha-512 生成密鑰)
root_password='$6$5vShu8v/wRoByWOr$0uGqOl9W40u.hXXZwLBrP6jGFubcw.UM3JE13eOkdm7RsfcnseVsCe1YBR6VawPtFH4rNROi2sJ35X98dNO.C/'
host_name=debian
common_user=debian
common_password='$6$5vShu8v/wRoByWOr$0uGqOl9W40u.hXXZwLBrP6jGFubcw.UM3JE13eOkdm7RsfcnseVsCe1YBR6VawPtFH4rNROi2sJ35X98dNO.C/'
time_zone="Asia/Shanghai"
mirror="ftp.cn.debian.org"
tftp_port=69install_pxe_server (){if ! [ -x "$(command -v dhcpd)" ]; thenif [ $? -eq 0 ];thenecho "install firewalld"
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 <<EOF>>/etc/dhcp/dhcpd.conf
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 "debian-installer/amd64/bootnetx64.efi";}else {filename "pxelinux.0";}}
}
EOFecho "install tftpd-hpa"
apt install -y tftpd-hpaecho "configuration tftpd-hpa"
echo > /etc/default/tftpd-hpa
cat <<EOF>>/etc/default/tftpd-hpa
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/srv/tftp/"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -c -s"
EOFecho "start tftpd-hpa"
systemctl enable tftpd-hpa
systemctl start tftpd-hpaecho "downlaod netboot"
curl -o /srv/tftp/netboot.tar.gz http://mirrors.ustc.edu.cn/debian/dists/bookworm/main/installer-amd64/current/images/netboot/netboot.tar.gz
tar -xf /srv/tftp/netboot.tar.gz -C /srv/tftpecho "configuration default"
rm -rf /srv/tftp/debian-installer/amd64/pxelinux.cfg/default
cat <<EOF | tee /srv/tftp/debian-installer/amd64/pxelinux.cfg/default
path debian-installer/amd64/boot-screens
default debian-installer/amd64/boot-screens/vesamenu.c32#自定義圖片路徑位置
#menu background /debian.pngmenu hshift 13
menu width 49
menu margin 8
menu tabmsg
timeout 100menu title Installer boot menu
label Auto Install Debian 12menu label ^Auto Install Debian 12menu defaultkernel debian-installer/amd64/linuxappend auto=true priority=critical vga=788 initrd=debian-installer/amd64/initrd.gz preseed/url=tftp://${host_IP}/preseed/debian-12-preseed.cfg
menu endlabel local#menu defaultcom32 chain.c32menu label Boot from ^local drivelocalboot 0xffff
menu end
EOFecho "create directory preseed"
mkdir -p /srv/tftp/preseedecho "establish debian-12-preseed.cfg"
cat <<EOF>>/srv/tftp/preseed/debian-12-preseed.cfg
# 地區設置語言、國家和地區
d-i debian-installer/locale string en_US
d-i debian-installer/language string en
d-i debian-installer/country string CN
d-i debian-installer/locale string en_GB.UTF-8
d-i localechooser/supported-locales multiselect en_US.UTF-8, zh_CN.UTF-8# 配置鍵盤
d-i keyboard-configuration/xkb-keymap select us# 配置自動配置網絡DHCP
d-i netcfg/choose_interface select auto# 配置hostname和domain
d-i netcfg/get_hostname string $host_name
d-i netcfg/get_domain string $host_name# 指定軟件包鏡像源的設置
d-i mirror/country string manual
#d-i mirror/protocol string http
d-i mirror/http/hostname string $mirror
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string# 選擇軟件包,標準系統實用程序
tasksel tasksel/first multiselect standard ssh-server #開啟root登錄并設置root密碼,關閉普通用戶創建
d-i passwd/root-login boolean true
d-i passwd/make-user boolean false# Root密碼,可以是明文
#d-i passwd/root-password password $root_password
#d-i passwd/root-password-again password $root_password# root用戶密碼使用crypt(3)哈希進行加密(apt install -y whois) (mkpasswd -m sha-512 生成密鑰)
d-i passwd/root-password-crypted password $root_password# 普通用戶創建
#d-i passwd/make-user boolean true
#d-i passwd/user-fullname string Debian User
#d-i passwd/username string $common_user# 普通用戶密碼,可以是明文
#d-i passwd/user-password password $common_password
#d-i passwd/user-password-again password $common_password# 普通用戶密碼使用crypt(3)哈希進行加密(apt install -y whois) (mkpasswd -m sha-512 生成密鑰)
#d-i passwd/user-password-crypted password $common_password#允許弱密碼在用戶賬戶創建時被接受
d-i user-setup/allow-password-weak boolean true# 時鐘與時區設置
d-i clock-setup/utc boolean true
d-i time/zone string $time_zone
d-i clock-setup/ntp boolean false
#d-i clock-setup/ntp-server string ntp.example.com# uefi引導,強制使用gpt分區表
#d-i partman-efi/non_efi_system boolean true
#d-i partman-partitioning/choose_label string gpt
#d-i partman-partitioning/default_label string gpt# 配置磁盤 LVM xfs
d-i partman-auto/method string lvm
d-i partman-auto/disk string /dev/sda
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman/default_filesystem string xfs# partman在沒有確認的情況下自動分區
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true# 基本系統安裝
#d-i base-installer/kernel/p_w_picpath string linux-server#設置apt
#d-i apt-setup/security_host string mirrors.aliyun.com
#d-i apt-setup/security_path string /ubuntu
#d-i debian-installer/allow_unauthenticated string false
#d-i pkgsel/upgrade select safe-upgrade
#d-i pkgsel/language-packs multiselect
#d-i pkgsel/update-policy select none
#d-i pkgsel/updatedb boolean trueb# 禁止在安裝的時候彈出CD/DVD掃描提示
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
d-i apt-setup/cdrom/set-first boolean false
d-i apt-setup/cdrom/set-next boolean false
d-i apt-setup/cdrom/set-failed boolean false# 安裝額外的軟件包,不更新系統
d-i pkgsel/include string openssh-server vim vim-tiny sudo whois git firewalld curl
d-i pkgsel/upgrade select none# grub安裝
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i grub-installer/bootdev string default
d-i grub-installer/skip boolean false
d-i grub-installer/bootdev string /dev/sda
#d-i lilo-installer/skip boolean false# 安裝完成之后不要彈出安裝完成的界面,直接重啟
d-i finish-install/reboot_in_progress note# 允許ssh服務使用root用戶登錄
d-i preseed/late_command string in-target sed -i 's/#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
EOFecho "restart dhcp"
systemctl enable isc-dhcp-serverecho "restart tftpd-hpa isc-dhcp-server"
systemctl restart tftpd-hpa isc-dhcp-serverecho -e "\033[32mThe pxe server Install Sussess...\033[0m" elseecho -e "\033[33mThe pxe server Install Failed...\033[0m" exit 1fielseecho -e "\033[31mThe pxe server Install already...\033[0m"
fi
}main (){install_pxe_server
}main
執行安裝
bash /pxe_server_install.sh