RH124(14)----Linux中无人职守安装脚本及PXE环境搭建

一、 kickstart 自动安装脚本的作用

  • #在企业中安装多台操作系统时面临的问题#
    当安装 Linux 操作系统时,安装过程会需要回答很多关于设定的问题,这些问题必须手动选择,否则无法进行安装,当只安装 1 台 Linux 系统,手动选择设定工作量比较轻松,当安装多台 Linux,这些设定需要重复多次,这些重复动作是效率底下的操作。

  • #如何解决以上问题?#
    用文件来记录所有安装过程中问题的答案,并让所有需要安装的主机自动读取。

  • #kickstart 作用#
    以上解决方案中记录系统安装过程中所有问题答案的文件叫 kickstart 脚本

二、实验环境

1、配置实验环境

  • 火墙,selinux 关闭
  • httpd 服务开启
  • 配置软件仓库能正常工作
[root@localhost dev]# mount /dev/cdrom /var/www/html/westos
[root@localhost westos]# vim /etc/yum.repos.d/westos.repo
[AppStream]
name=AppStream
baseurl=http://192.168.0.6/rhel8.0/AppStream
gpgcheck=0

[BaseOS]
name=BaseOS
baseurl=http://192.168.0.6/rhel8.0/BaseOS
gpgcheck=0
[root@localhost westos]# dnf install  httpd  pykickstart opensll -y
[root@localhost html]# systemctl enable --now httpd
[root@localhost html]# systemctl disable --now firewalld
[root@localhost html]# setenforce 0 ##selinux 调整为警告模式

2、用模板自作 kickstart

[root@localhost yum.repos.d]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# cp /root/anaconda-ks.cfg  /var/www/html/westos.cfg
[root@localhost html]# ls
westos.cfg
[root@localhost html]# vim westos.cfg
[root@localhost html]# openssl passwd -6 ##生成密码加密字符串复制
[root@localhost html]# ksvalidator  westos.cfg  ### 检测westos.cfg语法
[root@localhost html]# chmod 644 westos.cfg

#启动192.168.0.206
#安装界面按tab键  输入ks=http://192.168.0.106/westos.cfg

文件编辑内容

#version=RHEL8
#ignoredisk --only-use=vda  ## 1 只是用我们系统中的第一快硬盘
# Partition clearing information
#clearpart --none --initlabel
clearpart --all --initlabel   ## 2 把硬盘内东西删掉 
# Use graphical install
#graphical
text                      ## 3 安装过程不开图形
# Use CDROM installation media
# cdrom                    
url --url=http://192.168.0.6/rhel8.0  ## 4 系统安装资源
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information        
#network  --bootproto=dhcp --device=enp1s0 --onboot=off --ipv6=auto --no-activate
network  --bootproto=dhcp --onboot=on  ## 5 网卡设定
#network  --hostname=localhost.localdomain   ## 6 主机名不设定
#repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
repo --name="AppStream" --baseurl=http://192.168.0.6/rhel8.0/AppStream           ## 7 
# Root password
rootpw --iscrypted $6$zZ8631BwW20.E.ey$Pkf757edQJCVGmtsPNEHLn/XMe1yAUzEEYJjU2TBYHfWEox9G60lm/JNdbrf5H7Cub/gTyGNrgqPMuM2yzLci0  
### 更改8 密码设定  openssl passwd -6 ##生成密码加密字符串复制
# X Window System configuration information
#xconfig  --startxonboot        ## 9 安装完成后开机不启动图形
# Run the Setup Agent on first boot
#firstboot --enable
firstboot --disable              ## 10 首次启动初始化禁止
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# Disk partitioning information   ## 11
part / --fstype="xfs" --grow  --size=1
part /boot --fstype="xfs"  --size=500
part swap --fstype="swap"  --size=500

reboot    ## 14 重启
%packages      ## 12 
#@^graphical-server-environment
@base       ## 安装软件组
lftp  ##安装软件包
%end

%post ## 13
touch /mnt/westosfile{1..10}
%end

3、搭建 dhcpd 服务器

目的:让被安装的主机可以获得 ip 来访问网络资源及 kickstart 文件
整体搭建步骤:下载dhcpd服务,修改配置文件,启动该服务

[root@localhost westos]# dnf install  httpd  pykickstart opensll -y
[root@localhost html]# systemctl enable --now httpd

4、测试

注意:准备好网络桥接
方法一:在已有虚拟机的关闭模式下添加光盘,将光盘启动设置为第一启动项,应用,启动,之后在系统安装界面选择:

Install Red Hat Enterprise Linux 8.0.0    <---- <TAB>键
ks=http://192.168.0.186/westos.cfg       <----<ENTER>键
查看效果
关闭虚拟机,选择硬盘为第一启动项,启动,会看到进入非图形界面,试验成功!

在这里插入图片描述
在这里插入图片描述

方法二:新建虚拟机时选择网络方式创建,选择安装源及自己编写的cfg文件路径,安装,查看效果,进入非图形界面,试验成功!
在这里插入图片描述

在这里插入图片描述

三、PXE环境搭建

**PXE介绍:**预启动执行环境(Preboot eXecution Environment,PXE)也被称为预执行环境,提供了一种使用网络接口(Network Interface)启动计算机的机制。这种机制让计算机的启动可以不依赖本地数据存储设备(如硬盘)或本地已安装的操作系统。

**验证方法:**在已经装好的虚拟机的关闭模式下选择网卡为第一启动项,启动虚拟机,能够正常进入到无图形模式,试验成功!

1、配置实验环境

[root@localhost var]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
redhat.repo
[root@localhost yum.repos.d]# vim westos.repo
[AppStream]
name=AppStream
baseurl=http://192.168.0.6/rhel8.0/AppStream
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=http://192.168.0.6/rhel8.0/BaseOS
gpgcheck=0
            
[root@localhost yum.repos.d]# dnf clean all
[root@localhost yum.repos.d]# systemctl disable --now firewalld
[root@localhost yum.repos.d]# vim /etc/sysconfig/selinux 
SELINUX=disabled

[root@localhost yum.repos.d]# setenforce 0
[root@localhost yum.repos.d]# getenforce
Permissive

2、搭建dhcp服务器及编写脚本

电脑向外发送索要ip的请求,dhcp服务器接受到请求后再分发ip网关dns的同时,又被包含了一个要读取相应文件的指向,这就是所说的PXE网络环境的工作模型,也是为什么会读取真机网络的原因。第一步,搭建dhcp;第二步,配置文件。

[root@localhost yum.repos.d]# cat /etc/dhcp/dhcpd.conf
[root@localhost yum.repos.d]# cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcp.conf
[root@localhost yum.repos.d]# vim /etc/dhcp/dhcpd.conf
## 改名字 删除27 28 35以后的
subnet 192.168.0.0 netmask 255.255.255.0 {
  range 192.168.0.10 192.168.0.60;
  option routers 192.168.0.6;
  filename "pxelinux.0";
  next-server 192.168.0.106;
}
[root@localhost yum.repos.d]# systemctl restart dhcpd
[root@localhost yum.repos.d]# systemctl status dhcpd

dhcp服务器向外分配ip网关dns的同时,告诉分机应该读取pxelinux0文件,pxelinux0文件内让读取pxelinux.cfg/default文件,在default文件内配置读取下一步的文件。

[root@localhost westos]# dnf install syslinux tftp-server httpd -y
[root@localhost dhcp]# dnf install syslinux-nonlinux-6.04-4.el8.noarch -y
[root@localhost dhcp]# systemctl enable --now httpd
[root@localhost yum.repos.d]# systemctl disable --now firewalld
[root@localhost dhcp]# systemctl enable --now tftp.service

[root@localhost dhcp]# less /usr/share/doc/syslinux/pxelinux.txt

[root@localhost dhcp]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/
[root@localhost dhcp]# cp /westos/isolinux/*  /var/lib/tftpboot
[root@localhost dhcp]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# mkdir pxelinux.cfg
[root@localhost tftpboot]# cd pxelinux.cfg
[root@localhost pxelinux.cfg]# ls
[root@localhost pxelinux.cfg]# touch default
[root@localhost pxelinux.cfg]# ls
default
[root@localhost pxelinux.cfg]# cp /var/www/html/rhel8.0/isolinux/isolinux.cfg  default
[root@localhost pxelinux.cfg]# vim default
menu title hahahahaha

label linux
  menu label ^Install Red Hat Enterprise Linux 8.2
  kernel vmlinuz
  append initrd=initrd.img repo=http://192.168.0.6/rhel8.0 ks=http://192.168.0.106/westos.cfg  quiet

3、验证结果

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值