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自动化安装系统部署,解决了长时间的消耗。方便企业公司自动化安装系统。

如有不足请多多指教!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容