```sh # 设置变量 alias cp='cp' centos7_iso='https://mirrors.tuna.tsinghua.edu.cn/centos/7.9.2009/os/x86_64/' ubuntu_iso'https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/22.04/ubuntu-22.04.2-live-server-amd64.iso' dhip=$(ip addr |grep inet -w|awk -F' ' '{print $2}'|sed -n '2p' |awk -F'/' '{print $1}') dhipNet=$(echo ${dhip:0:-$(expr length `echo $dhip |cut -d '.' -f4`)}) dhipStart=${dhipNet}100 dhipEnd=${dhipNet}200 # 关闭selinux echo '- 关闭selinux' setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 关闭防火墙 echo '- 关闭防火墙' systemctl stop firewalld # 安装软件 echo -e '开始安装tftp/xinetd/vsftpd/syslinux...\n' yum install tftp-server xinetd vsftpd syslinux-4.05 dhcp-4.2.5 -y > /dev/null if [ ! -e /var/ftp/centos7 ] then mkdir -p /var/ftp/centos7 elif [ ! -e /mnt/centos7 ] then mkdir -p /mnt/centos7 fi mount -o loop /dev/sr0 /mnt/centos7 if [[ $? -eq 0 || -s /mnt/centos7/isolinux ]] then echo '拷贝镜像文件到ftp目录下' cp -rf /mnt/centos7/* /var/ftp/centos7/ else echo -e '未挂载iso文件,将使用华为镜像站镜像!\n' # 在华为镜像站下载启动文件,也可以用本地iso镜像里的文件,拷贝到/var/lib/tftpboot目录即可 echo '开始下载初始化文件' wget -P /var/lib/tftpboot/ --no-check-certificate ${centos7_iso}images/pxeboot/initrd.img echo '开始下载内核文件' wget -P /var/lib/tftpboot/ --no-check-certificate ${centos7_iso}images/pxeboot/vmlinuz fi sed -i 's/yes/no/g' /etc/xinetd.d/tftp # grep -v "^#" /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example |grep -v "^$"| grep -v "opt*" |head -n5 # 配置dhcp cat > /etc/dhcp/dhcpd.conf << EOF log-facility local7; ignore client-updates; # 创建地址池 subnet ${dhipNet}0 netmask 255.255.255.0{ range ${dhipStart} ${dhipEnd}; #地址池 option routers ${dhip}; # 网关地址 ddns-update-style none; # 禁止ddns动态更新 next-server ${dhip}; # 服务器地址 filename "pxelinux.0"; # 指定pxe引导文件 default-lease-time 600; max-lease-time 7200; } EOF # 拷贝引导程序 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/ # 配置引导文件 cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF #UI menu.c32 default centos7 local prompt 1 timeout 5 label centos7 local kernel vmlinuz append initrd=initrd.img method=ftp://${dhip}/centos7 label centos7 net kernel vmlinuz append initrd=initrd.img method=${centos7_iso} # ks=ftp EOF # 设置开机自启,并重新启动 systemctl enable --now tftp vsftpd dhcpd xinetd systemctl restart tftp vsftpd dhcpd xinetd if [ $? -eq 0 ] then echo 'pxe引导服务搭建成功!' fi ``` ## 无人值守引导配置 ```sh #platform=x86, AMD64, 或 Intel EM64T #version=DEVEL # Install OS instead of upgrade install # Keyboard layouts keyboard 'us' # Root password rootpw --iscrypted $1$RrxYwvA3$RQQcS4Mas0Ef.TqS0Ptot/ # System language lang en_US.UTF-8 --addsupport=zh_CN.UTF-8 # System authorization information auth --useshadow --passalgo=sha512 # Use text mode install text # SELinux configuration selinux --disabled # Do not configure the X Window System skipx # Firewall configuration firewall --disabled # Network information network --bootproto=dhcp --device=eth0 # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Use network installation url --url="http://https://repo.huaweicloud.com/centos/7.9.2009/os/x86_64/" # url --url=ftp://:@/ #platform=x86, AMD64, 或 Intel EM64T #version=DEVEL # Install OS instead of upgrade install # Keyboard layouts keyboard 'us' # Root password rootpw --iscrypted $1$RrxYwvA3$RQQcS4Mas0Ef.TqS0Ptot/ # System language lang en_US.UTF-8 --addsupport=zh_CN.UTF-8 # System authorization information auth --useshadow --passalgo=sha512 # Use text mode install text # SELinux configuration selinux --disabled # Do not configure the X Window System skipx # Firewall configuration firewall --disabled # Network information network --bootproto=dhcp --device=eth0 # Reboot after installation reboot # System timezone timezone Asia/Shanghai # Use network installation #url="http://https://repo.huaweicloud.com/centos/7.9.2009/os/x86_64/" url --url=ftp://192.168.225.133/centos7 # System bootloader configuration bootloader --location=mbr # Clear the Master Boot Record zerombr # Partition clearing information ignoredisk --only-use=sda clearpart --all --initlabel # Disk partitioning information part /boot --fstype="xfs" --asprimary --size=2048 part / --fstype="xfs" --grow --asprimary --size=1 services --enabled="sshd,network" # 安装软件包 %packages network-tools openssh openssl-devel wget %end # 系统安装前运行程序 %pre --interpreter=/bin/bash echo 'centos7 install' %end # 系统安装后运行程序 %post --interpreter=/bin/bash wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7- reg.repo %end ```