PXE(Preboot Execution Environment):预启动执行环境,PXE是由Intel设计的协议,它可以使计算机通过网络而不是从本地硬盘、光驱等设备启动。 基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统PXE可以引导和安装Windows,linux等多种操作系统。

目标:通过网卡能够下载安装操作系统的安装文件,安装包,能够让主机不拿光盘,u盘,只拿网卡来引导,进而能够让它自动的启动起来,安装操作系统。只要主机网线插上,进行简单的配置,就可以利用PXE芯片来实现自动安装。 前提准备:centos6,7 IP:10.0.0.200/24 独立的非桥接的网段:vmnet6 ,关闭SELinux策略,firewall防火墙

1、我们把httpd服务开启,并把必要的安装包安装好 yum install dhcp tftp-server httpd syslinux systemctl enable dhcpd httpd tftp.socket systemctl start httpd [root@centos7 tftpboot]# ss -ntlu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 :5353 :
udp UNCONN 0 0 :49063 :
udp UNCONN 0 0 192.168.122.1:53 :
udp UNCONN 0 0 :67 :
udp UNCONN 0 0 %virbr0:67 :
udp UNCONN 0 0 :68 :
udp UNCONN 0 0 :111 :
udp UNCONN 0 0 :893 :
udp UNCONN 0 0 :::69 :::

udp UNCONN 0 0 :::111 :::

udp UNCONN 0 0 :::893 :::

tcp LISTEN 0 128 :111 :
tcp LISTEN 0 128 :6000 :
tcp LISTEN 0 5 192.168.122.1:53 :
tcp LISTEN 0 128 :22 :
tcp LISTEN 0 128 127.0.0.1:631 :
tcp LISTEN 0 100 127.0.0.1:25 :
tcp LISTEN 0 128 127.0.0.1:6010 :
tcp LISTEN 0 128 127.0.0.1:6015 :
tcp LISTEN 0 128 127.0.0.1:6016 :
tcp LISTEN 0 128 :::111 :::

tcp LISTEN 0 128 :::80 :::

tcp LISTEN 0 128 :::6000 :::

tcp LISTEN 0 128 :::22 :::

tcp LISTEN 0 128 ::1:631 :::

tcp LISTEN 0 100 ::1:25 :::

tcp LISTEN 0 128 ::1:6010 :::

tcp LISTEN 0 128 ::1:6015 :::*
tcp LISTEN 0 128 ::1:6016 :::*
这一项是查看到69,67,80端口已在运行,可以进行下一步操作, 2、我们要让dhcp告诉客户端tftp是谁,从tftp上下载什么文件,要添加两项内容: [root@centos7 tftpboot]# vim /etc/dhcp/dhcpd.conf

Fixed IP addresses can also be specified for hosts. These addresses

should not also be listed as being available for dynamic assignment.

Hosts for which fixed IP addresses have been specified can boot using

BOOTP or DHCP. Hosts for which no fixed address is specified can only

be booted with DHCP, unless there is an address range on the subnet

to which a BOOTP client is connected which has the dynamic-bootp flag

set.

host fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com; } host centos6 { hardware ethernet 00:0c:29:d1:ac:34; log-facility local7;

No service will be given on this subnet, but declaring it helps the

DHCP server to understand the network topology.

subnet 10.0.0.0 netmask 255.255.255.0 { range 10.0.0.10 10.0.0.100; option routers 10.0.0.1; filename "pxelinux.0"; next-server 10.0.0.200;
}

This is a very basic subnet declaration.

subnet 10.254.239.0 netmask 255.255.255.224 { range 10.254.239.10 10.254.239.20; option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org; }

This declaration allows BOOTP clients to get dynamic addresses,

which we don't really recommend.

filename "pxelinux.0"; next-server 10.0.0.200; next-server就是说的tftp,后加自己的地址,

3、可以这样搜索出想要下载的安装包 [root@centos7 tftpboot]# yum search pxelinux Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile =============================== Matched: pxelinux =============================== syslinux.x86_64 : Simple kernel loader which boots from a FAT filesystem syslinux-extlinux.x86_64 : The EXTLINUX bootloader, for booting the local system. syslinux-tftpboot.noarch : SYSLINUX modules in /var/lib/tftpboot, available for : network booting 4、 [root@centos7 tftpboot]# yum install syslinux Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package syslinux.x86_64 0:4.05-15.el7 will be installed --> Finished Dependency Resolution

Dependencies Resolved

================================================================================= Package Arch Version Repository Size

Installing: syslinux x86_64 4.05-15.el7 development 990 k

Transaction Summary

Install 1 Package

Total download size: 990 k Installed size: 2.3 M Is this ok [y/d/N]: y Downloading packages: syslinux-4.05-15.el7.x86_64.rpm | 990 kB 00:00:00
Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : syslinux-4.05-15.el7.x86_64 1/1 Verifying : syslinux-4.05-15.el7.x86_64 1/1

Installed: syslinux.x86_64 0:4.05-15.el7

Complete! [root@centos7 tftpboot]# rpm -ql syslinux /usr/bin/gethostip /usr/bin/isohybrid /usr/bin/memdiskfind /usr/bin/syslinux /usr/share/syslinux/pwd.c32 /usr/share/syslinux/pxechain.com /usr/share/syslinux/pxelinux.0 /usr/share/syslinux/reboot.c32 /usr/share/syslinux/rosh.c32 /usr/share/syslinux/sanboot.c32 可以看到安装包中含有“ /syslinux/pxelinux.0 ”包,

4、[root@centos7 tftpboot]# vim /var/www/html/ks/ks7-mini.cfg 查看一下应答文件的url路径是否有误, #version=DEVEL

System authorization information

auth --enableshadow --passalgo=sha512

Use CDROM installation media

url --url=http://192.168.141.130/centos/7/os/x86_64/

Use graphical install

text

Run the Setup Agent on first boot

firstboot --enable ignoredisk --only-use=sda

Keyboard layouts

keyboard --vckeymap=us --xlayouts='us'

System language

lang en_US.UTF-8

Network information

network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate network --hostname=centos7.localdomain

Root password

rootpw --iscrypted $6$VYubI8zpV4R/R1M7$.vVqobGDm7t9YAHH924tWzNjqrKGAt4JiBMBu2iWwpIuW2RuupH5OgA1JbEgmZgh8oOSFlY6FeQMVBhN4yM8E0

System services

firewall --disabled selinux --disabled services --disabled="chronyd"

System timezone

timezone Asia/Shanghai --isUtc --nontp "/var/www/html/ks/ks7-mini.cfg" 47L, 1472C

5、把安装包中的文件拷贝到当前目录, [root@centos7 tftpboot]# cp /usr/share/syslinux/pxelinux.0 . [root@centos7 tftpboot]# ls f2 pxelinux.0

6、mkdir /var/www/html/centos/7 -pv 添加开机自动挂载光盘 vim /etc/fstab /dev/sr1 /var/www/html/centos/7 iso9660 default 0 0 挂载磁盘 mount /dev/sr1 /var/www/html/centos/7
mount /dev/sr0 /mnt 重新读取磁盘挂载 mount -a [root@centos7 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 50G 0 part / ├─sda3 8:3 0 2G 0 part [SWAP] ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 10G 0 part /data sdb 8:16 0 120G 0 disk ├─sdb1 8:17 0 40G 0 part /madedu_data ├─sdb2 8:18 0 50G 0 part └─sdb3 8:19 0 30G 0 part sr0 11:0 1 10G 0 rom /mnt sr1 11:1 1 3.7G 0 rom /var/www/html/centos/7

7、[root@centos7 ~]# cd /var/www/html/centos/7/ [root@centos7 7]# ls CentOS_BuildTag isolinux RPM-GPG-KEY-CentOS-Debug-6 EFI Packages RPM-GPG-KEY-CentOS-Security-6 EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Testing-6 GPL repodata TRANS.TBL images RPM-GPG-KEY-CentOS-6 [root@centos7 7]# cd isolinux/ [root@centos7 isolinux]# ls boot.cat grub.conf isolinux.bin memtest TRANS.TBL vmlinuz boot.msg initrd.img isolinux.cfg splash.jpg vesamenu.c32

8、我们把所要用到的文件有选择的拷贝过来,不需要的不用拷贝 [root@centos7 isolinux]# cp initrd.img vmlinuz /var/lib/tftpboot/ 9、我们把“ isolinux.cfg ”文件也拷贝过来,它是开机菜单,不能直接放在/var/lib/tftpboot/下,要新建一个文件夹:“ mkdir /var/lib/tftpboot/pxelinux.cfg/ ” 拷贝过去还要更名为“ default ” [root@centos7 isolinux]# ls boot.cat grub.conf isolinux.bin memtest TRANS.TBL vmlinuz boot.msg initrd.img isolinux.cfg splash.jpg vesamenu.c32 [root@centos7 isolinux]# mkdir /var/lib/tftpboot/pxelinux.cfg/ [root@centos7 isolinux]# cp isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

10、 [root@centos7 isolinux]# cd /var/lib/tftpboot/ [root@centos7 tftpboot]# tree . ├── initrd.img ├── pxelinux.0 ├── pxelinux.cfg │   └── default └── vmlinuz

1 directory, 4 files

11、[root@centos7 tftpboot]# rpm -ql syslinux /usr/bin/gethostip /usr/bin/isohybrid /usr/bin/memdiskfind /usr/share/syslinux/meminfo.c32 /usr/share/syslinux/menu.c32 /usr/share/syslinux/pcitest.c32 其中,/usr/share/syslinux/menu.c3该包也是一个简洁的开机菜单,开机蓝色背景界面,我们把它拷贝过来, [root@centos7 tftpboot]# cp /usr/share/syslinux/menu.c32 . [root@centos7 tftpboot]# ls initrd.img menu.c32 pxelinux.0 pxelinux.cfg vmlinuz [root@centos7 tftpboot]# tree . ├── initrd.img ├── menu.c32 ├── pxelinux.0 ├── pxelinux.cfg │   └── default └── vmlinuz

1 directory, 5 files 有了这几个基本项,我们就可以实现安装了,但是有些地方还要改, [root@centos7 tftpboot]# vim pxelinux.cfg/default

default menu.c32 #prompt 1 timeout 600

menu title Welcome to www.magedu.com

label mini menu label ^Install an Mini Centos7.6 system Kernel vmlinuz append initrd=initrd.img ks=http://192.168.141.130/ks/centos7-mini.cfg label desktop menu label Install an ^Desktop Centos7.6 system Kernel vmlinuz append initrd=initrd.img ks=http://192.168.141.130/ks/centos7-desktop.cfg
label local menu label Boot from ^local drive menu default localboot 0xffff 以上是我们修改过的配置文件,配置了三个文件,分别是最小化安装,桌面安装,本地安装, 查看其权限: [root@centos7 tftpboot]# ll pxelinux.cfg/default -r--r--r-- 1 root root 476 Feb 3 16:58 pxelinux.cfg/default [root@centos7 tftpboot]# tree . ├── initrd.img ├── menu.c32 ├── pxelinux.0 ├── pxelinux.cfg │   └── default └── vmlinuz

1 directory, 5 files 我们的pxe安装文件如上所示!!!

12、正常情况下,centos7有系统就会用系统来启动,若无系统情况下,我们来看:

dd if=/dev/zero of=/dev/sda bs=1 count=446 446+0 records in 446+0 records out 446 bytes (446 B) copied, 0.00115566 s, 386 KB/s reboot! 此时,系统已经被人为破坏,把CD光盘断开,

注意:我们要把/var/www/html/ks/centos7-mini.cfg的路径也修改为10.0.0.200,才能正确安装。