PXE 基于网络自动安装CentOS 6/7/8
说明:
- 下载好相关镜像文件,新建虚拟机安装系统
- 使用 CentOS 8 作为http、dhcp、tftp 服务器
- 要求作为服务器的 CentOS 8 的 ip 为 10.0.0.8
- 在VM上依次添加光驱并选择对应的镜像文件:8 > 7 > 6
设置服务端
CentOS 8 正常开机,确保网络畅通,执行以下脚本:
#!/bin/bash
. /etc/init.d/functions
echo -e "\e[1;32m开始配置服务器!请等待。。。\e[0m"
yum -y install httpd tftp-server dhcp-server syslinux &> /dev/null
cat > /etc/dhcp/dhcpd.conf <<EOF
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.100 10.0.0.200;
option routers 10.0.0.2;
next-server 10.0.0.8;
filename "pxelinux.0";
}
EOF
systemctl start httpd tftp.socket dhcpd
ss -ntul | grep -e 67 -e 69 -e 80 &> /dev/null && action '成功启用各项服务'
sleep 3
echo '- - -' > /sys/class/scsi_host/host0/scan
echo '- - -' > /sys/class/scsi_host/host1/scan
echo '- - -' > /sys/class/scsi_host/host2/scan
mkdir -p /var/www/html/centos/{6,7,8}/os/x86_64/
mount /dev/sr0 /var/www/html/centos/8/os/x86_64/
mount /dev/sr1 /var/www/html/centos/7/os/x86_64/
mount /dev/sr2 /var/www/html/centos/6/os/x86_64/ && action '成功挂载各个光盘'
sleep 3
mkdir /var/www/html/ks/
cd /var/www/html/ks/
#################################################
# 写入centos8的应答文件
cat > centos8.cfg <<EOF
# 基本设置
text # 文本安装界面
lang en_US.UTF-8
keyboard --vckeymap=us --xlayouts=us
timezone Asia/Shanghai --isUtc --nontp # 必须是大写U
rootpw --plaintext centos
selinux --disabled # 禁用SELinux
firewall --disabled # 禁用firewalld
firstboot --enable # 初始化设置
reboot # 安装完自动重启
skipx
services --disabled="chronyd"
# 网络
url --url=http://10.0.0.8/centos/8/os/x86_64/
network --device eth0 --bootproto dhcp --ipv6=auto --activate
network --hostname=centos8
# 分区
zerombr # 清除mbr
ignoredisk --only-use=sda # 选择在sda安装系统【此行必须指定】
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda # 此行必须指定
clearpart --all --initlabel # 清除分区信息
part / --fstype="ext4" --ondisk=sda --size=102400
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024
# 软件安装
%packages
@^minimal-environment
kexec-tools
vim
curl
wget
tree
%end
# 脚本
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
systemctl disable --now firewalld.service
systemctl stop firewalld.service
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/8/BaseOS/x86_64/os/
gpgcheck=0
enabled=1
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/x86_64/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/8/AppStream/x86_64/os/
gpgcheck=0
enabled=1
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/epel/8/x86_64
gpgcheck=0
enabled=1
EOFA
yum makecache
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.88
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB
cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]"
export EDITOR=vim
EOFC
cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD
cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand ("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#")
call setline(3, "#********************************************")
call setline(4, "#Author: jacklee")
call setline(5, "#QQ: 1227163339")
call setline(6, "#Time: ".strftime("%F_%T"))
call setline(7, "#FileName: ".expand("%"))
call setline(8, "#Copyright: ".strftime("%Y")." All rights reserved")
call setline(9, "#Description: ")
call setline(10,"#*********************************************")
endif
endfunc
EOFF
yum -y install chrony*
sed -i.bak 's/^pool 2.centos.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
%end
EOF
################################################
# 写入centos7的应答文件
cat > centos7.cfg <<EOF
text # 文本安装界面
lang en_US.UTF-8
keyboard 'us'
timezone Asia/Shanghai --nontp
rootpw --plaintext centos
selinux --disabled # 禁用SELinux
firewall --disabled # 禁用firewalld
firstboot --enable # 初始化设置
reboot # 安装完自动重启
services --disabled="chronyd"
# 网络
url --url=http://10.0.0.8/centos/7/os/x86_64/
network --device eth0 --bootproto dhcp
network --hostname=centos7
# 分区
zerombr # 清除mbr
ignoredisk --only-use=sda # 选择在sda安装系统【此行必须指定】
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda # 此行必须指定
clearpart --all --initlabel # 清除分区信息
part / --fstype="ext4" --ondisk=sda --size=102400
part /boot --fstype="ext4" --ondisk=sda --size=1024
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
# 软件安装
%packages
@^minimal
kexec-tools
vim
curl
wget
tree
%end
# 脚本
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
systemctl disable --now firewalld.service
systemctl stop firewalld.service
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
gpgcheck=0
enabled=1
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0
enabled=1
EOFA
yum makecache
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion
sed -r -i "s/quiet/& net.ifnames=0/" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.77
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB
cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]"
export EDITOR=vim
EOFC
cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD
cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand ("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#")
call setline(3, "#********************************************")
call setline(4, "#Author: jacklee")
call setline(5, "#QQ: 1227163339")
call setline(6, "#Time: ".strftime("%F_%T"))
call setline(7, "#FileName: ".expand("%"))
call setline(8, "#Copyright: ".strftime("%Y")." All rights reserved")
call setline(9, "#Description: ")
call setline(10,"#*********************************************")
endif
endfunc
EOFF
yum -y install chrony*
sed -i.bak 's/^server 0.centos.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 1.centos.*/server ntp1.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 2.centos.*/server time1.cloud.tencent.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 3.centos.*/server time2.cloud.tencent.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
%end
EOF
###################################################
# 写入centos6的应答文件
cat > centos6.cfg <<EOF
# 基本设置
text # 文本安装界面
lang en_US.UTF-8
keyboard 'us'
timezone Asia/Shanghai
rootpw --plaintext centos
selinux --disabled # 禁用SELinux
firewall --disabled # 禁用firewalld
firstboot --enable # 初始化设置
reboot # 安装完自动重启
# 网络
url --url=http://10.0.0.8/centos/6/os/x86_64/
network --device eth0 --bootproto dhcp
network --hostname=centos6
# 分区
zerombr # 清除mbr
ignoredisk --only-use=sda # 选择在sda安装系统【此行必须指定】
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
clearpart --all --initlabel # 清除分区信息
part / --fstype="ext4" --ondisk=sda --size=102400
part /boot --fstype="ext4" --ondisk=sda --size=1024
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
# 软件安装
%packages
@core
@server-policy
@workstation-policy
autofs
vim-enhanced
%end
# 脚本
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
chkconfig iptables off
service iptables stop
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/centos/6/os/x86_64/
gpgcheck=0
enabled=1
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/
gpgcheck=0
enabled=1
EOFA
yum makecache
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.66
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB
cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]"
export EDITOR=vim
EOFC
cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD
cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand ("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#")
call setline(3, "#********************************************")
call setline(4, "#Author: jacklee")
call setline(5, "#QQ: 1227163339")
call setline(6, "#Time: ".strftime("%F_%T"))
call setline(7, "#FileName: ".expand("%"))
call setline(8, "#Copyright: ".strftime("%Y")." All rights reserved")
call setline(9, "#Description: ")
call setline(10,"#*********************************************")
endif
endfunc
EOFF
yum -y install ntp* &> /dev/null
sed -i.bak 's/^server 0.*/server ntp.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 1.*/server ntp1.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 2.*/server time1.cloud.tencent.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 3.*/server time2.cloud.tencent.com iburst/' /etc/ntp.conf
chkconfig --add ntpd
chkconfig ntpd on
service ntpd restart
%end
EOF
# 应答文件完成
##############################
mkdir /var/lib/tftpboot/centos{6,7,8}
cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6
cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7
cp /var/www/html/centos/8/os/x86_64/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8
cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
cp /var/www/html/centos/8/os/x86_64/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg/
cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF
default menu.c32
timeout 600
menu title Install centos Linux
label linux8
menu label Auto Install centos Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://10.0.0.8/ks/centos8.cfg
label linux7
menu label Auto Install centos Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://10.0.0.8/ks/centos7.cfg
label linux6
menu label Auto Install centos Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.0.0.8/ks/centos6.cfg
label manual
menu label ^ManualInstall Centos Linux 8.0
kernel centos8/vmlinuz
append initrd=centos8/initrd.img
inst.repo=http://10.0.0.8/centos/8/os/x86_64/
label rescue
menu label ^Rescue a CentOS Linux system 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img
inst.repo=http://10.0.0.8/centos/8/os/x86_64/ rescue
label local
menu default
menu label Boot from ^local drive
localboot Oxffff
EOF
echo -e "\e[1;32m服务器以及相关文件配置完成!\e[0m"
安装系统
- 脚本执行完毕,服务器端配置成功
- 在VM新建虚拟机,选择需要安装的版本,并设置硬盘大小为200G(至少150G)
- 注意,CentOS 7/8 应设置内存至少为 2G
- 开机后迅速按esc选择第四项 网络启动(实际上默认也会跳转到安装菜单)
- 随即进入安装菜单界面,根据VM中选择的版本在此处选择对应的选项
- 等待安装
备注
- 脚本内容较长,是因为3个版本的应答文件中都包含了大篇幅的初始化脚本内容
- 用此方式安装,最终得到的ip为
- CentOS 6 = 10.0.0.66
- CentOS 7 = 10.0.0.77
- CentOS 8 = 10.0.0.88
- 若需要自定义ip,可在开始安装之前直接修改脚本内容
- 若有自己写好的应答文件,可直接删除脚本的【30~407行】内容
并将应答文件放入服务器CentOS 8的/var/www/html/ks/
目录下
但文件名必须为centos6/7/8.cfg
- 硬盘大小至少设置150G的原因:应答文件中分区策略使用了150G