centos7 部署kickstart脚本

发布时间:2024年01月11日

kickstart主机为仅主机模式

复制下方所有到.sh结尾的文件,执行脚本即可

要注意的是:

? ? 1、需要关闭虚拟网络编辑器中使用DHCP获取IP

? ? 2、这个脚本会重新配置yum源,原来的yum源会被移动到/tmp下

? ? 3、如果不成功,手动查看并关闭防火墙,selinux,iptables

? ? 4、新机器获取的IP在x.x.x.100----x.x.x.200之间

? ? 5、脚本执行完成,创建新机器时第一次开机不要挂载光盘,等待安装成功后关机,挂载光盘再开机

? ? 6、遇到问题请留言

#!/bin/bash
#kaskstart无人值守安装方式的部署
#cjq

#请检查虚拟机的虚拟网络编辑器中使用DHCP获取IP是否关闭
read -p "你的网络编辑器是否取消了DHCP自动分配?yes/no:" a
if [ $a == yes ] ; then
? #配置yum源
? umount /dev/sr0
? mkdir /mnt/cdrom
? mv /etc/yum.repos.d/* /tmp/
? touch /etc/yum.repos.d/centos7.repo
? echo "[development]
name=centos_7
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0" > /etc/yum.repos.d/centos7.repo
? mount /dev/sr0 /mnt/cdrom
? echo -e "/dev/sr0\t/mnt/cdrom\tiso9660\tdefaults\t0 0 "
? mount -a
? yum clean all
? yum repolist

? #永久关闭防火墙
? echo -e "\n\t\t1.关闭防火墙"
? firewall_status=$(systemctl is-active firewalld)

? if [ "$firewall_status" == "active" ]; then
? ? ? ? systemctl stop firewalld
? ? ? ? systemctl disable firewalld
? ? ? ? echo "防火墙关闭成功"
? else
? ? echo "防火墙未激活"
? fi

?#永久关闭selinux
? echo -e "\n\t\t2.关闭selinux"
? selinux_status=$(cat /etc/selinux/config | grep -i "selinux=" |cut -d "=" -f 2)
? if [ "$selinux_status" == "enforcing" ] ; then
? sed -i 's/SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
? ? ? ? echo "成功永久关闭selinux"
? else
? ? ? ? echo "selinux未开启"
? fi
?#安装DHCP
? ?#获取ip,可完成不同网段的DHCP
? ? ip=`ifconfig | awk 'NR==2{print $2}'`
? ? ip1=`ifconfig | awk 'NR==2{print $2}' | awk 'BEGIN{FS=".";OFS="."}NR==1{print $1,$2,$3}'`
? yum -y install dhcp
? echo "option domain-name \"example.org\";
option domain-name-servers 114.114.114.114, 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet $ip1.0 netmask 255.255.255.0 {
? range ?$ip1.100 $ip1.200;
? option routers $ip;
? next-server $ip;
? filename \"pxelinux.0\";
}" > /etc/dhcp/dhcpd.conf
?#启动dhcpd服务
? systemctl start dhcpd
? systemctl enable dhcpd
? systemctl status dhcpd

?#安装tftp
? yum -y install tftp-server xinetd
? sed -i '/disable/s/yes/no/' vim /etc/xinetd.d/tftp
?#启动xinetd服务 tftpd服务归xinetd服务管理
? systemctl start xinetd

?#配置使用PXE启动所需的相关文件
? yum install -y system-config-kickstart syslinux
? mkdir /var/lib/tftpboot/pxelinux.cfg
? cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
? cp /mnt/cdrom/images/pxeboot/* /var/lib/tftpboot/
? cp /mnt/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
? sed -i '1s/vesamenu.c32/linux/' /var/lib/tftpboot/pxelinux.cfg/default
? sed -i '64s/hd:LABEL=CentOS\\x207\\x20x86_64 quiet/ftp:\/\/'$ip'\/pub inst.ks=ftp:\/\/'$ip'\/ks.cfg/' /var/lib/tftpboot/pxelinux.cfg/default

?#安装ftp
? yum install -y vsftpd
?#启动vsftpd服务
? systemctl start vsftpd
? systemctl enable vsftpd
? systemctl status vsftpd

?#配置ftp软件仓库
? echo -e "/dev/sr0\t/var/ftp/pub\tiso9660\tdefaults\t0 0" >> /etc/fstab
? mount -a
?#查看端口
? echo "dhcp端口情况:"
? netstat -antup | grep dhcp
? echo "tftp端口情况:"
? netstat -antup | grep 69
? echo "vsftp端口情况:"
? netstat -antup | grep vsftpd

?#生成ks.cfg文件
?touch /var/ftp/ks.cfg
?echo "#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$DtsN5hKw\$7lblhttK0f2QwDY2GqNVb/
# System language
lang en_US
# System authorization information
auth ?--useshadow ?--passalgo=sha512
# Use graphical install
graphical
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx


# 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://$ip/pub\"
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype=\"xfs\" --size=500
part swap --fstype=\"swap\" --size=2048
part / --fstype=\"xfs\" --grow --size=1

%post --interpreter=/bin/bash
mv /etc/yum.repos.d/* /opt
mkdir /mnt/cdrom
mount /dev/sr0 /mnt/cdrom/
echo \"[centos7]
name=centos_7 base
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0\" > /etc/yum.repos.d/centos7.repo
echo \"/dev/sr0 ? ?/mnt/cdrom ? ? iso9660 ? ? defaults ? ? ? ?0 0\" >> /etc/fstab
%end

%packages
@base

%end" > /var/ftp/ks.cfg
else?
? echo "请设置"
? exit 1
fi
?

文章来源:https://blog.csdn.net/2301_81851878/article/details/135401875
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。