Linux—教你如何高效的批量网络装机


引言

大规模的 Linux 应用环境中,服务器往往并不配备光驱设备,在这样的情况下,如何为数十乃至上百台服务器裸机快速安装系统呢?下面我来介绍一下,通过 PXE 技术远程安装系统,并且实现无人值守安装的操作。


一、部署 PXE 远程安装服务

PXE是由 Intel 公司开发的网络引导技术,工作在 Client/Server 模式,允许客户机通过网络从远程服务器下载引导镜像,并加载安装文件或者整个操作系统。

1. 批量部署的前提条件

  • 客户机的网卡支持 PXE协议(集成BOOTROM芯片),且主板支持网络引导。

  • 网络中有一台 DHCP服务器以便为客户机自动分配地址、指定引导文件位置。

  • 服务器通过 TFTP (Trivial File Transfer Protocol,简单文件传输协议)提供引导镜像文件的下载。

其中,第一个条件实际上是硬件要求,目前绝大多数服务器和大多数 PC 都能够提供此支持,只需在BIOS 设置中允许从 Network 或 LAN 启动即可。

2. 批量部署优点

  • 规模化:同时装配多台服务器

  • 自动化:安装系统、配置各种服务

  • 远程实现:不需要光盘、U盘等安装介质

3. PXE服务的构建过程

  • PXE服务器和客户机的工作过程如下所述:

① PXE客户机发出DHCP请求,向DHCP服务器申请IP地址。

② DHCP服务器响应PXE客户机的请求,自动从IP地址池中分配一个IP地址给PXE客户机,并且告知PXE客户机TFTP服务器的IP地址和PXE引导程序文件pxelinux.0,默认在TFTP共享目录/var/lib/tftpboot/下。

③ PXE客户机向TFTP服务器发起获取pxelinux.0引导程序文件的请求。

④ TFTP服务器响应PXE客户机的请求,将其共享的pxelinux.0文件传输给PXE客户机。

⑤ PXE客户机通过网络来启动到系统安装主界面。

⑥ PXE客户机向文件共享服务器(ftp、http、nfs等)发起获取centos或windows系统安装文件的请求。

⑦ 文件共享服务响应PXE客户机的请求,将共享的系统安装文件传输给PXE客户机。

⑧ PXE客户机进入到安装提示向导界面,用户需要手动来完成系统安装的操作

4.搭建 PXE 远程安装服务器

4.1 添加网卡并配置网卡信息

在这里插入图片描述

[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost /etc/sysconfig/network-scripts]#ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.128  netmask 255.255.255.0  broadcast 192.168.8.255

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:d2:bb:cf  txqueuelen 1000  (Ethernet)

[root@localhost /etc/sysconfig/network-scripts]#cp ifcfg-ens33 ifcfg-ens37
[root@localhost /etc/sysconfig/network-scripts]#vim ifcfg-ens37
  1 TYPE=Ethernet
  2 PROXY_METHOD=none
  3 BROWSER_ONLY=no
  4 BOOTPROTO=static
  5 DEFROUTE=yes
  6 IPV4_FAILURE_FATAL=no
  7 IPV6INIT=yes
  8 IPV6_AUTOCONF=yes
  9 IPV6_DEFROUTE=yes
 10 IPV6_FAILURE_FATAL=no
 11 IPV6_ADDR_GEN_MODE=stable-privacy
 12 NAME=ens37
 13 DEVICE=ens37
 14 ONBOOT=yes
 15 IPADDR=192.168.100.100 

16 NETMASK=255.255.255.0
 17 GATEWAY=192.168.100.1

wq保存退出

[root@localhost /etc/sysconfig/network-scripts]#systemctl restart network
[root@localhost /etc/sysconfig/network-scripts]#ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.128  netmask 255.255.255.0  broadcast 192.168.8.255
                                 ......      
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.100  netmask 255.255.255.0  broadcast 192.168.100.255
                                 ......

4.2 安装并启用 TFTP 服务

[root@localhost /etc/sysconfig/network-scripts]#yum install tftp-server.x86_64 -y
[root@localhost /etc/sysconfig/network-scripts]#vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = no     #no表示客户机可以多台一起连接,yes表示客户机只能一台一台连接
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot  #指定TFTP根目录,-c允许上传
        disable                 = no
        per_source              = 11                    #限制主机最大连接数,防止某个主机独占服务
        cps                     = 100 2                 #表示服务器最多启动100个连接,达到会停2s
        flags                   = IPv4
}
~                                   
wq保存退出

启动并设置开机自启

[root@localhost /mnt/images/pxeboot]#systemctl start tftp.socket 
[root@localhost /mnt/images/pxeboot]#systemctl status tftp.socket 
● tftp.socket - Tftp Server Activation Socket
   Loaded: loaded (/usr/lib/systemd/system/tftp.socket; disabled; vendor preset: disabled)
   Active: active (listening) since 三 2021-09-01 23:04:40 CST; 7s ago
   Listen: [::]:69 (Datagram)

901 23:04:40 localhost.localdomain systemd[1]: Listening on Tftp Server Activation Socket.
901 23:04:40 localhost.localdomain systemd[1]: Starting Tftp Server Activation Socket.

[root@localhost /mnt/images/pxeboot]#systemctl enable tftp
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.

4.3 安装并启用 DHCP 服务

[root@localhost /etc/sysconfig/network-scripts]#yum install dhcp -y
[root@c7-1 network-scripts]# cp -rfp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhc
p/dhcpd.conf cp:是否覆盖"/etc/dhcp/dhcpd.conf"? yes
[root@c7-1 network-scripts]# vim /etc/dhcp/dhcpd.conf 

subnet 192.168.100.0 netmask 255.255.255.0 {        #配置网段
  range 192.168.100.40 192.168.100.50;              #配置地址池
  option routers 192.168.100.100;                   #配置网关
  next-server 192.168.100.100;                      #指定tftp服务器地址
  filename "pxelinux.0";                           #指定PXE引导程序的文件名
}
wq 保持退出

#开启DHCP服务并设置开机自启
[root@localhost /etc/sysconfig/network-scripts]#systemctl start dhcpd
[root@localhost /etc/sysconfig/network-scripts]#systemctl enable dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
[root@localhost /etc/sysconfig/network-scripts]#systemctl is-enabled dhcpd
enabled

4.4 准备 Linux 内核、初始化镜像文件

[root@localhost /etc/sysconfig/network-scripts]#mount /dev/sr0 /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost /etc/sysconfig/network-scripts]#df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   56G  3.7G   52G    7% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  9.1M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  172M  326M   35% /boot
tmpfs                    378M   40K  378M    1% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /mnt

复制Linux系统内核文件和初始化镜像文件

[root@localhost /etc/sysconfig/network-scripts]#cd /mnt/images/pxeboot/
[root@localhost /mnt/images/pxeboot]#ls
initrd.img  TRANS.TBL  vmlinuz
[root@localhost /mnt/images/pxeboot]#cp initrd.img vmlinuz /var/lib/tftpboot/
[root@localhost /mnt/images/pxeboot]#ls /var/lib/tftpboot/
initrd.img  vmlinuz

4.5 准备 PXE 引导程序

[root@localhost /mnt/images/pxeboot]#yum -y install syslinux
[root@localhost ~]#cd /mnt/images/pxeboot/
[root@localhost /mnt/images/pxeboot]#ls                      #查找PXE引导程序位置
initrd.img  TRANS.TBL  vmlinuz                               #拷贝到tftp的根目录下。系统引导文件
[root@localhost /mnt/images/pxeboot]#cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost /mnt/images/pxeboot]#ls /var/lib/tftpboot/
initrd.img  pxelinux.0  vmlinuz

4.6 安装 FTP 服务,准备 CentOS 7 安装源

[root@localhost /mnt/images/pxeboot]#yum install -y vsftpd
[root@localhost /mnt/images/pxeboot]#mkdir /var/ftp/centos7
[root@localhost /mnt/images/pxeboot]#cp -rf /mnt/* /var/ftp/centos7/

启动服务并设置开机自启

[root@localhost /mnt/images/pxeboot]#systemctl start vsftpd
[root@localhost /mnt/images/pxeboot]#systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

4.7 配置启动菜单文件

[root@localhost ~]# cd /var/lib/tftpboot
[root@localhost tftpboot]# ls
initrd.img  pxelinux.0  vmlinuz
[root@localhost tftpboot]# mkdir pxelinux.cfg
[root@localhost /var/lib/tftpboot]#vim pxelinux.cfg/default

default auto 
prompt 1 
label auto
      kernel vmlinuz 
      append initrd=initrd.img method=ftp://192.168.100.100/centos7    

label linux text    
      kernel vmlinuz 
      append text initrd=initrd.img method=ftp://192.168.100.100/centos7 

label linux rescue    
      kernel vmlinuz 
      append rescue initrd=initrd.img method=ftp://192.168.100.100/centos7

4.8 验证 PXE 网络安装

先关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled

然后直接点击启动

在这里插入图片描述

在字符界面提示 “boot:” 后直接按Enter键,将会自动通过网络下载安装文件,并进入默认的图形安装入口

在这里插入图片描述

在这里插入图片描述

成功进入系统设置界面

二、实现 Kickstart 无人值守安装

1. Kickstart 概述

  • KickStart 是一种无人职守安装方式。
  • KickStart 的工作原理是通过记录典型的安装过程中所需人工干预填写的各种参数,并生成一个名为 ks.cfg 的文件;在其后的安装过程中(不只局限于生成 KickStart 安装文件的机器)当出现要求填写参数的情况时,安装程序会首先去查找 KickStart 生成的文件,当找到合适的参数时,就采用找到的参数,当没有找到合适的参数时,才需要安装者手工干预。
  • 如果 KickStart 文件涵盖了安装过程中出现的所有需要填写的参数时,安装者完全可以只告诉安装程序从何处取 ks.cfg 文件,然后去忙自己的事情。等安装完毕,安装程序会根据 ks.cfg 中设置的重启选项来重启系统,并结束安装。

2. 准备安装应答文件

2.1 安装工具

[root@localhost ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost /var/lib/tftpboot]#yum install -y system-config-kickstart.noarch 

2.2 打开配置程序窗口

在这里插入图片描述

2.3 基本配置及安装方法

在这里插入图片描述

在这里插入图片描述

2.4 进行分区

在这里插入图片描述

2.5 网络配置

在这里插入图片描述

2.6 防火墙设置

在这里插入图片描述

2.7 指定保存的目录位置

在这里插入图片描述

2.8 ks.cfg应答文件配置

[root@localhost /var/lib/tftpboot]#cd /var/ftp/
[root@localhost /var/ftp]#ls
centos7  ks.cfg  pub
[root@localhost /var/ftp]#vim ks.cfg

在这里插入图片描述

如果需要自己配置软件包,需要编辑ks.cfg文件vim ks.cfg ,添加至最后。

[root@localhost /var/ftp]#cd /root/
[root@localhost ~]#ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]#vim anaconda-ks.cfg 

在这里插入图片描述

[root@localhost ~]#vim /var/ftp/ks.cfg 
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$rxCPOPG9$VAVCgSMhab5XOeK8dUwdu/
# Use network installation
url --url="ftp://192.168.8.135/centos7"
# System language
lang zh_CN
# 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
# Halt after installation
halt
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1

%packages
@^graphical-server-environment
@base
@core
@desktop-debugging
@development
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@hardware-monitoring
@input-methods
@internet-browser
@multimedia
@print-client
@x11
chrony

%end

在这里插入图片描述

2.9 编辑引导菜单文件 default,添加 ks 引导参数

[root@localhost /var/ftp]#vim /var/lib/tftpboot/pxelinux.cfg/default 

default auto
prompt 1
label auto
      kernel vmlinuz
      append initrd=initrd.img method=ftp://192.168.8.135/centos7 ks=ftp://192.168.8.135/ks.cfg

label linux text
      kernel vmlinuz
      append text initrd=initrd.img method=ftp://192.168.8.135/centos7 ks=ftp://192.168.8.135/ks.cfg

label linux rescue
      kernel vmlinuz
      append rescue initrd=initrd.img method=ftp://192.168.8.135/centos7 ks=ftp://192.168.8.135/ks.cfg


[root@localhost /var/ftp]#systemctl restart vsftpd && systemctl restart dhcpd && systemctl restart tftp

重新启动,等待自动安装。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

  • 实现 PXE 远程装机要求网卡支持,且必须有 Linux 安装源,以及可用的 TFTP、DHCP 服务器。

  • 无人值守的应答文件可通过 Kickstart 配置程序来完成,该程序由 system-config-kickstart 软件提供。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

头发莫的了呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值