PXE实现自动批量安装部署操作系统

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此文件是在系统安装好之后自动生成的,这个文件记录了系统在安装过程中的所有设定。

  1. 安装图形化生成kickstart自动安装脚本的工具
[root@www ~]# yum install system-config-kickstart -y
  1. 启动图形制作工具
[root@www ~]# system-config-kickstart
  1. 按照图示更改配置
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    安装源选择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 
  1. 将此自动化安装脚本共享出去
[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
  1. 测试安装
    在这里插入图片描述
    此处的网址根据自己的写,我配置的为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 #设置超时时间3s

display 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 line

label linux
  menu label ^Install Red Hat Enterprise Linux hahahahahaah #设置安装时显示的字符
  menu default #安装时默认选择这个选项
  kernel vmlinuz
  append initrd=initrd.img repo=http://172.25.254.133/rhel7 ks=http://172.25.254.133/ks1.cfg quiet #静默

label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.9
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install Red Hat Enterprise Linux 7.9 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	Red Hat Enterprise Linux 7.9.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a Red Hat Enterprise Linux system
  text help
	If the system will not boot, this lets you access files
	and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
	If your system is having issues, a problem with your
	system's memory may be the cause. Use this utility to
	see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

新建虚拟机进行安装

在这里插入图片描述
选择打开电源时进入固件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
默认会选择第一个,不用自己手动选,等待3s自动安装
在这里插入图片描述
安装完成后记得关机再选择打开电源时进入固件,将启动方式更改成默认的方式:Hard Drive 即硬盘启动方式。不然会进入无限安装
在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

acro_09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值