Kickstart基于CentOS 7.X自动安装部署脚本

小柒博客 评论81,980字数 1363阅读4分32秒阅读模式
摘要用途:自动化安装系统。 优势:传统装机需要下载系统镜像,然后人工一步一步设置各种参数,然后点击按钮安装,如果需要同时安装100台机器,则重复性工作太多,而且耗费人力和时间,kick...

什么是kickstart批量安装?

Kickstart是一种无人值守的安装方式之一(还有一种叫做Cobbler)。他的工作原理就是在安装过程中记录人工干预填写的各种参数,并生成一个名为ks.cfg的文件。如果在自动安装过程中出现要填写参数的情况,安装程序就会首先去查找ks.cfg文件,如果找到合适的参数,就采用所找的参数;如果没有找到,便弹出对话框让安装着手动书写。所以,ks.cfg文件涵盖了所有安装过程中需要填写的参数。那么安装者现在只需要告诉安装程序从何处下载ks,cfg文件,剩下的交给kickstart就可以完成安装操作系统这个事情。等安装完毕之后,安装程序根据ks.cfg中设置的重启/关闭系统,并结束安装。

此脚本是Kickstart基于CentOS 7.X自动安装部署脚本,有需要朋友可以参考,脚本内容如下:

系统环境:CentOS 7.9

[root@localhost ~]# vim install_kickstart_centos7.sh

#!/bin/bash
#Date:2020-4-27 16:03:24
#Author Blog:
#	https://www.yangxingzhen.com
#Author WeChat:
#	微信公众号:小柒博客
#Author mirrors site:
#	https://mirrors.yangxingzhen.com
#About the Author
#	BY:YangXingZhen
#	Mail:xingzhen.yang@yangxingzhen.com
#	QQ:675583110
#Auto Install deploy kickstart(CentOS 7.X)

IPADDR=$(ifconfig |awk '/cast/ {print $2}' |awk -F: '{print $NF}')
GATEWAY=$(ip route | awk '/default/ {print $3}')
IP_1=$(echo ${IPADDR} | awk -F. '{print $1}')
IP_2=$(echo ${IPADDR} | awk -F. '{print $2}')
IP_3=$(echo ${IPADDR} | awk -F. '{print $3}')

#安装httpd、dhcp、tftp、xinetd
yum -y install httpd httpd-devel dhcp* tftp-server xinetd

#配置tftp
cat >/etc/xinetd.d/tftp <<EOF
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -u nobody -s /var/lib/tftpboot
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp

#挂载光盘
mkdir -p /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

#安装syslinux
yum -y install syslinux sysinux-devel

#配置pxelinux.cfg
mkdir -p /var/lib/tftpboot/pxelinux.cfg
\cp /mnt/cdrom/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
\cp /mnt/cdrom/isolinux/vesamenu.c32 /var/lib/tftpboot
\cp /mnt/cdrom/images/pxeboot/vmlinuz /var/lib/tftpboot
\cp /mnt/cdrom/images/pxeboot/initrd.img /var/lib/tftpboot
\cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot
chmod 644 /var/lib/tftpboot/pxelinux.cfg/default
cat >/var/lib/tftpboot/pxelinux.cfg/default <<EOF
default vesamenu.c32
timeout 10
display boot.msg
menu clear
menu background splash.png
menu title CentOS Linux 7
label linux
menu label ^Install CentOS Linux 7
menu default
kernel vmlinuz
append initrd=initrd.img inst.repo=http://${IPADDR}/centos7 quiet ks=http://${IPADDR}/ks.cfg
label check
menu label Test this ^media & install CentOS Linux 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
EOF

#配置httpd
mkdir -p /var/www/html/centos7
mount /dev/cdrom /var/www/html/centos7

#配置dhcp
cat >/etc/dhcp/dhcpd.conf <<EOF
ddns-update-style interim;
ignore client-updates;
next-server ${IPADDR};
filename "pxelinux.0";
allow booting;
allow bootp;
subnet ${IP_1}.${IP_2}.${IP_3}.0 netmask 255.255.255.0 {
#--- default gateway
option routers ${GATEWAY};
option subnet-mask 255.255.255.0;
option domain-name-servers 202.96.134.133,114.114.114.114;
range dynamic-bootp ${IP_1}.${IP_2}.${IP_3}.10 ${IP_1}.${IP_2}.${IP_3}.253;
}
EOF

#配置ks.cfg
cat >/var/www/html/ks.cfg <<EOF
install
text
keyboard 'us'
rootpw 123456
timezone Asia/Shanghai
url --url=http://${IPADDR}/centos7
lang en_US.UTF-8
network --bootproto=dhcp --device=ens33 --noipv6
auth --useshadow --passalgo=sha512
zerombr   #清除mbr引导
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
clearpart --all --initlabel  #清空分区
part /boot --fstype xfs --size 200    #/boot分区
part swap --size 512                  #swap分区
part / --fstype xfs --size 1 --grow   #根分区
firstboot --disable       #负责协助配置redhat一些重要的信息
selinux --disabled        #关闭selinux
firewall --disabled       #关闭防火墙
logging --level=info      #设置日志级别
reboot                    #安装完成重启
%packages
@^minimal
@core
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%post
#修改为固定IP(DHCP分配什么IP就改成什么IP)
IPADDR=\$(hostname -I)
GATEWAY=\$(ip route |awk '/default/ {print \$3}')
#修改为静态IP
sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-ens33
echo "IPADDR=\${IPADDR}" >>/etc/sysconfig/network-scripts/ifcfg-ens33
echo "NETMASK=255.255.255.0" >>/etc/sysconfig/network-scripts/ifcfg-ens33
echo "GATEWAY=\${GATEWAY}" >>/etc/sysconfig/network-scripts/ifcfg-ens33

systemctl restart network
#配置DNS
echo "nameserver 202.96.134.133" >/etc/resolv.conf
echo "nameserver 114.114.114.114" >>/etc/resolv.conf

SOFT="lrzsz wget gcc gcc-c++ make cmake autoconf bzip2 bzip2-devel curl openssl openssl-devel rsync gd zip perl unzip ntpdate sysstat man mtr lsof iotop net-tools openssl-devel openssl-perl iostat subversion nscd vim ntp"

#更改为163的源
yum -y install wget
mkdir -p /etc/yum.repos.d/bak
wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo
mv /etc/yum.repos.d/CentOS-*.repo /etc/yum.repos.d/bak
yum clean all
yum makecache
yum -y install \${SOFT}

#更改ulimit参数
\cp /etc/security/limits.conf /etc/security/limits.conf.back
Limit=\$(cat /etc/security/limits.conf | grep "65535"|wc -l)
if [ \${Limit} -eq 0 ];then
echo "* soft nproc 65535" >> /etc/security/limits.conf
echo "* hard nproc 65535" >> /etc/security/limits.conf
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
echo "ulimit -SHn 65535" >> /etc/profile
echo "ulimit -SHn 65535" >> /etc/rc.local
fi
#设置SSH
sed -i "s/\#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config
sed -i "s/GSSAPIAuthentication yes/GSSAPIAuthentication no/g" /etc/ssh/sshd_config

#设置时区
if [ "\$(cat /etc/crontab | grep ntpdate)" = "" ]; then
	echo "0 23 * * * root /usr/sbin/ntpdate cn.pool.ntp.org >> /var/log/ntpdate.log" >> /etc/crontab
fi
rm -rf /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate cn.pool.ntp.org
hwclock -w

#设置sysctl
SYSCONF="
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_fin_timeout = 120
net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535

net.nf_conntrack_max = 16404388
net.netfilter.nf_conntrack_tcp_timeout_established = 10800

#kernel: TCP: time wait bucket table overflow
net.ipv4.tcp_max_tw_buckets = 30000

fs.file-max=655350"

#重启SSHD服务
systemctl restart sshd
echo \${SYSCONF} >>/etc/sysctl.conf    
/sbin/sysctl -p
%end
EOF

#启动各个服务
systemctl restart dhcpd
systemctl restart xinetd
systemctl restart httpd
systemctl restart tftp

#开机自启
systemctl enable dhcpd
systemctl enable xinetd
systemctl enable httpd
systemctl enable tftp

echo -e "\033[32m温馨提示:新建虚拟机内存不能≥2G,网络模式需为桥接方式...\033[0m"

脚本执行方式:

[root@localhost ~]# sh install_kickstart_centos7.sh

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
公众号已复制
Shell最后更新:2024-1-24
小柒博客
  • 本文由 小柒博客 发表于 2020年5月22日 09:58:45
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/7145.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证