PXE简介
PXE(Preboot eXecution Environment)是一种在计算机启动时使用网络接口从远程服务器获取操作系统安装和启动信息的技术。通过PXE,计算机可以从局域网中的PXE服务器上下载操作系统安装文件,并进行自动化的操作系统部署或故障排除。由Intel公司开发的PXE网络引导技术工作在Client/Server模式,可以同时装配多台机器,安装系统、配置各种服务,同时不需要光盘、U 盘等安装介质实现远程连接。
kickstart自动安装脚本的作用
在企业中安装多台操作系统时面临的问题
当安装Linux操作系统时,安装过程会需要回答很多关于设定的问题,这些问题必须手动选择,否则无法进行安装。当只安装1台Linux系统,手动选择设定工作量比较轻松;当安装多台Linux,这些设定需要重复多次,这些重复动作是效率低下的操作
如何解决以上问题?
用文件来记录所有安装过程中问题的答案,并让所有需要安装的主机自动读取
以上解决方案中记录系统安装过程中所有问题答案的文件叫kickstart脚本
实验环境:
- RHEL7主机
- 开启主机图形
- 配置网络可用
- 关闭VMware DHCP功能
- 软件仓库能正常工作
如果安装的时候安装了图形界面则输入init 5
将图形界面打开;若是没有安装图形界面,则可以使用yum install group "Server with GUI" -y
命令下载安装图形界面。
/root/anaconda-ks.cfg
此文件是在系统安装好之后自动生成的,这个文件记录了系统在安装过程中的所有设定。
- 安装图形化生成kickstart自动安装脚本的工具
[root@www ~]# yum install system-config-kickstart -y
- 启动图形制作工具
[root@www ~]# system-config-kickstart
- 按照图示更改配置
安装源选择HTTP通过网络分享安装源,现在我们没有网络分享安装源,所以需要自己搭建
- 下载httpd,并启动
[root@www ~]# yum install httpd -y [root@www ~]# systemctl enable --now httpd
- 创建一个符号链接(软链接),在
/var/www/html/
目录下创建一个指向/rhel7/
目录的软链接。通过创建这个软链接,当访问/var/www/html/
时,实际上会访问到/rhel7/
的内容[root@www ~]# ln -s /rhel7/ /var/www/html/ [root@www ~]# cd /var/www/html/ [root@www html]# ls rhel7
根据自己的网卡配置,图示为ens33,而我自己的是eth0。
上图为安装后运行的脚本,可以根据自己的需要写运行脚本。
我修改了文件名称为ks1.cfg
4. 编辑ks1.cfg文件
[root@www ~]# vim ks1.cfg
修改完检查是否有语法错误
[root@www ~]# ksvalidator ks.cfg
- 将此自动化安装脚本共享出去
[root@www ~]# cp ks1.cfg /var/www/html/
进入网页进行测试
6. 安装并搭建DHCP服务
安装DHCP服务器为其他服务器提供分配ip的功能
[root@www ~]# yum install dhcpd -y
# 生成配置文件
[root@www ~]# \cp -f /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf# 编写/etc/dhcp/dhcpd.conf配置文件
[root@www ~]# cat /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
## option definitions common to all supported networks...
option domain-name "wwwl.org"; #公司域名
option domain-name-servers 114.114.114.114;#对外分发的dns地址default-lease-time 600;
max-lease-time 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 10.152.187.0 netmask 255.255.255.0 {
#}# This is a very basic subnet declaration.subnet 172.25.254.0 netmask 255.255.255.0 {range 172.25.254.30 172.25.254.40;#地址池option routers 172.25.254.2;#网关next-server 172.25.254.133;#下一个服务器filename "pxelinux.0";#在next-server上读取的文件
}#重启DHCP
[root@www ~]# systemctl restart dhcpd
[root@www ~]# systemctl enable --now dhcpd
- 测试安装
此处的网址根据自己的写,我配置的为ks=http://172.25.254.133/ks1.cfg
8. 搭建PXE网络安装环境实现服务器自动部署
# 环境需要最基本的程序
[root@www ~]# yum install syslinux.x86
# 共享pxelinux.0数据文件的网络服务
[root@www ~]# yum install tftp-server.x86_64 -y
# tftp服务自启并立即启动
[root@www ~]# systemctl enable --now tftp
# 将镜像中引导 Linux 系统的文件复制到该目录下
[root@www ~]# cp -p /rhel7/isolinux/* /var/lib/tftpboot/
# pxelinux 的主要引导文件复制到这里
[root@www ~]# cp -p /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@www ~]# cd /var/lib/tftpboot/
[root@www tftpboot]# mkdir pxelinux.cfg
# 将该配置文件复制到pxelinux.cfg/default中
[root@www tftpboot]# cp isolinux.cfg pxelinux.cfg/default[root@www tftpboot]# vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 30 #设置超时时间3sdisplay boot.msg# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Red Hat Enterprise Linux 7.9
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13# Border Area
menu color border * #00000000 #00000000 none# Selected item
menu color sel 0 #ffffffff #00000000 none# Title bar
menu color title 0 #ff7ba3d0 #00000000 none# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none# Help text
menu color help 0 #ffffffff #00000000 none# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.menu tabmsg Press Tab for full configuration options on menu items.menu separator # insert an empty line
menu separator # insert an empty linelabel linuxmenu label ^Install Red Hat Enterprise Linux hahahahahaah #设置安装时显示的字符menu default #安装时默认选择这个选项kernel vmlinuzappend initrd=initrd.img repo=http://172.25.254.133/rhel7 ks=http://172.25.254.133/ks1.cfg quiet #静默label checkmenu label Test this ^media & install Red Hat Enterprise Linux 7.9kernel vmlinuzappend initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rd.live.check quietmenu separator # insert an empty line# utilities submenu
menu begin ^Troubleshootingmenu title Troubleshootinglabel vesamenu indent count 5menu label Install Red Hat Enterprise Linux 7.9 in ^basic graphics modetext helpTry this option out if you're having trouble installingRed Hat Enterprise Linux 7.9.endtextkernel vmlinuzappend initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 xdriver=vesa nomodeset quietlabel rescuemenu indent count 5menu label ^Rescue a Red Hat Enterprise Linux systemtext helpIf the system will not boot, this lets you access filesand edit config files to try to get it booting again.endtextkernel vmlinuzappend initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rescue quietlabel memtestmenu label Run a ^memory testtext helpIf your system is having issues, a problem with yoursystem's memory may be the cause. Use this utility tosee if the memory is working correctly.endtextkernel memtestmenu separator # insert an empty linelabel localmenu label Boot from ^local drivelocalboot 0xffffmenu separator # insert an empty line
menu separator # insert an empty linelabel returntomainmenu label Return to ^main menumenu exitmenu end
新建虚拟机进行安装
选择打开电源时进入固件
默认会选择第一个,不用自己手动选,等待3s自动安装
安装完成后记得关机再选择打开电源时进入固件,将启动方式更改成默认的方式:Hard Drive 即硬盘启动方式。不然会进入无限安装