部署环境:centos7.9(带图形)
网络:外网通
注意事项:此脚本在虚拟化环境中运行有几点注意事项,详情请参考作者抖音号:bdqnpeng(有视频讲解),或作者个人主页:PXE脚本使用-实验状态|彭大帅不太帅|网络安全|linux|华为|思科-彭大帅不太帅
生产状态使用请私信留言即可!
PXE 服务器配置需要高一点,cpu请开启虚拟化支持!
我们直接将脚本复制到centos7主机上。(在主机上创建一个文件,将以下脚本拷贝进去保存即可)
#!/bin/bash
#function:install PXEServer
#author:tommypeng 20220708 final version
###############root用户判断#######################
if
[ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld && echo "防火墙已经关闭"
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && echo "关闭selinux"
##############脚本运行前置条件#################
echo "主机需要挂载centos7镜像"
echo "实验环境请添加创建仅主机网卡,防止DHCP冲突,非实验环境请忽略此提示!"
sleep 5
read -p "以上环境是否已经符合要求?请输入yes 或者 no :" guaiguai
case $guaiguai in
yes)
echo "真是个乖孩子"
;;
no)
echo "完犊子了,因为你的不听话,只能退出脚本运行了,你还是慢慢敲命令吧!"
exit 0
;;
*)
echo "你好像不认识汉字也不认识英文,要不要我推荐你去厚街小学上学啊"
exit 1
esac
ping -c 3 www.baidu.com
if
[ $? = 0 ]
then
echo "外网通讯良好!"
else
echo "丫的你在逗我吗?网都没有安装个毛线!"
exit 1
fi
###########网络配置############
read -p "请输入您的仅主机网卡名:" ppo
onlyhostIP=$(ip a | grep "inet "|grep "$ppo" | awk '{print $2}' | awk -F '/' '{print $1}')
onlyhostMASk=$(ip a | grep "inet "|grep "$ppo" | awk '{print $2}' | awk -F '/' '{print $2}')
A=$(echo "$onlyhostIP" | awk -F . '{print $1}')
B=$(echo "$onlyhostIP" | awk -F . '{print $2}')
C=$(echo "$onlyhostIP" | awk -F . '{print $3}')
SS1="$A.$B.$C.1"
SS2="$A.$B.$C"
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$ppo" ]
then
rm -f /etc/sysconfig/network-scripts/ifcfg-$ppo
fi
echo "#HWADDR=00:0C:29:F1:B0:E9
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
#IPADDR=192.168.145.128
#PREFIX=24
#GATEWAY=192.168.145.1
#DNS1=192.168.145.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
#NAME=$'\746\634\611\747\672\677\750\677\636\746\616\645 1'
UUID=2d182d1c-56e1-323f-be1a-d4ae07b1f614
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "IPADDR=$onlyhostIP" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "PREFIX=$onlyhostMASK" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "GATEWAY=$SS1" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "DNS1=$SS1" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "NAME=$PPO" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
echo "DEVICE=$PPO" >>/etc/sysconfig/network-scripts/ifcfg-$ppo
ifdown $ppo && ifup $ppo && echo "$ppo重启网卡成功"
ifdown "system*"
##########创建安装目录##########
cd /var/ftp/ && mkdir centos7u4
################DHCP#############
yum install dhcp -y
if [ $? -eq 0 ]
then
echo "DHCP安装成功"
else
echo "完犊子了,出现致命错误,即将退出"
exit 1
fi
#################DHCP配置##########
####cp -f /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cat >> /etc/dhcp/dhcpd.conf <<EOF
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
log-facility local7;
subnet $SS2.0 netmask 255.255.255.0 {
range $SS2.20 $SS2.100;
option routers $SS2.2;
option domain-name-servers centos7.expample.com;
default-lease-time 600;
max-lease-time 7200;
next-server $onlyhostIP;
filename "pxelinux.0";
}
option domain-name "centos7.expample.com";
option domain-name-servers ns1.example.org, ns2.example.org;
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 10.254.239.0 netmask 255.255.255.224 {
range 10.254.239.10 10.254.239.20;
option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
subnet 10.254.239.32 netmask 255.255.255.224 {
range dynamic-bootp 10.254.239.40 10.254.239.60;
option broadcast-address 10.254.239.31;
option routers rtr-239-32-1.example.org;
}
# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 {
range 10.5.5.26 10.5.5.30;
option domain-name-servers ns1.internal.example.org;
option domain-name "internal.example.org";
option routers 10.5.5.1;
option broadcast-address 10.5.5.31;
default-lease-time 600;
max-lease-time 7200;
}
# Hosts which require special configuration options can be listed in
# host statements. If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.
host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename "vmunix.passacaglia";
server-name "toccata.fugue.com";
}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.
class "foo" {
match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}
shared-network 224-29 {
subnet 10.17.224.0 netmask 255.255.255.0 {
option routers rtr-224.example.org;
}
subnet 10.0.29.0 netmask 255.255.255.0 {
option routers rtr-29.example.org;
}
pool {
allow members of "foo";
range 10.17.224.10 10.17.224.250;
}
pool {
deny members of "foo";
range 10.0.29.10 10.0.29.230;
}
}
EOF
###########安装syslinux,tftp服务##################
yum install syslinux -y
sleep 5
if [ $? -eq 0 ]
then
echo "syslinux安装成功"
else
echo "完犊子了,出现致命错误,即将退出"
exit 1
fi
yum install tftp-server -y
if [ $? -eq 0 ]
then
echo "tftp-server安装成功"
else
echo "完犊子了,出现致命错误,即将退出"
exit 1
fi
sleep 5
sed -i 's/disable = yes/disable = no/g' /etc/xinetd.d/tftp
sed -i '14s/yes/no/g' /etc/xinetd.d/tftp
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot && cd /var/lib/tftpboot/
##########创建安装目录##########
cd /var/ftp/ && mkdir centos7u4
cd /var/lib/tftpboot/ && mkdir centos7u4
################安装vsftp服务####################
yum install vsftpd -y
if [ $? -eq 0 ]
then
echo "vsftpd安装成功"
else
echo "完犊子了,出现致命错误,即将退出"
exit 1
fi
sleep 5
mkdir /var/ftp/centos7 && mount /dev/cdrom /var/ftp/centos7/
cd /var/ftp/centos7/images/pxeboot/ && cp initrd.img vmlinuz /var/lib/tftpboot/ && echo "初始化与压缩内核已经复制到TFTP站点中"
##############编辑启动菜单default配置文件################
mkdir /var/lib/tftpboot/pxelinux.cfg
cat >> /var/lib/tftpboot/pxelinux.cfg/default << EOF
default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://$onlyhostIP/centos7 ks=ftp://$onlyhostIP/ks.cfg
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://$onlyhostIP/centos7
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://$onlyhostIP/centos7
EOF
#################启动服务###################
###systemctl start dhcpd && systemctl start tftp && systemctl start vsftpd && echo "所有服务均启动"
###########应答##########
yum install system-config-kickstart -y
if [ $? -eq 0 ]
then
echo "kickstart安装成功"
else
echo "完犊子了,出现致命错误,即将退出"
exit 1
fi
##################应答文件################
cat >> /var/ftp/ks.cfg <<EOF
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$MWRlRqh4$ctIOAjZTrJfPUOwjDEfN0/
#########Pxg-1981
# System language
lang zh_CN
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --enable
# SELinux configuration
selinux --disabled
# 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="ftp://$onlyhostIP/centos7"
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all
# Disk partitioning information
part home --fstype="xfs" --size=4096
part boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1
%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
EOF
###############启动服务##############
echo "请到虚拟网络编辑器关闭仅主机网络的DHCP服务器,二十秒后本机将重启"
systemctl enable dhcpd && systemctl enable tftp && systemctl enable vsftpd
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32m重启后请重新运行光盘挂载命令:mount /dev/cdrom /var/ftp/centos7/\033[0m"
sleep 20
reboot
脚本运行过程中需要您确认环境是否符合要求,比如是否创建了仅主机网卡,符合要求请输入yes,脚本会继续运行。
还需要根据提示输入仅主机网卡的名称查看方法如下:
那么我们输入ens37回车即可。
请注意,在安装过程中我们需要手动切换一下ens33网卡,保持外网通讯,不然yum安装会失败(实验状态一点小bug)
保证上图以太网ens33网卡一直保持在ens33上(左边的黑点标记)
只要默默等待脚本运行完就行。
脚本最后会提示自动重启,重启后需要运行一条命令重新挂载一下光盘镜像。
命令如下:mount /dev/cdrom /var/ftp/centos7/
主机重启过程中,我们需要在虚拟网络编辑器中关闭仅主机网络的DHCP服务(此服务现在由我们部署的PXEServer提供了,防止冲突)
这样服务端就算部署好了。
我们新建一台虚拟机,选择系统随后安装,添加仅主机网卡,然后从固件启动,修改第一启动项为网卡,该机启动后,PXEServer会给其派发IP,推送内核,完成自动安装。
(注意:从安全考虑,被推送系统的主机需要您在看到boot时回车一下确认一下)
另,安装好系统重启后需要调整bios启动顺序,或者将PXEServer主机关闭。
生产状态想使用此脚本注意事项请留言!!!!