PXE自動化安裝centos6、7系統

在公司避免不了會給電腦重裝系統,小到幾臺大到幾十上百臺,如果就只有幾臺手動安裝就可以了,那么如果幾十上百臺,手動安裝那是一件很費力費時間的事情。現在那么就可以考慮自動化給公司安裝系統。下面介紹如何使用PXE自動化給公司安裝centos6、7系統。

首先何為PXE:

PXE:Preboot Excution Environment, Intel公司研發,沒有任何操作系統的主機能夠基于網絡完成系統的安裝工作。

PXE的工作原理:

  1. Client向PXE Server上的DHCP發送IP地址請求消息,DHCP檢測Client是否合法(主要是檢測Client的網卡MAC地址),如果合法則返回Client的IP地址,同時將啟動文件pxelinux.0的位置信息一并傳送給Client。
  2. Client向PXE Server上的TFTP發送獲取pxelinux.0請求消息,TFTP接收到消息之后再向Client發送pxelinux.0大小信息,試探Client是否滿意,當TFTP收到Client發回的同
    意大小信息之后,正式向Client發送pxelinux.0 Client執行接收到的pxelinux.0文件。
  3. Client向TFTP Server發送針對本機的配置信息文件(在TFTP 服務的pxelinux.cfg目錄下),TFTP將配置文件發回Client,繼而Client根據配置文件執行后續操作。
  4. Client向TFTP發送Linux內核請求信息,TFTP接收到消息之后將內核文件發送給Client。
  5. Client向TFTP發送根文件請求信息,TFTP接收到消息之后返回Linux根文件系統。
  6. Client啟動Linux內核。
  7. Client下載安裝源文件,讀取自動化安裝腳本。

安裝過程:

一、安裝前準備:

關閉作為DHCP的服務器的防火墻和SELinux,防止以后自動化安裝問題出錯。DHCP服務器為靜態IP。

二、安裝DHCP服務

作為以后PXE自動化安裝,必須從服務其上拿到相應的IP地址,才能繼續自動化安裝。
1.以下在VM虛擬機下操作,要做DHCP服務器,需手懂給系統分配IP,并把橋接模式去掉,留下主機模式。

[root@centos7 ~]#nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.18.144/24 
這里為服務手動指定一個IP 地址

并將使用本地DHCP服務將IP地址分配給虛擬機前面√去掉。
1.安裝DHCP:注意幾個比較重要的配置文件

[root@centos7 ~]#yum -y install dhcp
[root@centos7 ks]#rpm -ql dhcp
......
/etc/dhcp/dhcpd.conf  注:dhcp的配置文件
/var/lib/dhcpd/dhcpd.leases 注:服務器端查看那些機器從這獲取了ip

2.查看配置文件,配置DHCP服務:

[root@centos7 ~]#systemctl start dhcpd 注:剛開始啟動,是啟動不了DHCP服務,沒有配置文件。
[root@centos7 ~]#cat /etc/dhcp/dhcpd.conf 
注:默認DHCP服務是沒有配置,讓我們自己配置或者參考 /usr/share/doc/dhcp*/dhcpd.conf.example文件,
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
[root@centos7 ~]#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf  注:拷貝模板文件
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
# option definitions common to all supported networks...
#option domain-name "example.org"; 選項域名:這里可自己起一個
option domain-name "jie";
#option domain-name-servers ns1.example.org, ns2.example.org;  域名NDS
option domain-name-servers 114.114.114.114,8.8.8.8;
option routers 192.168.18.1;
default-lease-time 600;  默認租賃時間600秒
max-lease-time 7200;    最長時間7200秒
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
subnet 192.168.18.0 netmask 255.255.255.0 {
 range 192.168.18.100 192.168.18.150;
} 在這添加這臺電腦所屬的網段參考下面的例子來填。
subnet 10.152.187.0 netmask 255.255.255.0 {
}

3.啟動服務:


實驗:開一臺虛擬機是否從這臺DHCP服務器上拿到ip


結果機器成功拿到ip但還不確定是否從這臺DHCP拿到的ip,我們可以下命令在DHCP服務其上查看。
[root@centos7 ~]#cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.2.5
server-duid "\000\001\000\001!\006\036\335\000\014),\257\222";
lease 192.168.18.136 {
  starts 6 2017/07/22 14:35:24;
  ends 6 2017/07/22 14:45:24;
  cltt 6 2017/07/22 14:35:24;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:14:f0:43;
  client-hostname "cnetos7";
}
lease 192.168.18.135 {
  starts 6 2017/07/22 14:35:24;
  ends 6 2017/07/22 14:45:24;
  cltt 6 2017/07/22 14:35:24;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:14:f0:39;
  client-hostname "cnetos7";
}

客戶端 :cat /var /lib/dhclient/dhclient.leases


通過查看ip與mac地址則可發現客戶端成功獲取ip。

三、為PXE自動化安裝,給系統裝上必要的軟件服務

1.安裝軟件包:

[root@centos7 ~]#yum -y install httpd  :httpd服務走網絡自動化安裝
[root@centos7 ~]systemctl start httpd  :啟動httpd服務
[root@centos7 ~]#systemctl enable httpd   :開機啟用httpd服務
[root@centos7 ~]#yum -y install tftp-server :安裝tftp服務端軟件包
[root@centos7 ~]systemctl start tftp  :啟動 tftp服務
[root@centos7 ~]#systemctl enable tftp   :開機啟用 tftp服務
[root@centos7 ~]#yum -y install syslinux :生成將來服務要用的pxelinux.0等重要文件
[root@centos7 ~]#yum -y install system-config-kickstart :圖形化生成系統安裝配置文件。
啟動服務后查看是否相應端口打開。httpd:80 端口 tftp:69端口
iptables -F關閉防火墻

2.配置共享服務:
實現網絡能獲取資源安裝包:

 [root@centos7 ~]#mkdir /var/www/html/centos7  :創建centos7目錄存放centos7安裝相關文件
 [root@centos7 ~]#mkdir /var/www/html/centos6 ::創建centos6目錄存放centos6安裝相關文件
在機器上掛載centos6和centos 7的安裝光盤,也可以把按裝光盤里的內容拷貝進相應的目錄。這里直接把相應光盤掛載到相應的目錄。
[root@centos7 ks]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sr0        3.7G  3.7G     0 100% /var/www/html/centos6
/dev/sr1        7.8G  7.8G     0 100% /var/www/html/centos7
[root@centos7 ~]#mount /dev/sr1 /var/www/html/centos/7
[root@centos7 ~]#mount /dev/sr0 /var/www/html/centos/6
注意:在VM上如果掛上兩張光盤不能馬上識別兩張,只識別其中一張是,可重啟系統。重啟系統后,注意相應的服務打開沒有。

通過頁面查看是否成功。



通過頁面可成功訪問,則繼續下一步。
3.裝備kickstart文件:
kickstart文件通常以cfg結尾,就像我們剛裝完操作系統會在root 家目錄下生成一個anaconda-ks.cfg的文件,這個文件記錄了我們安裝操作系統的配置信息。

[root@centos7 ~]#cat anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# 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=off --ipv6=auto --activate
network  --bootproto=dhcp --device=ens34 --ipv6=auto --activate
network  --hostname=centos7.3.zj.com
# Root password
rootpw --iscrypted $6$9HqkLNFljMIUPxQp$RN/c9R4Aioox2YN9G5CUxp2Tx9adxkTpCDBfEd7q.T2ILpu.N3ui0OX0A1m.RVww1PxEi7vexNbduJC/QVQAe0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Vladivostok --isUtc --nontp
user --name=zj --password=$6$fcqC26T/5m2H6wQ4$TR69Lknl3TcGUXChOQP4nedTV3Wrd/hc77p.1ZPRKWnoNKYUr5/DUTDFZK34XUAYlRv92XHRA733FPPYKeW8o. --iscrypted --gecos="zj"
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=plain
# Partition clearing information
clearpart --none --initlabel
%packages
@^gnome-desktop-environment
@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
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end

kickstart的文件我們可以按照原本系統完成安裝的anaconda-ks.cfg作為模板來修改,也可以通過system-config-kickstart 來生成cfg文件。

選擇安裝包這里centos7上默認不可選著,這時我們可以修改yum源的repo文件名稱改為development.(不知這是否是一個bug)
以上是生成centos7的kickstart配置文件,可根據自己的需求再詳細配置。生成centos6的kickstart需在centos6的系統完成以上相同的步驟即可。
也可以通過查看centos7.3.cfg文件,與系統的anaconda-ks.cfg文件其實是大同小異。
[root@centos7 ~]#cat centos7.3.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$8jkYz2Hx$8IafUvMALxHgdMtM//D.Q.
# Use network installation
url --url="http://192.168.18.144/centos7"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Partition clearing information
clearpart --all
# Disk partitioning information
part / --fstype="xfs" --size=50000
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=2048
%packages
@base
@gnome-desktop
%end

把生成的centos7.3.cfg和centos6.9.cfg文件存到相應的網絡路徑中

[root@centos7 ~]#mkdir /var/www/html/ks :生成ks目錄,用于存放centos6和centos7的kickstart文件。
[root@centos7 ~]#cp /root/centos7.3.cfg /var/www/html/ks/     :centos7上拷貝centos7.3.cfg文件到/var/www/html/ks/目錄下
[root@centos6 ~]#scp centos6.9.cfg 192.168.18.144:/var/www/html/ks/
:在centos6上的centos6.9.cfg 傳到/var/www/html/ks/目錄下。

通過瀏覽器查看:配置成功

四、配置DHCP服務:

前面配置過DHCP服務,這里我們只需添加pxelinux.0信息

subnet 192.168.18.0 netmask 255.255.255.0 {
 range 192.168.18.100 192.168.18.150;
 option routers 192.168.18.1;    
 filename "pxelinux.0";     #指定PXE引導程序的文件名
 next-server 192.168.18.144;  #指定TFTP服務器的地址
}
subnet 10.152.187.0 netmask 255.255.255.0 {
}
注明:filename這一個選項很重要,它的作用是指明bootstrap所在的位置,一般是指向一個TFTP服務器的某個目錄下。這里是相對路徑,其中路徑的上半部分在的一個配置文件之中。
[root@centos7 ~]#systemctl restart dhcpd  重啟DHCP服務。

五、整理準備相關文件

[root@centos7 ~]#mkdir /var/lib/tftpboot/pxelinux.cfg/  :在/var/lib/tftpboot/生成pxelinux.cfg目錄為將來生成default菜單。
[root@centos7 ~]#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/  :拷貝pxelinux.0(引導程序)menu.c32(背景圖片)到/var/lib/tftpboot/下
[root@centos7 ~]#mkdir /var/lib/tftpboot/centos6 :創建centos6目錄存放centos6的內核引導文件。
[root@centos7 ~]#cp /var/www/html/centos6/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/  :拷貝centos6光盤的vmlinuz,initrd.img文件到/var/lib/tftpboot/里
[root@centos7 ~]#mkdir /var/lib/tftpboot/centos7 :創建centos7目錄存放centos7的內核引導文件。
[root@centos7 ~]#cp /var/www/html/centos7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/  :拷貝centos7光盤的vmlinuz,initrd.img文件到/var/lib/tftpboot/里

六、配置啟動菜單

可參考根據系統光盤文件的isolinux目錄下isolinux.cfg文件修改。

[root@centos7 ~]#vim /var/lib/tftpboot/pxelinux.cfg/default
[root@centos7 ~]#cat /var/lib/tftpboot/pxelinux.cfg/default  :這里配置default啟動菜單如下。
default menu.c32
timeout 600
menu title PXE CentOS Linux 7 and 6  Install Menu
label autocentos7   :自動安裝centos7
  menu label ^Automatic Mini Install CentOS 7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://192.168.18.144/ks/centos7.3.cfg
label manualcentos7  :手動安裝centos7
  menu label ^Manual Install CentOS 7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img inst.repo=http://192.168.18.144/centos7
label autocentos6 :自動安裝centos6
  menu label Automatic ^Mini Install CentOS 6
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img ks=http://192.168.18.144/ks/centos6.9.cfg
label manuallcentos6  :手動安裝centos7
  menu label ^Manual Install CentOS 6
  kernel cenots6/vmlinuz
  append initrd=centos6/initrd.img inst.repo=http://192.168.18.144/centos6
label local  :默認本地安裝
  menu label Boot from ^local drive
  menu default
  localboot 0xffff

七、檢查各項配置

在/var/lib/tftpboot/里的文件目錄如下

[root@centos7 ~]#tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│   ├── initrd.img
│   └── vmlinuz
├── centos7
│   ├── initrd.img
│   └── vmlinuz
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
    └── default

到這里PEX自動化安裝配置文件就全部完成。

八、測試安裝

選擇網絡啟動安裝

成功進入并從服務器獲取了ip,將執行PEX自動化安裝centos
這里分別測試centos7和centos6的自動安裝
centos6成功進入字符界面開始安裝。

稍等片刻centos6成功完成安裝。
centos7稍等片刻也進入圖形自動化安裝界面如需字符界面可在centos7.3.cfg文件修改
[root@centos7 ~]#cat /var/www/html/ks/centos7.3.cfg 
......
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical               注:這里改為text即為安裝時為字符界面安裝
firstboot --disable
# SELinux configuration
selinux --disabled
......

PXE自動化安裝系統部署,解決了長時間的消耗。方便企業公司自動化安裝系統。

如有不足請多多指教?。。?/p>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容