Linux中的无人值守安装脚本Kickstart

目录

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

在企业中安装多台操作系统时面临的问题

如何解决以上问题?

二.实验环境

三.kickstart自动安装脚本的制作 

通过模板生成kickstart文件

1.安装图形化生成kickstart自动安装脚本的工具

2.图形化工具配置流程

 3.配置文件详细介绍

 4.将编好的文件共享出去

5.在环境内搭建dhcp

6.测试安装 

 7.搭建pxe网络安装环境实现服务器自动部署

 启动tftp服务

 将光驱系统安装界面的所有内容全部考到tftp的共享目录下面

 修改dhcp配置文件

重启dhcp服务  

修改/var/lib/tftpboot/pxelinux.cfg/default配置文件

测试安装


 .kickstart自动安装脚本的作用 

在企业中安装多台操作系统时面临的问题

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

如何解决以上问题

用文件来记录所有安装过程中问题的答案 并让所有需要安装的主机自动读取
kickstart 作用
以上解决方案中记录系统安装过程中所有问题答案的文件叫 kickstart 脚本

二.实验环境

1.准备一台带图形化界面的centos7或者rhel7操作系统设备,init 5可以打开图形化界面,

[root@centos-pxe yum.repos.d]# hostnamectl 
   Static hostname: centos-pxe
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e0ddc8253f6c49c789414991b782f7af
           Boot ID: 2006210e125e4ab9acb15f4deb7d49ca
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

2.关闭防火墙和selinux

[root@centos-pxe ~]# systemctl stop firewalld.service 
[root@centos-pxe ~]# setenforce 0
[root@centos-pxe ~]# getenforce 
Permissive

3.开启httpd服务

[root@centos-pxe ~]# systemctl enable httpd.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

# 开启http服务并且开机自启

4.软件仓库可以正常使用

[root@centos-pxe yum.repos.d]# mkdir /centos7
[root@centos-pxe yum.repos.d]# mount /dev/sr0 /centos7/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos-pxe yum.repos.d]# vim centos7.repo
[root@centos-pxe yum.repos.d]# cat centos7.repo 
[centos7]
name=centos7
baseurl=file:///centos7
gpgcheck=0

[root@centos-pxe yum.repos.d]# yum makecache 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
centos7                                                   | 3.6 kB  00:00:00     
(1/4): centos7/group_gz                                   | 153 kB  00:00:00     
(2/4): centos7/filelists_db                               | 3.3 MB  00:00:00     
(3/4): centos7/primary_db                                 | 3.3 MB  00:00:00     
(4/4): centos7/other_db                                   | 1.3 MB  00:00:00     
元数据缓存已建立
# 我配置的是本地yum仓库

 5.配置网络

[root@centos-pxe ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.147  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::93fe:ed96:289:28db  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:93:13:3e  txqueuelen 1000  (Ethernet)
        RX packets 39289  bytes 27913680 (26.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23370  bytes 1452053 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

6 关闭vmware dhcp功能

注: 该主机使用的是nat网络,在后续制作过程中需要使用到dhcp,所以需要将虚拟网络编辑器中nat模式下的dhcp服务关闭。(不关闭的话,如果其他主机在发送ip请求时它会相应,此时如果在系统内再装一个dhcp的话,就不知道到底是谁在将ip下发出去了)

三.kickstart自动安装脚本的制作 

rhel7 系统中提供图形的 kickstart 制作方式
rhel8 中已经把图形的工具取消 并添加到 rhn 网络中
rhel8 中如果无法通过 rhn 网络制作 kickstart 可以使用模板生成

通过模板生成kickstart文件

在已经装好的centos7 / root / anaconda - ks.cfg 就是安装当前系统时回答的所有问题的答案
生成的 kickstart(例如当时安装系统你划分的分区,root密码,安装软件,时区等等一些问题) 此文件为 kickstart 模板
[root@centos-pxe ~]# ll
总用量 8
-rw-------. 1 root root 1560 3月  10 12:01 anaconda-ks.cfg
# 此文件是在系统安装好后自动生成的, 这个文件记录了系统在安装过程中的所有设定
-rw-r--r--. 1 root root 1591 3月  10 12:02 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 8月   5 12:23 公共
drwxr-xr-x. 2 root root    6 8月   5 12:23 模板
drwxr-xr-x. 2 root root    6 8月   5 12:23 视频
drwxr-xr-x. 2 root root    6 8月   5 12:23 图片
drwxr-xr-x. 2 root root    6 8月   5 12:23 文档
drwxr-xr-x. 2 root root    6 8月   5 12:23 下载
drwxr-xr-x. 2 root root    6 8月   5 12:23 音乐
drwxr-xr-x. 2 root root    6 8月   5 12:23 桌面
[root@centos-pxe ~]# pwd
/root

1.安装图形化生成kickstart自动安装脚本的工具

[root@centos-pxe ~]# yum install system-config-kickstart -y
# 安装 
[root@centos-pxe ~]# system-config-kickstart 
# 启动图形制作工具

2.图形化工具配置流程

[root@centos-pxe ~]# ln -s /centos7/ /var/www/html/
[root@centos-pxe ~]# cd /var/www/html/
[root@centos-pxe html]# ll
总用量 0
lrwxrwxrwx. 1 root root 9 8月   5 13:44 centos7 -> /centos7/

# 创建一个名为 centos7 的符号链接在 /var/www/html/ 目录下,这个链接指向 /centos7/ 目录。

 

 注:本次实验采用http的方式,也可以使用其他的方式,http服务器就是该主机的ip,目录就是我环境准备挂载光盘的目录,并且在/var/www/html/下做了软连接,浏览器通过输入ip和/centos7,可以实现正常访问。

 

 注:可选可不选

注:如果是以上情况,可以后续在脚本里面添加软件包

 

注:在系统安装好后自动下载创建的内容,可写可不写,后续也可在脚本里面添加 

 

 

 3.配置文件详细介绍

[root@centos-pxe ~]# vim ks.cfg 


#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$l5ipMk3f$qh24rI2QriIAbFH7bv4671  # 密码是加密的
# System language

lang zh_CN   # 中文语言
# System authorization information
auth  --useshadow  --passalgo=sha512   # 加密方式
# Use text mode install
text  # 安装过程中不开图形化界面
# SELinux configuration
selinux --disabled    # selinux状态
# Do not configure the X Window System
skipx


# Firewall configuration
firewall --disabled  # 防火墙状态
# Network information
network  --bootproto=dhcp --device=ens33  #网络设定
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://192.168.10.147/centos7"  # 安装源
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=1024       #/boot分区大小为1024M
part swap --fstype="swap" --size=512        #swap分区大小为512M
part / --fstype="xfs" --grow --size=1       # swap分区大小为500M

%packages
@base     #安装软件组base
httpd     #安装单个软件httpd
%end

%post                  # 系统安装后自动执行的脚本
mkdir -p /centos7

echo mount /dev/sr0 /centos7 >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

cat > /etc/yum.repos.d/centos7.repo<<EOF

[centos7]
name=centos7
baseurl=file:///centos7
gpgcheck=0
EOF

yum insatall gcc -y
%end

修改完配置文件后检测语法是否正确

[root@centos-pxe ~]# ksvalidator ks.cfg 

 4.将编好的文件共享出去

通过http的方式将文件共享出现,需要安装的设备通过http来读取文件,实现自动安装

[root@centos-pxe ~]# cp /root/ks.cfg /var/www/html/
# 将文件拷贝到apache的共享目录下
[root@centos-pxe ~]# ll /var/www/html/
总用量 4
lrwxrwxrwx. 1 root root    9 8月   5 13:44 centos7 -> /centos7/
-rw-r--r--. 1 root root 1240 8月   5 14:46 ks.cfg

5.在环境内搭建dhcp

为其他服务器提供分配ip的功能,前提是其他的网卡必须设定成dhcp模式

让被安装的主机可以获得ip来访问网络资源及kickstart文件

[root@centos-pxe ~]# yum install dhcp -y


[root@centos-pxe ~]# rpm -qc dhcp   # 查看dhcp的配置文件有那些
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases
[root@centos-pxe ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@centos-pxe ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? ^Z
[1]+  已停止               cp -i /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
# dhcp配置文件为空,根据提示将模板复制到配置文件内,即用命令来生成配置文件
[root@centos-pxe ~]# \cp  -f /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 

补充: 

  • cp是复制文件或目录的命令。
  • \cp并不是一个新的命令,而是cp命令前加了一个转义字符\,用于在某些特殊情况下(如绕过别名)直接调用cp命令的原始行为。

注: 因为是多个文件拷贝到统一文件,所有与无用的配置文件可以干掉。

从37行后的内容可以删掉, 并且注释27,28行的内容 

配置dhcp配置文件

[root@centos-pxe ~]# vim /etc/dhcp/dhcpd.conf 
# dhcpd.conf
  2 #
  3 # Sample configuration file for ISC dhcpd
  4 #
  5 
  6 # option definitions common to all supported networks...
  7 option domain-name "example.org";   # 公司名称
  8 option domain-name-servers 114.114.114.114; 
  9 # dhcp工作模式的网络主机获得的dns的地址,这就是我们往外分发网络资源带的dns的地址

 10 default-lease-time 600;    # 默认租约期,就是我个他发个ip他可以使用的时间,默认是600小时
 11 max-lease-time 7200;       # 最长租约期, 默认是7200小时
 12 
 13 # Use this to enble / disable dynamic dns updates globally.
 14 #ddns-update-style none;
 15 
 16 # If this DHCP server is the official DHCP server for the local
 17 # network, the authoritative directive should be uncommented.
 18 #authoritative;
 19 
 20 # Use this to send dhcp log messages to a different log file (you also
 21 # have to hack syslog.conf to complete the redirection).
 22 log-facility local7;   # 定义我们的log的日志格式,为loacal7
 23 
 24 # No service will be given on this subnet, but declaring it helps the 
 25 # DHCP server to understand the network topology.
 26 
 27 #subnet 10.152.187.0 netmask 255.255.255.0 {
 28 #}
 29 
 30 # This is a very basic subnet declaration.
 31 # 网段
 32 subnet 192.168.10.0 netmask 255.255.255.0 {
 33   range 192.168.10.150  192.168.10.160;         # 分配ip的范围
 34   option routers 192.168.10.2;  # 网关路由
 35 }

 重启dhcp服务

[root@centos-pxe ~]# systemctl restart dhcpd

6.测试安装 

新建一个虚拟机,正常操作,在系统安装基面选择第二给,按tab键,在末行输入,ks=http://192.168.10.147/ks.cfg  然后按下enter键,之后全程自动执行。

 

完成,root密码就是在图形化界面你自己设置的密码

 7.搭建pxe网络安装环境实现服务器自动部署

[root@centos-pxe ~]# yum search pxelinux
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
================================ 匹配:pxelinux =================================
syslinux.x86_64 : Simple kernel loader which boots from a FAT filesystem
syslinux-extlinux.x86_64 : The EXTLINUX bootloader, for booting the local system.
syslinux-tftpboot.noarch : SYSLINUX modules in /var/lib/tftpboot, available for
                         : network booting

# 搜索与 pxelinux 相关的软件包

[root@centos-pxe ~]# yum install syslinux.x86_64 -y   # 环境所需要的最基本程序
[root@centos-pxe ~]# yum install tftp-server.x86_64 -y # 共享pxelinux.0数据文件的网络服务
 
 启动tftp服务
[root@centos-pxe ~]# rpm -ql tftp-server  # 找到tptp服务的启动程序
/etc/xinetd.d/tftp
/usr/lib/systemd/system/tftp.service  #启动程序
/usr/lib/systemd/system/tftp.socket
/usr/sbin/in.tftpd
/usr/share/doc/tftp-server-5.2
/usr/share/doc/tftp-server-5.2/CHANGES
/usr/share/doc/tftp-server-5.2/README
/usr/share/doc/tftp-server-5.2/README.security
/usr/share/man/man8/in.tftpd.8.gzf
/usr/share/man/man8/tftpd.8.gz
/var/lib/tftpboot    # tftp的共享目录


[root@centos-pxe ~]# systemctl enable --now tftp
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
 将光驱系统安装界面的所有内容全部考到tftp的共享目录下面
[root@centos-pxe tftpboot]# df -h
文件系统                           容量  已用  可用 已用% 挂载点
devtmpfs                           470M     0  470M    0% /dev
tmpfs                              487M     0  487M    0% /dev/shm
tmpfs                              487M  8.5M  478M    2% /run
tmpfs                              487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos_centos--7-root   16G  5.2G   11G   33% /
/dev/sda1                          397M  160M  238M   41% /boot
tmpfs                               98M   64K   98M    1% /run/user/0
/dev/sr0                           4.4G  4.4G     0  100% /centos7


[root@centos-pxe ~]# cp /centos7/isolinux/* /var/lib/tftpboot/   
isolinux/目录包含了启动过程中所需的文件,比如内核映像(vmlinuz)、初始RAM磁盘(initrd或initramfs)、配置文件(如isolinux.cfg)以及其他必要的启动支持文件。
[root@centos-pxe ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
,该文件是一个预启动执行环境(PXE)的启动加载器,也称为网络引导程序(NBP)
[root@centos-pxe ~]# cd /var/lib/tftpboot/
[root@centos-pxe tftpboot]# ls
boot.cat  grub.conf   isolinux.bin  memtest     splash.png  vesamenu.c32
boot.msg  initrd.img  isolinux.cfg  pxelinux.0  TRANS.TBL   vmlinuz
[root@centos-pxe tftpboot]# mkdir pxelinux.cfg    # 建立数据目录,数据文件
[root@centos-pxe tftpboot]# cp isolinux.cfg pxelinux.cfg/default

注:

  1. 启动加载器:在网络启动过程中,PXE 客户端(即待安装操作系统的计算机)会向网络中的 DHCP 服务器请求 IP 地址和启动文件的位置。DHCP 服务器会响应一个包含 next-server(TFTP 服务器地址)和 filename(通常是 pxelinux.0)的响应。然后,PXE 客户端会从指定的 TFTP 服务器下载 pxelinux.0 文件并执行它。

  2. 配置和执行:一旦 pxelinux.0 被加载到内存中并执行,它会查找 TFTP 服务器上的配置文件(通常位于 /var/lib/tftpboot/pxelinux.cfg/ 目录下),并根据配置文件的指令来加载内核映像、初始 RAM 磁盘和其他必要的文件,以启动操作系统安装程序。

 修改dhcp配置文件
[root@centos-pxe ~]# cat /etc/dhcp/dhcpd.conf 
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 114.114.114.114;

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 192.168.10.0 netmask 255.255.255.0 {
  range 192.168.10.150  192.168.10.160;
  option routers 192.168.10.2;
  next-server 192.168.10.147;   #指向下面的服务器,就是tftp所在的主机。
  filename "pxelinux.0";        # 在next-server上你要读取那个文件
}

重启dhcp服务  
[root@centos-pxe ~]# systemctl restart dhcpd
修改/var/lib/tftpboot/pxelinux.cfg/default配置文件

注: 第63行 menu default 是默认选择,默认是第二个,可以修改成第一个

        第65行,安装资源,将img后的内容删除,修改为http共享内容,写上其地址,以及ks=加       http共享目录下的资源  ,可以选择自动进入时间,默认是60s

测试安装

为了看到更加清晰,可以将原先的镜像直接移除

打开电源时进入固体 

 选择网络启动

 

扩充: linux下的int0到6的知识介绍

一. init是Linux系统操作中不可缺少的程序之一。
所谓的init进程,它是一个由内核启动的用户级进程。

内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。

所以,init始终是第一个进程(其进程编号始终为1)。

内核会在过去曾使用过init的几个地方查找它,它的正确位置(对Linux系统来说是/sbin/init。

如果内核找不到init,它就会试着运行/bin/sh,如果运行失败,系统的启动也会失败。                       

0:停机或者关机(千万不能将initdefault设置为0)

init 0,对应的系统会运行,/etc/rc.d/rc0.d里指定的程序

开机会执行的两个进程是killall和halt,这两个都表示为终止进程。

1:单用户模式,只root用户进行维护

init 1,对应的系统会运行,/etc/rc.d/rc1.d里指定的程序。

这个级别启动的服务有三个,udev、lvm相关的和single(单用户模式的服务)。

故此级别是单用户模式,只有root能用,不支持其他用户。

2:多用户模式,不能使用NFS(Net File System)

init 2,对应的系统会运行,/etc/rc.d/rc2.d里指定的程序。

3:完全多用户模式(标准的运行级别)

init 3 , 对应的系统运行/etc/rc.d/rc3.d

这个级别nfs服务是开启的,被成为完全多用户模式。

4:安全模式

init 4,对应的系统会运行,/etc/rc.d/rc4.d里指定的程序。

5:图形化(即图形界面)

init 5,对应的系统会运行,/etc/rc.d/rc5.d里指定的程序。

6:重启(千万不要把initdefault设置为6)

init 6,对应的系统会运行,/etc/rc.d/rc6.d里指定的程序。

这个级别里,只有两个服务,一个为killall,一个是reboot,即,关闭现在的系统,重启。故此级别是重启。

不同的系统版本,可能里面的文件会不同,如果要查看,可以通过ll来看,其实他们都是软连接。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值