1、介紹 2、原理 3、安裝部署PXE+kickstart 4、安裝TFTP 5、安裝DHCP 6、生成kickstart(ks.cfg)文件 7、安裝HTTP 8、虛擬機測試自動安裝
CentOS的安裝方法挺多,最多見的有光盤、Kickstart無人值守安裝、U盤安裝及ISO硬盤安裝等。若是如今讓你安裝100臺服務器,甚至更多臺服務器,你會用什麼辦法安裝呢,用光盤?這樣要損壞多少張光盤呢,價格也不菲,U盤安裝?一臺一臺接,很累好很差,用kickstart無人值守安裝的方法,能夠在內網批量安裝新服務器系統,這種方法極大地簡化了用光盤重複安裝的過程,極大地提升了工做效率。html
1.什麼是PXElinux
(1)PXE不是一種安裝方式,而是一種引導方式 (2)PXE(Pre-boot Execution Environment)協議可使計算機經過網絡啓動 (3)採用C/S結構
2.PXE的工做過程nginx
(1)PXE Client向DHCP發送請求 PXE Client從本身的PXE網卡啓動,經過PXE BootROM(自啓動芯片)會以UDP協議發送一個廣播請求,向本網絡中的DHCP服務器索取IP。 (2)DHCP服務器提供信息 DHCP服務器收到客戶端的請求,驗證是不是合法的PXE Client的請求,驗證經過它將給客戶端一個提供相應, 這個提供相應中包含了爲客戶端分配的IP地址、pxelinux啓動程序(TFTP)位置,以及配置文件所在位置。 (3)PXE客戶端請求下載啓動文件 客戶端收到服務器的迴應後,會迴應一個幀,以請求傳送啓動所需文件。這些文件包括:pxelinux.0、pxelinux.cfg/default、initrd.img等文件。 (4)Boot Server響應客戶端請求並傳送文件 當服務器收到客戶端的請求後,他們之間以後將有更多的信息在客戶端與服務器之間做應答,用以決定啓動參數。 BootROM由TFTP通信協議從Boot Server下載啓動安裝程序所必須的文件(pxelinux.0、pxelinux.cfg/default)。 default文件下載完成後,會根據該文件中定義的引導順序,啓動Liunx安裝程序的引導內核。 (5)請求下載自動應答文件 客戶端經過pxelinux.cfg/default文件成功的引導Linux安裝內核後,安裝程序首先必須肯定你經過什麼介質來安裝linux 若是經過網絡安裝(NFS.FTP.HTTP),則會在這個時候初始化網絡,並定位安裝源位置。 接着會讀取default文件中指定的自動應答文件ks.cfg所在位置,根據該位置請求下載該文件。 (6)將ks.cfg文件下載回來後,經過該文件找到OS Server,並安裝該文件的配置請求下載安裝過程須要的軟件包。 OS Server和客戶端創建鏈接後,將開始傳輸軟件包,客戶端將開始安裝操做系統。安裝完成後,將提示從新引導計算機。
補充信息,這裏圖文並茂介紹PXE工做流程git
http://www.zyops.com/autoinstall-kickstart
1.系統環境準備vim
[root@pxe ~]# uname -a #這是一臺桌面GUN的Linux,可能你問爲何要用桌面呢,後面要用到啦 Linux pxe 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux [root@pxe ~]# uname -m x86_64 #64位系統 [root@pxe ~]# cat /etc/redhat-release CentOS release 6.6 (Final) #版本是6.6 [root@pxe ~]# getenforce Disabled #禁用selinux [root@pxe ~]# /etc/init.d/iptables stop #關閉防火牆 [root@pxe ~]# ifconfig eth0 | sed -n '2p' | awk '{print $2}' | awk -F ":" '{print $2}' 192.168.16.100 #這臺機器的IP地址
注意:虛擬機環境網卡採用NAT模式,不要使用橋接模式,把VMware的NAT模式的DHCP服務也關閉windows
2.建立用於存放鏡像裏面的內容目錄服務器
[root@pxe ~]# mkdir -p /data/sys
3.掛載鏡像到mnt目錄下,再將系統鏡像裏邊的內容拷貝到共享目錄/data/sys中去網絡
[root@pxe ~]# mount /dev/cdrom /mnt/ mount: block device /dev/sr0 is write-protected, mounting read-only [root@pxe ~]# ll /mnt/ total 712 -r--r--r--. 2 test test 14 Oct 24 2014 CentOS_BuildTag dr-xr-xr-x. 3 test test 2048 Oct 24 2014 EFI -r--r--r--. 2 test test 212 Nov 28 2013 EULA -r--r--r--. 2 test test 18009 Nov 28 2013 GPL dr-xr-xr-x. 3 test test 2048 Oct 24 2014 p_w_picpaths dr-xr-xr-x. 2 test test 2048 Oct 24 2014 isolinux dr-xr-xr-x. 2 test test 686080 Oct 24 2014 Packages -r--r--r--. 2 test test 1354 Oct 20 2014 RELEASE-NOTES-en-US.html dr-xr-xr-x. 2 test test 4096 Oct 24 2014 repodata -r--r--r--. 2 test test 1706 Nov 28 2013 RPM-GPG-KEY-CentOS-6 -r--r--r--. 2 test test 1730 Nov 28 2013 RPM-GPG-KEY-CentOS-Debug-6 -r--r--r--. 2 test test 1730 Nov 28 2013 RPM-GPG-KEY-CentOS-Security-6 -r--r--r--. 2 test test 1734 Nov 28 2013 RPM-GPG-KEY-CentOS-Testing-6 -r--r--r--. 1 test test 3380 Oct 24 2014 TRANS.TBL [root@pxe ~]# cp -a /mnt/* /data/sys/
在安裝PXE過程當中,客戶端經過TFTP協議從TFTP服務器下載引導文件並執行,所以,須要配置TFTP服務器和PXE的引導配置完成這個過程app
1.安裝tftp和xinetddom
[root@pxe ~]# yum install tftp-server.x86_64 xinetd.x86_64 -y [root@pxe ~]# rpm -qa tftp-server xinetd xinetd-2.3.14-40.el6.x86_64 tftp-server-0.49-8.el6.x86_64
2.配置tftp服務
TFTP服務使用Xinetd服務管理,編輯/etc/xinetd.d/tftp,將文件中disable的參數由yes改成no [root@pxe ~]# vim /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 }
3.啓動xinetd,而且設置開機自啓動
[root@pxe ~]# /etc/init.d/xinetd start Starting xinetd: [ OK ] [root@pxe ~]# chkconfig --level 3 xinetd on [root@pxe ~]# chkconfig --list xinetd xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
4.配置支持PXE引導啓動程序
PXE啓動映像文件由syslinux軟件提供,只要安裝了syslinux,就會生成一個pxelinux.0文件,將這個文件複製到TFTP默認路徑便可。 syslinux是一個功能強大的引導加載程序,並且兼容各類介質,更加確切地說:syslinux是一個小型的Linux操做系統,它的目的是簡化Linux的時間,並創建修護或其餘特殊用途的啓動盤。
[root@pxe ~]# yum install syslinux.x86_64 -y [root@pxe ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ [root@pxe ~]# ll /var/lib/tftpboot/ total 28 -rw-r--r--. 1 root root 26759 Apr 19 23:54 pxelinux.0
5.用於網絡啓動的內核文件
將CentOS安裝光盤目錄中的p_w_picpaths/pxeboot/{vmlinuz,initrd.img}啓動文件複製到某個安裝目錄,好比/var/lib/tftpboot/
[root@pxe ~]# cp /data/sys/p_w_picpaths/pxeboot/vmlinuz /var/lib/tftpboot/ #複製內核文件 [root@pxe ~]# cp /data/sys/p_w_picpaths/pxeboot/initrd.img /var/lib/tftpboot/ #複製驅動文件 root@pxe ~]# cp /data/sys/isolinux/boot.msg /var/lib/tftpboot/ #複製boot.msg
6.建立pxelinx.cfg目錄,複製isolinux.cfg文件到/tftpboot目錄並更名default
[root@pxe ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg [root@pxe ~]# cp /data/sys/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
7.編輯配置default文件
[root@pxe ~]# cd /var/lib/tftpboot/pxelinux.cfg/ [root@pxe pxelinux.cfg]# chmod 644 default [root@pxe pxelinux.cfg]# vim default default linux #默認啓動的是「lobeltext」中標記的內核 #prompt 1 #開啓會顯示命令行'boot: '提示符。prompt值爲0時則不提示,將會直接啓動'default'參數中指定的 timeout 1 #timeout時間是引導時等待用戶手動選擇的時間,設爲1可直接引導,單位爲1/10秒 display boot.msg # 菜單背景圖片、標題、顏色 menu background splash.jpg menu title Welcome to CentOS 6.6! menu color border 0 #ffffffff #00000000 menu color sel 7 #ffffffff #ff000000 menu color title 0 #ffffffff #00000000 menu color tabmsg 0 #ffffffff #00000000 menu color unsel 0 #ffffffff #00000000 menu color hotsel 0 #ff000000 #ffffffff menu color hotkey 7 #ffffffff #ff000000 menu color scrollbar 0 #ffffffff #00000000 label linux menu label ^Install or upgrade an existing system menu default kernel vmlinuz #指定要啓動的內核。一樣要注意路徑,默認是/var/lib/tftpboot目錄 append initrd=initrd.img ks=http://192.168.16.100/ks.cfg #添加ks文件的地址
在PXE安裝過程當中,PXE客戶機主要經過DHCP服務獲取地址,PXE引導文件名稱,而後再客戶端機上經過TFTP協議從TFTP服務器下載引導文件並執行,從而啓動系統安裝程序執行後,接着下載並安裝程序啓動安裝。
1.安裝和配置DHCP
[root@pxe ~]# yum install dhcp.x86_64 -y [root@pxe ~]# vim /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.sample # see 'man 5 dhcpd.conf' # ddns-update-style none; #不ddns更新 next-server 192.168.16.100; #告知客戶端tftp服務器的ip地址 filename "pxelinux.0"; #告知客戶端從TFTP根目錄下載pxelinux.0文件 subnet 192.168.16.0 netmask 255.255.255.0 { #設定子網 range 192.168.16.10 192.168.16.20; #設定子網範圍 option domain-name-servers 114.114.114.114; #設定DNS option routers 192.168.16.2; #設定默認網關 default-lease-time 600; #設置默認的IP租用時間,單位爲秒 max-lease-time 7200; #最大的IP租用時間,單位爲秒 } log-facility local4; #記錄DHCP日誌
2.建立日誌文件
[root@pxe ~]# vim /etc/rsyslog.conf # Save dhcpd messages also to dhcpd.log local4.* /var/log/dhcpd.log [root@pxe ~]# touch /var/log/dhcpd.log [root@pxe ~]# chmod 600 /var/log/dhcpd.log
3.啓動DHCP服務器
[root@pxe ~]# /etc/init.d/dhcpd start Starting dhcpd: [ OK ]
4.設置開機自啓動
[root@pxe ~]# chkconfig dhcpd on [root@pxe ~]# chkconfig --list | grep dhcp dhcpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
一般安裝操做系統過程當中,須要經常和服務器進行交互操做,爲了減小這個交互過程可使用kickstart。使用kickstart,只需先定義好一個kickstart自動應答配置文件ks.cfg,並讓安裝程序知道該配置文件的位置,在安裝過程當中安裝程序就能夠自動從文件中讀取kickstart配置文件。
1.使用CentOS Linux提供一個圖形化的kickstart配置工具
[root@pxe ~]# yum install system-config-kickstart.noarch
打開kickstart工具,也在圖形終端打開命令行窗口鍵入命令system-config-kickstart打開
這裏選擇語言、時區、root密碼,勾選第一個,安裝系統後重啓
選擇全新安裝,安裝源方式爲HTTP,HTTP地址和目錄
選擇是引導,默認就行啦
分區信息,清除引導,移除分區,添加你須要的分區
添加網絡設備,若是隻有一張網卡就eth0啦
這裏密碼加密,默認就行
防火牆配置,我這裏是選擇關閉的
顯示配置,默認便可
選擇安裝的軟件包,根據本身狀況選擇哈
選擇左上角的File,點擊Save(保存)
保存的路徑/data/sys/ks.cfg,這個路徑很講究喔
查看保存後的ks.cfg文件,本應該是有三部分的,但我沒有選擇腳本選項,因此這裏文件只有兩部分,第一是鍵盤類型,語言,安裝方式等系統的配置,有必選項和可選項,若是缺乏某項必選項,安裝時會中斷並提示用戶選擇此項的選項,第二是軟件包安裝
[root@pxe ~]# cat /data/sys/ks.cfg #platform=x86, AMD64, or Intel EM64T #version=DEVEL # Firewall configuration firewall --disabled #防火牆禁用 # Install OS instead of upgrade install #表示是安裝系統不是升級 # Use network installation url --url="http://192.168.16.100/" #告訴安裝程序,到http://ip/下面找安裝介質 # Root password rootpw --iscrypted $1$ZoLiOvI6$jV67RM98I/JKaKpE6vUDz0 #root密碼 # System authorization information auth --useshadow --passalgo=sha512 # Use text mode install text #文本安裝方式 firstboot --disable # System keyboard #鍵盤類型 keyboard us # System language #系統語言 lang en_US # SELinux configuration #禁用selinux selinux --disabled # Installation logging level #設置日誌級別 logging --level=info # Reboot after installation #安裝完系統後重啓 reboot # System timezone #系統時區 timezone Asia/Shanghai # Network information #網絡信息 network --bootproto=dhcp --device=eth0 --onboot=on # System bootloader configuration bootloader --location=mbr #系統引導相關信息 # Clear the Master Boot Record zerombr # Partition clearing information #清除全部分區 clearpart --all --initlabel # Disk partitioning information #系統的三個分區 part /boot --fstype="ext4" --size=200 part swap --fstype="swap" --size=2048 part / --asprimary --fstype="ext4" --grow --size=1 %packages #安裝的軟件包 @base @compat-libraries @debugging @development git %end
給予ks.cfg權限
[root@pxe ~]# chmod 777 /data/sys/ks.cfg
1.檢查是否安裝
[root@pxe ~]# /usr/local/nginx/sbin/nginx -v #這是我編譯安裝好的nginx nginx version: nginx/1.12.0
2.配置nginx.conf文件
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name 192.168.16.100; location / { root /data/sys; #這個目錄是存放ks.cfg index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3.檢查語法
[root@pxe conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4.啓動nginx服務
[root@pxe conf]# /usr/local/nginx/sbin/nginx
而後就是你去吃個飯回來,就安裝好啦,去吃飯啦
最後想說的是,這個自動化安裝功能我超級喜歡,就好像windows AD部署利用組策略自動安裝軟件同樣,嘻嘻,感謝51CTO這個平臺,讓本身學習更多知識,看到不少大牛的好文章,才能讓本身進步。