部署步骤

1、更新yum源

yum repolist

centos7 清除make信息 删除centos7_IP


2、安装dhcpd、tftp、syslinux、wget和vsftpd

yum install -y dhcp tftp tftp-server syslinux wget vsftpd

3、关闭SELinux(手动修改/etc/selinux/config),确保SELINUX=disabled,关闭防火墙

vim /etc/selinux/config
SELINUX=disabled
#保存退出

centos7 清除make信息 删除centos7_centos7 清除make信息_02

setenforce 0
systemctl disable firewalld
systemctl stop firewalld

4、关机修改配置,poweroff,在VMware中将虚拟机的网络适配器设置为“桥接模式”,如果是物理机就不需要了

5、修改网卡IP

  • 直接修改网卡IP
nmcli con show
nmcli con modi ens32 ipv4.method manual ipv4.addresses "192.168.1.1/24"      #我的网卡接口id是ens32。于是将它的IP设为192.168.1.1。
nmcli con up ens32
nmcli con show
ip a       #确认192.168.1.1已经成功设置。
  • 添加一个新的网卡
    旧的网卡保持你原来的模式和IP地址,新的网卡使用桥接模式,下面使用修改文件的方式来修改IP地址。
ifconfig      #记住新加的网卡名,我这里是ens37
cd /etc/sysconfig/network-scripts
vim ifcfg-ens37
#按照下面的图2修改文件
#保存退出
systemctl restart network

centos7 清除make信息 删除centos7_centos7 清除make信息_03


centos7 清除make信息 删除centos7_IP_04


centos7 清除make信息 删除centos7_linux_05

6、设置DHCP服务

vim /etc/dhcp/dhcpd.conf
#确保配置文件内容如下
ddns-update-style interim;
ignore client-updates;
authoritative;
allow booting;
allow bootp;
allow unknown-clients;
 
# A slightly different configuration for an internal subnet.
 subnet 192.168.1.0 netmask 255.255.255.0 
{
   range 192.168.1.2 192.168.1.253;
   option domain-name-servers 192.168.1.1;
   option domain-name "server1.example.com";
   option routers 192.168.1.1;
   option broadcast-address 192.168.1.255;
   default-lease-time 600;
   max-lease-time 7200;
 
   # PXE SERVER IP
   next-server 192.168.1.1;     # DHCP server ip
   filename "pxelinux.0";
 }

这里的“pxelinux.0”文件在下面的步骤再进行配置,这里设定它的路径是/tftpboot/pxelinux.0,但不需要使用路径,只需使用名称。配置文件中的每一行结尾都有分号!

7、为xinetd配置TFTP服务器,即将tftp服务交给xinetd来管理

vim /etc/xinetd.d/tftp
#修改内容如下
service tftp
{
     socket_type          = dgram
     protocol                = udp
     wait                      = yes
     user                    = root
     server                  = /usr/sbin/in.tftpd
     server_args             = -s /tftpboot
     disable                 = no
     per_source              = 11
     cps                     = 100 2
     flags                   = IPv4
 }

我们要将tftp服务的位置挪到/tftpboot中,将有个程序仍然认为它的路径在/var/lib/tftpboot中,所以,要改回来。

vim /usr/lib/systemd/system/tftp.service
#修改ExecStart=/usr/sbin/in.tftpd -s /var/lib/tftpboot
ExecStart=/usr/sbin/in.tftpd -s /tftpboot

这样才能与/etc/xinetd.d/tftp中的相应内容保持一致。

8、设置TFTP服务器网络启动文件。首先要创建一个路径/tftpboot

mkdir /tftpboot
chmod 777 /tftpboot

cp /usr/share/syslinux/pxelinux.0 /tftpboot
cp /usr/share/syslinux/menu.c32 /tftpboot
cp /usr/share/syslinux/memdisk /tftpboot
cp /usr/share/syslinux/mboot.c32 /tftpboot
cp /usr/share/syslinux/chain.c32 /tftpboot

再创建两个路径:

mkdir /tftpboot/pxelinux.cfg
mkdir /tftpboot/netboot

9、将CentOS的ISO文件发布到FTP的文件夹中。要做到这一点,必须要有一个CentOS 7的ISO文件。我要把它以虚拟光盘的形式挂载到虚拟机中。

lsblk     #查看光驱挂载路径
mkdir -p /var/ftp/pub
cp -r /run/media/root/'CentOS 7 x86_64'/* /var/ftp/pub   #“CentOS 7 x86_64”要用引号括起来,否则会出错
#如果系统没有自动挂载可以自己手动挂载再复制文件
#mkdir /test 
#mount /dev/sr0 /test    
#cp -r /test/* /var/ftp/pub

centos7 清除make信息 删除centos7_vim_06

10、把系统启动时需要使用的镜像文件拷贝到目录位置

cp /var/ftp/pub/images/pxeboot/vmlinuz /tftpboot/netboot/
cp /var/ftp/pub/images/pxeboot/initrd.img /tftpboot/netboot/

centos7 清除make信息 删除centos7_centos7 清除make信息_07

11、创建一个kickstart,即无人值守安装文件,名字叫ks.cfg

vim /var/ftp/pub/ks.cfg
#其内容如下:

 #platform=x86, AMD64, or Intel EM64T
 #version=DEVEL
 # Firewall configuration
 firewall --disabled
 # Install OS instead of upgrade
 install
 # Use NFS installation media
 url --url="ftp://192.168.1.1/pub/"
 rootpw --plaintext 123456
#root的密码设为123456
 # Use graphical install
 graphical
 firstboot disable
 # System keyboard
 keyboard us
 # System language
 lang en_US
 # SELinux configuration
 selinux disabled
 # Installation logging level
 logging level=info
# System timezone
 timezone Asia/Shanghai
 # System bootloader configuration
 bootloader location=mbr
 clearpart --all --initlabel
 part swap --asprimary --fstype="swap" --size=1024
 part /boot --fstype xfs --size=200
 part pv.01 --size=1 --grow
 volgroup rootvg01 pv.01
 logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow
 reboot

%packages
 @core
 wget
 %end

%post
 %end

保存后,用ksvalidator检查一下是否有语法错误。

ksvalidator /var/ftp/pub/ks.cfg

centos7 清除make信息 删除centos7_vim_08


12、创建一个pxe菜单

vim /tftpboot/pxelinux.cfg/default
#default的内容如下:
 default menu.c32
 prompt 0
 timeout 30
 MENU TITLE Togogo.net Linux Training

 LABEL centos7_x64
 MENU LABEL CentOS 7 X64
 KERNEL /netboot/vmlinuz
 APPEND  initrd=/netboot/initrd.img inst.repo=ftp://192.168.1.1/pub ks=ftp://192.168.1.1/pub/ks.cfg

最后这个APPEND写成一行,中间不要断行。

13、重启dhcpd、tftp、vsftpd服务,并把他们设置为开机自启动。

systemctl enable dhcpd
systemctl enable tftp
systemctl enable vsftpd      #将以上服务设为开机自动启动。

systemctl restart vsftpd
systemctl restart dhcpd
systemctl restart tftp         #重新启动以上服务。

14、测试
创建一个新的虚拟机,不需要挂载光驱,并把它的网卡调为桥接模式,内存要2G以上!!!开机后,新的虚拟机会通过pxe服务器来装操作系统而且是无人值守安装。


出错情况汇总

1、yum安装没有成功
第一步yum安装的时候,就没有成功,自己却忽略了。可能的情况是:1)/etc/yum.repos.d目录中存在着另一个仓库且无法使用。需要将其disable或删除。2)无法上网。ping www.baidu.com时显示找不到目标。
yum repolist

2、没有修改PXE服务器的IP地址192.168.1.1

3、没有将/var/ftp/pub/images/pxeboot/vmlinuz和initrd.img拷贝到/tftpboot/netboot下

4、/tftpboot/pxelinux.cfg/default内容残缺

5、/etc/selinux/config中SELINUX=disable,正确的应该是disabled

6、vsftpd配置不允许匿名登录,导致客户机无法下载所需要的安装文件

7、没有设置成"桥接模式"

8、测试的客户机中仍然使用ISO来安装

9、硬盘满了

10、新建的虚拟机内存没有达到2G