pxe+kickstart+dhcp+tftp+httpd实现无人值守安装CentOS7系统

下面是小凰凰的简介,看下吧!
💗人生态度:珍惜时间,渴望学习,热爱音乐,把握命运,享受生活
💗学习技能:网络 -> 云计算运维 -> python全栈( 当前正在学习中)
💗您的点赞、收藏、关注是对博主创作的最大鼓励,在此谢过!
有相关技能问题可以写在下方评论区,我们一起学习,一起进步。
后期会不断更新python全栈学习笔记,秉着质量博文为原则,写好每一篇博文。

一、简介

1、什么是PXE
  • PXE,全名Pre-boot Execution Environment,预启动执行环境
  • PXE客户端(client)这个术语是指机器在PXE启动过程中的角色。一个PXE客户端可以是一台服务器、笔记本电脑或者其他装有PXE启动代码的机器(我们电脑的网卡)。
2、PXE的工作过程

在这里插入图片描述推荐看下:
在这里插入图片描述注意:
1. 后面实操应该实时查看系统日志,结合这个图进行分析理解!

2. 上面不论是dhcp还是其他服务我们都部署在一个虚拟机上!

补充图解:
在这里插入图片描述

二、实现网络手工安装

1、系统环境准备

目的:实现为数百台空白服务器,安装操作系统!

# 关闭防火墙
systemctl stop firewalld

# 关闭selinux
sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config # 重启
getenforce
# 虚拟机网卡说明
本机需要两块网卡,一块网卡为NAT模式,一块网卡为LAN区段

配置yum源:

cd /etc/yum.repos.d/
rm -f *
mount /dev/sr0 /mnt
vim /etc/yum.repos.d/base.repo
'
name=base
baseurl=file:///mnt/
gpgcheck=0
enabled=1
'
2、安装配置dhcp服务
// 本机的内网网卡ip为192.168.1.201
yum install dhcp -y
[root@linux-node1 ~]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
        range 192.168.1.1 192.168.1.200;
        option subnet-mask 255.255.255.0;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 192.168.1.201;
        filename "/pxelinux.0";
}
// 注:dhcp服务器应该和需要安装操作系统的虚拟机在一个内网环境中,且没有其他dhcp服务器的干扰!

systemctl restart dhcpd
3、安装配置tftp服务
yum install tftp-server -y
[root@linux-node1 ~]# vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot // 指定目录,保持默认,不用修改
        disable                 = no // 由原来的yes改为no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

systemctl start tftp.socket
systemctl start tftp.service
netstat -luntp|egrep '69|67'
4、安装配置httpd服务
yum install httpd -y
sed -i '96i ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf
systemctl restart httpd
mkdir /var/www/html/CentOS-7.6
cp -a /mnt/* /var/www/html/CentOS-7.6/ 
# 注意还有两个隐藏文件没有拷贝过去:
cp /mnt/.discinfo /var/www/html/CentOS-7.6/
cp /mnt/.treeinfo /var/www/html/CentOS-7.6/
# 前面将centos7的镜像文件挂载到了/mnt下,我们需要把镜像文件复制到httpd的发布目录来,后面虚拟机回自己找httpd来下载

curl http://192.168.1.201/CentOS-7.6/ # 看下httpd是否成功!
5、配置PXE启动程序
(1)PXE引导配置

syslinux是一个功能强大的引导加载程序,而且兼容各种介质。SYSLINUX是一个小型的Linux操作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。如果没有找到pxelinux.0这个文件,可以安装一下。

yum install syslinux -y
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp -a /var/www/html/CentOS-6.7/isolinux/* /var/lib/tftpboot/
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cp /var/www/html/CentOS-6.7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
(2)PXE配置文件default解析
[root@linux-node1 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
label linux
   menu label ^Install CentOS 7
   kernel vmlinuz
   append initrd=initrd.img inst.stage2=http://192.168.1.201/CentOS-7.6/ quiet net.ifnames=0 biosdevname=0
// 其中"net.ifnames=0 biosdevname=0"这两个内核启动参数是为了让网卡名称为ethN,而不是默认的eno16777728这样的随机名称。
6、测试手工网络安装操作系统

因为我电脑是mac本,这里听说本机的内网网卡需要用lan区段,但是我这个vmware fusion好像并没有这个网络模式,因此我采用了仅主机模式,但是到这里出现了问题!这个只能等以后有机会了,再补充完善吧!

(1)新建空白虚拟机
(2)启动虚拟机

三、实现kickstart自动化安装

1、ks.cfg详解

ks.cfg文件组成大致分为3段

  • 命令段
键盘类型,语言,安装方式等系统的配置,有必选项和可选项,如果缺少某项必选项,安装时会中断并提示用户选择此项的选项
  • 软件包段
 - %packages
 - @groupname:指定安装的包组
 - package_name:指定安装的包
 - -package_name:指定不安装的包
在安装过程中默认安装的软件包,安装软件时会自动分析依赖关系。
  • 脚本段(可选)
1. %pre:安装系统前执行的命令或脚本(由于只依赖于启动镜像,支持的命令很少)
2. %post:安装系统后执行的命令或脚本(基本支持所有命令)

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

2、编写ks.cfg文件
# 先生成一个密码备用
[root@linux-node1 ~]# grub-crypt
Password:123456
Retype password:123456
$6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/

[root@linux-node1 ~]# mkdir /var/www/html/ks_config
[root@linux-node1 ~]# vim /var/www/html/ks_config/CentOS-7.6-ks.cfg
# Kickstart Configurator for CentOS 7.6 by yao zhang
install
url --url="http://192.168.1.201/CentOS-7.6/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post
wget -O /tmp/optimization.sh http://192.168.1.201/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end
3、开机优化脚本
[root@linux-node1 ~]# vim /var/www/html/ks_config/optimization.sh
#!/bin/bash
##############################################################
# File Name: /var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2015-12-03 15:23:08
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=192.168.1.201
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip  -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
        echo "Http server is bad!"
        exit 1
}
# Defined result function
function Msg(){
        if [ $? -eq 0 ];then
          action "$1" /bin/true
        else
          action "$1" /bin/false
        fi
}
# Defined IP function
function ConfigIP(){
        Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print $6}'`
        cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
        DEVICE=eth0
        TYPE=Ethernet
        ONBOOT=yes
        NM_CONTROLLED=yes
        BOOTPROTO=none
        IPADDR=10.0.0.$Suffix
        PREFIX=24
        GATEWAY=10.0.0.2
        DNS1=10.0.0.2
        DEFROUTE=yes
        IPV4_FAILURE_FATAL=yes
        IPV6INIT=no
        NAME="System eth0"
        END
        Msg "config eth0"
}
# Defined Yum source Functions
function yum(){
        YumDir=/etc/yum.repos.d
        [ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori} 
        wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
        wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
        Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
        [ -f "/etc/issue" ] && >/etc/issue
        Msg "Hide issue" 
        [ -f "/etc/issue.net" ] && > /etc/issue.net
        Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
        [ -f "/etc/security/limits.conf" ] && {
        echo '*  -  nofile  65535' >> /etc/security/limits.conf
        Msg "open files"
        }
}
# Defined Kernel parameters Functions
function kernel(){
        KernelDir=/etc
        [ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
        wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
        Msg "Kernel config"
}
# Defined System Startup Services Functions
function boot(){
        for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"` 
          do 
           chkconfig $oldboy off
        done
        Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
        echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
        echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
        Msg "Time Synchronization"
}
# Defined main Functions
function main(){
        ConfigIP
        yum
        HideVersion
        openfiles
        kernel
        boot
        Time
}
main
4、整合编辑default配置文件
# 最精简配置
[root@linux-node1 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default ks
prompt 0
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.1.201/ks_config/CentOS-7.6-ks.cfg ksdevice=eth0 # 告诉安装程序ks.cfg文件在哪里
# ksdevice=eth0代表当客户端有多块网卡的时候,要实现自动化需要设置从eth1安装,不指定的话,安装的时候系统会让你选择,那就不叫全自动化了。
5、无人值守自动安装

打开系统电源,出去喝杯水。过会回来,系统就以经装好了._

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凤求凰的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值