原理:html
要進行自動安裝的主機A,加電啓動時以網卡爲第一啓動設備java
1.啓動時會向網絡廣播,找到dhcp服務器B請求分配IP地址信息,服務器B除了給其分配基本的IP信息(ip、netmask、geteway、dns、domain,主要爲前2者),並給出pxe引導文件地址linux
2.主機B根據A提供的pxe相關文件的tftp地址下載這些文件而後啓動這個引導裝載器(爲何用tftp而不用ftp由於前者是後者的精簡版,訪問資源不用用戶認證!!)ios
3.主機B在引導裝載器的命令行交互模式中告訴其kickstart文件的http地址,由引導裝載器下載此文件 (本文將該ks地址寫入到引導裝載器的配置文件中,從而略去命令行的交互)redis
4.引導裝載器根據kickstart上提供的主機安裝步驟以及centos7安裝包所在的http地址來完成自動安裝shell
實驗環境:vim
1.CentOS7光盤鏡像一張
centos
2.主機A(centos7)、B(centos6或7)、C(centos7)位於同一網絡中服務器
主機A:192.168.56.6網絡
dhcp服務:
提供地址:192.168.56.0/24內分配192.168.56.20-254
提供pxe啓動服務器文件地址 : 192.168.56.6 pxelinux.0
tftp服務:
提供pxe文件、引導文件(含圖形界面),tftp服務器在數據目錄/var/lib/tftpboot/提供pxelinux.0文件和centos7安裝光盤上的相關文件
.
├── boot.msg
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
│ └── default
├── splash.jpg
├── vesamenu.c32
└── vmlinuz
http服務:
提供軟件包庫: centos7安裝盤的內容放在/var/www/html/centos/7/iso/x86_86
提供kickstart文件: ks文件放在/var/www/html/centos/7/iso/ks_c7_x86_64.cfg
主機B:192.168.56.12
負責驗證主機A上的網絡服務是否正常
測試dhcp服務是否正常
dhclient -d 192.168.56.6
測試pxe引導文件是否能下載
tftp 192.168.56.6 -c get pxelinux.0
測試軟件包庫地址:
elinks http://192.168.56.6/centos/7/iso/x86_86
測試kickstart文件地址:
elinks http://192.168.56.6/centos/7/iso/ks_c7_x86_64.cfg
主機C:要進行自動安裝centos7的主機
實驗步驟:
1、主機A的環境準備
1.關閉selinux服務和防火牆服務
# grep "^SELINUX=" /etc/selinux/config
SELINUX=disabled
# systemctl disable firewalld
# systemctl stop firewalld
2.設置yum源,以便安裝其餘服務軟件
# cat /etc/yum.repos.d/CentOS-dvd.repo
[dvd]
name=CentOS-7 - dvd
baseurl=file:///media/dvd
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
# tail -1 /etc/fstab
/dev/cdrom /media/dvd iso9660 defaults,ro 0 0
在第一個的光驅插入centos7的安裝盤,而後執行
# mount -a
以下看到掛載已成功
# mount | grep /media
/dev/sr0 on /media/dvd type iso9660 (ro,relatime)
2、主機A搭建DHCP服務,並用主機客戶端B測試
a.安裝和配置DHCP
# yum -y install dhcp
# vim /etc/dhcp/dhcpd.conf
# cat /etc/dhcp/dhcpd.conf
##########################dhcpd.conf文件開始##########################
#設置客戶端的搜索域名,也就是/etc/resolv.conf中的search指令
option domain-name "tangsw.comg";
#設置客戶端的DNS
option domain-name-servers 192.168.56.6, 192.168.56.11;
#IP地址的最小、大租約時間
default-lease-time 600;
max-lease-time 7200;
#日誌設置
log-facility local7;
#給客戶端分配的地址段,以及指定客戶機從哪一個地址下載pxe文件來啓動引導裝載器
subnet 192.168.56.0 netmask 255.255.255.0 {
range 192.168.56.20 192.168.56.254;
option routers 192.168.56.6;
next-server 192.168.56.6;
filename "pxelinux.0";
}
##########################dhcpd.conf文件開始##########################
b.啓動並設置開機啓動
# systemctl start dhcpd
# systemctl enable dhcpd
c.用主機B驗證可否從服務器A獲取IP租約信息
[root@localhost ~]# yum install -y dhclient
[root@localhost ~]# dhclient -d eth0
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/08:00:27:85:73:30
Sending on LPF/eth0/08:00:27:85:73:30
Sending on Socket/fallback
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x4b5bbd38)
DHCPACK from 192.168.56.6 (xid=0x4b5bbd38)
bound to 192.168.56.104 -- renewal in 277 seconds.
^C
從輸出的信息能夠看到eth0能從192.168.56.6獲取到IP 192.168.56.104
3、主機A上tftp服47務的安裝,準備tftp要提供文件,以及主機B客戶端測試、主機C測試
1. 安裝tftp,tftp是依賴於超級守護進程xinetd的,由xinetd負責管理tftp的啓動和運行
# yum -y install tftp-server
...........(略去).........
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
tftp-server x86_64 5.2-11.el7 dvd 44 k
Installing for dependencies:
xinetd x86_64 2:2.3.15-12.el7 dvd 128 k
...........(略去).........
tftp的配置文件以及數據文件路徑
# rpm -ql tftp-server | egrep "^(/etc|/var)"
/etc/xinetd.d/tftp
/var/lib/tftpboot
2.設置xinetd和tftp開機啓動
# systemctl enable xinetd
# systemctl start xinetd
# vim /etc/xinetd.d/tftp
# grep disable /etc/xinetd.d/tftp
disable = no
3..準備pxelinux.0 (來自syslinux包)
# yum -y install syslinux
# rpm -ql syslinux | grep '/pxelinux.0'
/usr/share/syslinux/pxelinux.0
# cd /var/lib/tftpboot/
[root@c7 tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
4.準備pxe引導相關的引導裝載器
[root@c7 tftpboot]# mkdir pxelinux.cfg
[root@c7 tftpboot]# cp /media/dvd/images/pxeboot/{initrd.img,vmlinuz} .
[root@c7 tftpboot]# cp /media/dvd/isolinux/{boot.msg,vesamenu.c32,splash.png} .
[root@c7 tftpboot]# cp /media/dvd/isolinux/isolinux.cfg pxelinux.cfg/default
[root@c7 tftpboot]# vim /media/dvd/isolinux/isolinux.cfg pxelinux.cfg/default
label linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.ks=http://192.168.56.6/centos/7/iso/ks_c7_x86_64.cfg ip=dhcp
# append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet ,上一行對本行(源文件)作了修改,告訴引導裝載器從哪裏下載ks文件進行自動安裝。
5.經過工具tftp測試客戶端B可否正常訪問服務器A上的tftp服務
注意:客戶端要關閉防火牆,不然可能tftp能鏈接上去可是下載不了文件
[root@localhost ~]# yum -y install tftp
[root@localhost ~]# tftp 192.168.56.6 -c get pxelinux.0
若是pxelinux.0文件能下載,則表示TFTP服務正常
6.測試主機c可否正常經過網絡引導(主機A上提供的DHCP以及TFTP)
設置主機c的網卡設備爲第一啓動設備,加電開機,若是能看到安裝引導界面則表示DHCP、TFTP服務正常,而且用於pxe引導的文件部署正常。
4、主機A上http服務的安裝,部署安裝源,部署kickstart文件,以及主機B客戶端測試、主機C測試
1.安裝httpd服務,設置開機啓動,啓動該服務
# yum -y install httpd
# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
# systemctl start httpd
2.在httpd服務上部署pxe安裝的安裝包庫
# mkdir -p /var/www/html/centos/7/iso/x86_64
# vim /etc/fstab
# tail -2 /etc/fstab
/dev/cdrom /media/dvd iso9660 defaults,ro 0 0
/media/dvd /var/www/html/centos/7/iso/x86_64 none bind,ro 0 0
# mount -a
關盤鏡像先掛載在/media/dvd做爲服務器A的yum源,再經過該目錄綁定到A的httpd目錄,做爲pxe自動安裝的安裝包,一箭雙鵰
在測試換
3.經過工具elinks,讓客戶端B測試服務器A上所部署的安裝源是否正常
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://192.168.56.6/centos/7/iso/x86_64
4.在httpd服務上部署kickstart文件
# yum -y install system-config-kiskstart
# system-config-kiskstart &
若是是用xshell之類的遠程登陸工具,則須要配置才能顯示system-config-kiskstart命令彈出的窗口,不然在A的桌面環境執行該命令。
在這個kickstart文件建立的程序中導入/root/anacodor-ks.cfg,基於這個文件來修改,而後將修改後的文件放到http的目錄,保存路徑爲/var/www/html/centos/7/iso/ks_c7_x86_64.cfg,並用ksvalidator校驗語法是否正確
# ksvalidator /var/www/html/centos/7/ks_c7_x86_64.cfg
# cat /var/www/html/centos/7/ks_c7_x86_64.cfg
##########################ks_c7_x86_64.cfg文件開始##########################
[root@c7 iso]# cat /var/www/html/centos/7/iso/ks_c7_x86_64.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# X Window System configuration information
xconfig --startxonboot
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Reboot after installation
reboot
# Root password
rootpw --iscrypted $1$KHH8cH8b$1O7rrwphchrB2AE0GGku/0
# System timezone
timezone Asia/Shanghai --isUtc
# Use network installation
url --url="http://192.168.56.6/centos/7/iso/x86_64"
repo --name="CentOS" --baseurl="http://192.168.56.11/centos/6/iso/x86_64" --cost=100
# System language
lang en_US
# Firewall configuration
firewall --disabled
# Network information。若是根據安裝主機的網卡數來重複指令的次數極可能安裝不了
network --bootproto=dhcp --device=link
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# SELinux configuration
selinux --disabled
ignoredisk --only-use=sda
# System bootloader configuration
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
%packages
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
kexec-tools
%end
##########################ks_c7_x86_64.cfg文件結束##########################
5.經過工具elinks,讓客戶端B測試服務器A上所部署的安裝源是否正常
[root@localhost ~]# elinks http://192.168.56.6/centos/7/iso/ks_c7_x86_64.cfg
驗證
將主機C在bios設置第一啓動設備爲網卡,開機後顯示引導界面,選擇"Install CentOS 7",按enter,若是能自動安裝centos7,說明成功了。