33.PXE实现Linux全自动化批量装机

本文档详细介绍了如何设置一个环境,通过PXE网络启动和Kickstart自动化安装CentOS7。步骤包括关闭防火墙和SElinux,配置DHCP和TFTP服务,设置HTTP服务器以提供安装镜像,以及生成和定制Kickstart配置文件。最后,通过虚拟机测试了整个自动化安装流程。
摘要由CSDN通过智能技术生成

1.        环境准备:

版本支持:

[root@PXE ~]# uname -a
Linux PXE 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

2.        关闭防火墙,关闭自启动:

[root@PXE ~]# systemctl stop firewalld
[root@PXE ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

3.        关闭SElinux系统,并禁用SElinux:

注意:SElinux指安全增强型Linux系统,他是一个Linux内核模块,也是Linux的一个安全子系统。SElinux有两个级别:警告,强制。setenforce 0:表示警告(Permissive);setenforce 1:表示强制(Enforcing)。

[root@PXE ~]# getenforce
Enforcing
[root@PXE ~]# setenforce 0
[root@PXE ~]# getenforce
Permissive
[root@PXE ~]# vi /etc/sysconfig/selinux
[root@PXE ~]# cat /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


#修改内容:SELINUX=disabled

4.        配置dhcp服务:

[root@PXE ~]# yum install dhcp

安装完成后,DHCP服务的主配置文件为 /etc/dhcp/dhcp.conf 我们修改其主配置文件以实现为客户端分配网络参数。

(1)copy样例文件到 /etc/dhcp/dhcp.conf

[root@PXE ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf                                                                                 cp: overwrite ‘/etc/dhcp/dhcpd.conf’? yes
[root@PXE ~]# cat /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

(2)修改主配置文件:

[root@PXE dhcp]# vi dhcpd.conf
[root@PXE dhcp]# cat dhcpd.conf
subnet 192.168.137.0 netmask 255.255.255.0 {
        range 192.168.137.10 192.168.137.100;
        default-lease-time 3600;
        filename "pxelinux.0";
        next-server 192.168.137.135;
}

(3)重启DHCP服务:

[root@PXE ~]# systemctl restart dhcpd

5.        配置tftp服务:

[root@PXE ~]# yum install tftp-server

(1)安装完成后,修改配置文件 /etc/xinetd/tftp :

[root@PXE ~]# vi /etc/xinetd.d/tftp
[root@PXE ~]# cat /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

#修改内容:disable        = no

(2)重启TFTP服务:

[root@PXE ~]# systemctl restart tftp

6.        获取pxelinux.0系统:

(1)安装syslinux

[root@PXE ~]# yum install syslinux

(2)拷贝pxelinux.0文件到TFTP服务器中:

[root@PXE ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/    ##引导文件

[root@PXE ~]# cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/      ##菜单文件

(3)将镜像放在光驱并运行:

[root@PXE ~]# mount -o loop -t iso9660 /dev/sr0 /media

(4)将镜像中的启动镜像文件和启动配置文件复制到TFTP根目录中:

[root@PXE ~]# cp /media/isolinux/vmlinuz /var/lib/tftpboot/       ##安装引导文件1

[root@PXE ~]# cp /media/isolinux/initrd.img /var/lib/tftpboot/    ##安装引导文件2

(5)在/var/lib/tftpboot/下创建配置文件:

[root@PXE ~]# cd /var/lib/tftpboot/
[root@PXE tftpboot]# ll
total 60536
-rw-r--r--. 1 root root 55129656 Sep 15 17:21 initrd.img
-rw-r--r--. 1 root root    55140 Sep 15 17:15 menu.c32
-rw-r--r--. 1 root root    26759 Sep 15 17:14 pxelinux.0
-rwxr-xr-x. 1 root root  6769256 Sep 15 17:21 vmlinuz
[root@PXE tftpboot]# mkdir pxelinux.cfg
[root@PXE tftpboot]# cd pxelinux.cfg/
[root@PXE pxelinux.cfg]# touch default
[root@PXE pxelinux.cfg]# vi default
[root@PXE pxelinux.cfg]# cat default
default menu.c32
timeout 300
prompt 0

label 1
menu label ^1) Install CentOS7
menu default
kernel vmlinuz
append initrd=initrd.img method=http://192.168.137.135/CentOS7 ks=http://192.168.137.135/ks.cfg

7.        配置http服务器:

[root@PXE ~]# yum install httpd

(1)启动httpd服务

[root@PXE lib]# systemctl restart httpd

(2)在/var/www/html/目录下创建目录CentOS7

[root@PXE ~]# cd /var/www/html/
[root@PXE html]# mkdir CentOS7
[root@PXE html]# ll
total 0
drwxr-xr-x. 2 root root 6 Sep 15 17:32 CentOS7

(3)将/dev/cdrom挂载在该目录下:

[root@PXE html]# mount /dev/cdrom /var/www/html/CentOS7
mount: /dev/sr0 is write-protected, mounting read-only

(4)保证能看到安装光盘的内容:http://192.168.137.135/CentOS7

 8.        Kickstart文件的生成:

(1)先查看模板文件:

[root@PXE ~]# ll
total 4
-rw-------. 1 root root 1600 Sep 15 09:55 anaconda-ks.cfg
[root@PXE ~]# cat anaconda-ks.cfg

(2)拷贝模板文件到/var/www/html/目录下:

[root@PXE ~]# cp anaconda-ks.cfg /var/www/html/
[root@PXE ~]# cd /var/www/html/
[root@PXE html]# ll
total 6
-rw-------. 1 root root 1600 Sep 15 18:01 anaconda-ks.cfg
drwxr-xr-x. 8 root root 2048 Nov  4  2020 CentOS7

(3)将模板文件改名为ks.cfg

[root@PXE html]# mv anaconda-ks.cfg ks.cfg
[root@PXE html]# ll
total 6
drwxr-xr-x. 8 root root 2048 Nov  4  2020 CentOS7
-rw-------. 1 root root 1600 Sep 15 18:01 ks.cfg

(4)为ks.cfg文件加上 r 权限:

[root@PXE html]# chmod +r ks.cfg
[root@PXE html]# ll
total 6
drwxr-xr-x. 8 root root 2048 Nov  4  2020 CentOS7
-rw-r--r--. 1 root root 1600 Sep 15 18:01 ks.cfg

(5)保证能看到安装脚本文件的信息:http://192.168.137.135/ks.cfg

 (6)修改配置文件ks.cfg

·开头修改部分:

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
url --url http://192.168.137.135/CentOS7  ###第一处修改
# Use graphical install
#graphical             ###第二处修改
text           ###第三次修改

·中间部分修改:

# Partition clearing information
#clearpart --none --initlabel    ###第一处修改
clearpart --all --initlabel        ###第二处修改
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=500
part pv.253 --fstype="lvmpv" --ondisk=sda --size=19979
volgroup centos --pesize=4096 pv.253
logvol /  --fstype="xfs" --size=17927 --name=root --vgname=centos
logvol swap  --fstype="swap" --size=2048 --name=swap --vgname=centos

·结尾部分修改:

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
 
reboot  ###加个reboot

9.        重启所有服务:

[root@PXE ~]# systemctl restart httpd
[root@PXE ~]# systemctl restart tftp
[root@PXE ~]# systemctl restart dhcpd

10.        创建虚拟机进行测试:

 (1)点击开启虚拟机:

(2)等待环境自动配置安装:

 

(3)等待几分钟就OK了:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值