centos7.0 pxe安装

当然可以!以下是一个简单的 CentOS 7 上安装 PXE(Preboot eXecution Environment)服务器的教程。PXE 服务器允许远程计算机通过网络引导安装操作系统或执行其他系统维护任务。

注意:在开始之前,请确保您拥有一台运行 CentOS 7 的计算机,并且具有 root 或具有 sudo 权限的用户帐户。

步骤 1:安装必要的软件

首先,您需要安装所需的软件包,包括 DHCP 服务器、TFTP 服务器和HTTP服务器(用于存储安装文件):

sudo yum install -y dhcp tftp tftp-server httpd  xinetd

步骤 2:配置 DHCP 服务器

配置服务器网卡
[lifei@bih-network_bj_jump_1 ~]$ cat /etc/sysconfig/network-scripts/ifcfg-ens224
TYPE=Ethernet
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.10
NETMASK=255.255.255.0
BOOTPROTO=none
ZONE=public

编辑 DHCP 配置文件 /etc/dhcp/dhcpd.conf

sudo nano /etc/dhcp/dhcpd.conf

将以下内容添加到文件的底部(根据您的网络配置进行适当修改):

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.100 192.168.1.200;
    option domain-name-servers 8.8.8.8;
    option routers 192.168.1.1;
    option broadcast-address 192.168.1.255;
    default-lease-time 600;
    max-lease-time 7200;
    next-server 192.168.1.10; # 指定 PXE 服务器的 IP 地址
    filename "pxelinux.0";
}

保存并关闭文件。接下来,编辑 /etc/sysconfig/dhcpd 文件,确保 DHCP 服务器在启动时启动并监听正确的接口。将以下行添加到该文件中:

DHCPDARGS="eth0"; # 根据您的网络接口进行修改

保存并关闭文件。

步骤 3:配置 TFTP 服务器

编辑 TFTP 配置文件 /etc/xinetd.d/tftp

sudo nano /etc/xinetd.d/tftp

确保文件的内容如下:

service tftp
{
    socket_type             = dgram
    protocol                = udp
    wait                    = yes
    user                    = root
    server                  = /usr/sbin/in.tftpd
    server_args             = -s /var/lib/tftpboot
    disable                 = no
    per_source              = 11
    cps                     = 100 2
    flags                   = IPv4
}

保存并关闭文件。然后,创建 TFTP 根目录并设置权限:

sudo mkdir /var/lib/tftpboot
sudo chmod -R 777 /var/lib/tftpboot

步骤 4:准备安装文件

在您的 PXE 服务器上创建一个目录来存储 CentOS 7 安装文件:

sudo mkdir -p /var/www/html/centos7
sudo mount -o loop 挂载镜像 /mnt
sudo cp -r /mnt/* /var/www/html/centos7/
sudo umount /mnt

步骤 5:配置 PXE 引导文件

下载并解压 PXE 引导文件(Syslinux):

sudo yum install -y syslinux
cd /var/lib/tftpboot
sudo cp /usr/share/syslinux/pxelinux.0 .
sudo mkdir pxelinux.cfg

cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/
cp /var/www/html/centos7/isolinux/initrd.img /var/lib/tftpboot/
cp /var/www/html/centos7/isolinux/vmlinuz /var/lib/tftpboot/ 

/var/lib/tftpboot/pxelinux.cfg 目录下创建一个名为 default 的文件,内容如下:

cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu.c32
MENU TITLE PXE Menu
TIMEOUT 300
TOTALTIMEOUT 9000
ONTIMEOUT local

LABEL local
  MENU LABEL Boot from local drive
  MENU DEFAULT
  LOCALBOOT 0

LABEL centos7
  MENU LABEL Install CentOS 7
  KERNEL vmlinuz
  APPEND initrd=initrd.img inst.repo=http://192.168.1.10/centos7/

步骤 6:启动和配置服务

启动并启用 DHCP、TFTP 和 HTTP 服务器:

sudo systemctl start dhcpd
sudo systemctl enable dhcpd
sudo systemctl start xinetd
sudo systemctl enable xinetd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl start tftp
sudo systemctl enable tftp

步骤 7:防火墙配置

如果您的防火墙启用,需要配置防火墙以允许 DHCP、TFTP 和 HTTP 流量:

sudo firewall-cmd --permanent --add-service=dhcp
sudo firewall-cmd --permanent --add-service=tftp
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

步骤 8:完成设置

现在,您的 PXE 服务器已经准备好了。可以将 PXE 启动的计算机连接到网络并设置其引导顺序以从网络引导。它们将自动获取 IP 地址并从 PXE 服务器启动 CentOS 7 安装程序。

请注意,此教程仅提供了一个简单的 PXE 配置示例。根据您的网络和需求,可能需要进行更多的定制。希望这个教程能帮助您开始设置 PXE 服务器!

补充

下面是对应的指定ks文件,及ks文件的案例,只供参考

[root@icdn-pxe-server ~]# cat /var/lib/tftpboot/pxelinux.cfg/default 
DEFAULT menu.c32
MENU TITLE PXE Menu
TIMEOUT 300
TOTALTIMEOUT 9000
ONTIMEOUT local

LABEL local
  MENU LABEL Boot from local drive
  MENU DEFAULT
  LOCALBOOT 0


LABEL Install CentOS 7 via VNC & KS BIOS_net_save
  MENU LABEL Install CentOS 7 via VNC & KS BIOS_net_save
  KERNEL vmlinuz
  APPEND initrd=initrd.img inst.repo=http://192.168.9.1/centos7/ inst.ks=http://192.168.9.1/lifei-ks_net.cfg 


LABEL Install CentOS 7 via VNC & KS BIOS
  MENU LABEL Install CentOS 7 via VNC & KS BIOS
  KERNEL vmlinuz
  APPEND initrd=initrd.img inst.repo=http://192.168.9.1/centos7/ inst.ks=http://192.168.9.1/lifei-ks.cfg

LABEL centos7

  MENU LABEL Install CentOS 7
  KERNEL vmlinuz
  APPEND initrd=initrd.img inst.repo=http://192.168.9.1/centos7/
[root@icdn-pxe-server ~]# cat    /var/www/html/lifei-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
# Use graphical install
graphical
#cmdline
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
#ignoredisk --only-use=nvme0n1
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

#VNC
#vnc --host=122.11.173.11 --port=8948
#vnc --host=zhb456.asuscomm.com --port=8948
#vnc --host=195.133.135.238 --port=8948 #taifong

# Network information
network  --bootproto=dhcp --device=eno1 --ipv6=ignore --activate --nameserver=8.8.8.8,1.1.1.1
network  --hostname=icdn-xx.icdn.plus

# Root password
rootpw --iscrypted $6$CMXG.KolO7uh5vTk$jLP2o5OIzdbtsTrqolXgGPFN8VJh99EzZ4236QgFtDlYDjg9Jx5kEyk8P8J1gsmuiV1D6bcOvfMqREdCs61eC0

# Disable SELinux
selinux --disabled

# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Singapore --isUtc

# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
#bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=nvme0n1
# Partition clearing information
clearpart --all --initlabel --drives=sda
#clearpart --all --initlabel --drives=nvme0n1

# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=1024
part /boot/efi --fstype="efi" --ondisk=sda --size=200 --fsoptions="umask=0077,shortname=winnt"
part / --fstype="xfs" --ondisk=sda --size=109440
#part / --fstype="xfs" --ondisk=sda --size=1 --grow



%packages
@^minimal
@core
chrony
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

%post --log=/root/ks_post.log

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org  
yum install -y https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
yum install -y --enablerepo=elrepo-kernel kernel-lt

grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg    


yum install epel-release -y

yum -y groupinstall Base
yum -y install hddtemp oniguruma jq nload lshw hdparm dmidecode iotop dhcp dhcp-server tree nc lm_sensors miniupnpc ppp screen

echo "myintip=\$(ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ \t]*//g' | cut -d ' ' -f2 | tr '\n' ',')">/etc/rc.d/init.d/icdngreeting
echo "mygateway=\$(ip route show|grep 'default'|cut -d ' ' -f3)">>/etc/rc.d/init.d/icdngreeting
echo "myhost=\$(hostname)">>/etc/rc.d/init.d/icdngreeting
echo "mynickname=\$(cat /etc/init.d/icdnnickname)">>/etc/rc.d/init.d/icdngreeting
echo "dmidecode -t 1 |grep -m1 \"Product Name\" |cut -d \":\" -f 2 > /dev/shm/pcmodel">>/etc/rc.d/init.d/icdngreeting
echo "cat /proc/cpuinfo |grep -m1 \"model name\"|cut -d \":\" -f 2  > /dev/shm/cpumodel">>/etc/rc.d/init.d/icdngreeting
echo "mypcinfo=\$(cat /dev/shm/cpumodel)' '\$(cat /dev/shm/pcmodel)">>/etc/rc.d/init.d/icdngreeting
echo "icdnid=\$(awk 'NR==3' /etc/init.d/icdnddns.sh | cut -d \"=\" -f2 | cut -d \"'\" -f2)">>/etc/rc.d/init.d/icdngreeting
echo "myparam=\"internalip=\$myintip&hostname=\$myhost&nickname=\$mynickname&gateway=\$mygateway&pcinfo=\$mypcinfo&myicdnid=\$icdnid\"">>/etc/rc.d/init.d/icdngreeting
echo "curl \"http://www.icdn.plus/node/register.php\" -d \"\$myparam\"">>/etc/rc.d/init.d/icdngreeting
echo "echo \$myparam">>/etc/rc.d/init.d/icdngreeting
echo "HOSTIP=\$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++) if(\$i==\"src\") print \$(i+1)}')">>/etc/rc.d/init.d/icdngreeting
echo "SSHPORT=\$(grep \"^Port\" /etc/ssh/sshd_config | awk '{print \$2}')">>/etc/rc.d/init.d/icdngreeting
echo "upnpc -e ICDN -a \$HOSTIP \$SSHPORT \$SSHPORT TCP">>/etc/rc.d/init.d/icdngreeting

chmod +x /etc/rc.d/init.d/icdngreeting
echo "/etc/rc.d/init.d/icdngreeting">>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
echo "*/60 * * * * root /etc/rc.d/init.d/icdngreeting">>/etc/crontab

echo "#!/bin/bash" > /etc/rc.d/init.d/icdnddns.sh
echo "#dynamic DNS" >> /etc/rc.d/init.d/icdnddns.sh
chmod +x /etc/rc.d/init.d/icdnddns.sh
echo "/etc/rc.d/init.d/icdnddns.sh">>/etc/rc.d/rc.local
echo "*/5 * * * * root /etc/rc.d/init.d/icdnddns.sh">>/etc/crontab

echo "#!/bin/bash" > /etc/rc.d/init.d/icdnextstatus.sh
echo "#send extra status" >> /etc/rc.d/init.d/icdnextstatus.sh
chmod +x /etc/rc.d/init.d/icdnextstatus.sh
echo "/etc/rc.d/init.d/icdnextstatus.sh">>/etc/rc.d/rc.local
echo "* * * * * root /etc/rc.d/init.d/icdnextstatus.sh">>/etc/crontab

echo "icdn-$(awk -v min=10000 -v max=99999 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')" > /etc/hostname

sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf 
echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf 
sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf 
echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf 

systemctl mask firewalld

%end
reboot

密码生成脚本

[root@bih-br-pxe ~]# cat pxepass.py
import sys
import crypt
salt=crypt.mksalt(crypt.METHOD_SHA512)
password=sys.argv[1]
print crypt.crypt(password,salt)
posted @ 2023-09-20 10:26  lifei888  阅读(189)  评论(0)    收藏  举报