1. 使用 kickstart 半自动化安装CentOS系统
1.1 配置yum.repo
[root@centos8 ~]# echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan
[root@centos8 ~]#yum -y install httpd
[root@centos8 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@centos8 ~]# mkdir -p /var/www/html/centos/{6,7,8}/os/x86_64
[root@centos8 ~]# mount /dev/sr2 /var/www/html/centos/8/os/x86_64/
[root@centos8 ~]# mount /dev/sr1 /var/www/html/centos/6/os/x86_64/
[root@centos8 ~]# ll /dev/sr0 /var/www/html/centos/7/os/x86_64/
[root@centos8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sr0 11:0 1 4.5G 0 rom /var/www/html/centos/7/os/x86_64
sr1 11:1 1 3.7G 0 rom /var/www/html/centos/6/os/x86_64
sr2 11:2 1 7.7G 0 rom /var/www/html/centos/8/os/x86_64
1.2 配置应答文件
1.2.1 centos8 的应答文件
[root@centos8 ~]#cat ks8.cfg
ignoredisk --only-use=sda
zerombr
text
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
url --url=http://10.0.0.8/centos/8/os/x86_64/
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network --hostname=centos8.magedu.org
rootpw --iscrypted $6$MWM8M4AAvxS0NS11$xHYITs7XFzxgwNxJrjP3BIkdTurSs2xZmnpPQrKC6yZh.MNyIZbxxv0GjA9MF8ntf62Et4.q3f.xUi8S49AOB.
firstboot --enable
skipx
services --disabled="chronyd"
timezone Asia/Shanghai --isUtc --nontp
part / --fstype="xfs" --ondisk=sda --size=102400
part /data --fstype="xfs" --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
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[Base]
name=base
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/BaseOS/x86_64/os/
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/AppStream/x86_64/os/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
https://mirrors.huaweicloud.com/epel/8/Everything/x86_64/
gpgcheck=0
enabled=1
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/8/extras/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/extras/x86_64/os/
enabled=1
gpgcheck=0
EOF
%end
1.2.2 centos7 的应答文件
[root@centos8 ~]# cat ks7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 123456
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
firstboot --enable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0
network --hostname=centos7.magedu.org
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://10.0.0.8/centos/7/os/x86_64/"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
#ignoredisk --only-use=sda
# Disk partitioning information
part / --fstype="xfs" --size=100000
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="xfs" --size=50000
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.huaweicloud.com/centos/7/os/x86_64/
https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.huaweicloud.com/epel/7/x86_64/
https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
EOF
%end
%packages
@web-server
%end
1.2.3 centos6 的应答文件
[root@centos8 ~]# cat ks6.cfg
firewall --disabled
install
url --url="http://10.0.0.6/centos/6/os/x86_64/"
rootpw --plaintext 123456
auth --useshadow --passalgo=sha512
text
firstboot --disable
keyboard us
lang en_US
selinux --disabled
skipx
logging --level=info
reboot
timezone Africa/Abidjan
network --bootproto=dhcp --device=eth0 --onboot=on
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part / --fstype="ext4" --size=100000
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="ext4" --size=50000
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.huaweicloud.com/centos/6/os/x86_64/
https://mirrors.aliyun.com/centos/6/os/x86_64/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.huaweicloud.com/epel/6/x86_64/
https://mirrors.aliyun.com/epel/6/x86_64/
enabled=1
gpgcheck=0
EOF
%end
%packages
@web-server
%end
1.2.4 将应答文件加载到HTTP服务
[root@centos8 ~]#mkdir -p /var/www/html/ksdir/
[root@centos8 ~]#mv ks*.cfg /var/www/html/ksdir/
1.3 下载启动文件
分别下载centos6,7,8的启动文件
[root@centos8 ~]#wget https://mirrors.aliyun.com/centos/6/isos/x86_64/CentOS-6.10-x86_64-netinstall.iso
[root@centos8 ~]#wget https://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-2003.iso
[root@centos8 ~]#wget https://mirrors.aliyun.com/centos/8/isos/x86_64/CentOS-8.2.2004-x86_64-boot.iso
在主机上装载需要的安装的系统启动文件
进入选择菜单,选择对应的系统
2. 实现pxe自动化安装linux
自动化安装linux主要分3步
第一步:配置DHCP服务
第二步:配置TFTP服务
第三步:配置HTTP服务
2.1 配置HTTP服务
配置HTTP服务的主要作用:
- kickstart文件
- yum repo
首先在充当服务器的主机配置多个光盘
2.1.1 配置yum.repo
[root@centos8 ~]# echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan
[root@centos8 ~]#yum -y install httpd
[root@centos8 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@centos8 ~]# mkdir -p /var/www/html/centos/{6,7,8}/os/x86_64
[root@centos8 ~]# mount /dev/sr2 /var/www/html/centos/8/os/x86_64/
[root@centos8 ~]# mount /dev/sr1 /var/www/html/centos/6/os/x86_64/
[root@centos8 ~]# ll /dev/sr0 /var/www/html/centos/7/os/x86_64/
[root@centos8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sr0 11:0 1 4.5G 0 rom /var/www/html/centos/7/os/x86_64
sr1 11:1 1 3.7G 0 rom /var/www/html/centos/6/os/x86_64
sr2 11:2 1 7.7G 0 rom /var/www/html/centos/8/os/x86_64
2.1.2 配置应答文件
2.1.2.1 centos8 的应答文件
[root@centos8 ~]#cat ks8.cfg
ignoredisk --only-use=sda
zerombr
text
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
url --url=http://10.0.0.8/centos/8/os/x86_64/
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network --hostname=centos8.magedu.org
rootpw --iscrypted $6$MWM8M4AAvxS0NS11$xHYITs7XFzxgwNxJrjP3BIkdTurSs2xZmnpPQrKC6yZh.MNyIZbxxv0GjA9MF8ntf62Et4.q3f.xUi8S49AOB.
firstboot --enable
skipx
services --disabled="chronyd"
timezone Asia/Shanghai --isUtc --nontp
part / --fstype="xfs" --ondisk=sda --size=102400
part /data --fstype="xfs" --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
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[Base]
name=base
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/BaseOS/x86_64/os/
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/AppStream/x86_64/os/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
https://mirrors.huaweicloud.com/epel/8/Everything/x86_64/
gpgcheck=0
enabled=1
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/8/extras/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/extras/x86_64/os/
enabled=1
gpgcheck=0
EOF
%end
2.1.2.2 centos7 的应答文件
[root@centos8 ~]# cat ks7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 123456
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
firstboot --enable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=eth0
network --hostname=centos7.magedu.org
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://10.0.0.8/centos/7/os/x86_64/"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
#ignoredisk --only-use=sda
# Disk partitioning information
part / --fstype="xfs" --size=100000
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="xfs" --size=50000
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.huaweicloud.com/centos/7/os/x86_64/
https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.huaweicloud.com/epel/7/x86_64/
https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
EOF
%end
%packages
@web-server
%end
2.1.2.3 centos6 的应答文件
[root@centos8 ~]# cat ks6.cfg
firewall --disabled
install
url --url="http://10.0.0.6/centos/6/os/x86_64/"
rootpw --plaintext 123456
auth --useshadow --passalgo=sha512
text
firstboot --disable
keyboard us
lang en_US
selinux --disabled
skipx
logging --level=info
reboot
timezone Africa/Abidjan
network --bootproto=dhcp --device=eth0 --onboot=on
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part / --fstype="ext4" --size=100000
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part /data --fstype="ext4" --size=50000
%post
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.huaweicloud.com/centos/6/os/x86_64/
https://mirrors.aliyun.com/centos/6/os/x86_64/
enabled=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.huaweicloud.com/epel/6/x86_64/
https://mirrors.aliyun.com/epel/6/x86_64/
enabled=1
gpgcheck=0
EOF
%end
%packages
@web-server
%end
2.1.3 配置应答文件到服务器
[root@centos8 html]# mkdir ksdir
[root@centos8 html]# cd ksdir/
[root@centos8 ksdir]# cp /root/ks8.cfg ks8.cfg
[root@centos8 ksdir]# cp /root/ks7.cfg ks7.cfg
[root@centos8 ksdir]# cp /root/ks6.cfg ks6.cfg
2.2 配置TFTP服务
配置TFTP服务的主要作用:
- 给主机提供pxelinux.0文件
- 系统启动的文件:vmliunz和initrd.img
- pxelinux.cfg菜单,http服务地址
2.2.1 安装并启动TFTP服务
#安装tftp
[root@centos8 ~]# yum -y install tftp-server
[root@centos8 ~]# #systemctl enable --now tftp
#安装pxelinux.0
[root@centos8 ~]# yum -y install syslinux-nonlinux
2.2.2 配置TFTP
[root@centos8 ~]# cd /var/lib/tftpboot/
#首先复制pxelinux.0文件
[root@centos8 tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
[root@centos8 tftpboot]# mkdir centos{6,7,8}
2.2.2.1 创建centos8的启动文件
[root@centos8 tftpboot]# cp /var/www/html/centos/8/os/x86_64/isolinux/vmlinuz centos8
[root@centos8 tftpboot]# cp /var/www/html/centos/8/os/x86_64/isolinux/initrd.img centos8
[root@centos8 tftpboot]# cp /usr/share/syslinux/menu.c32 .
[root@centos8 tftpboot]# cd /var/www/html/centos/8/os/x86_64/isolinux/
[root@centos8 isolinux]# cp ldlinux.c32 libcom32.c32 libutil.c32 /var/lib/tftpboot/
2.2.2.2 创建centos7的启动文件
[root@centos8 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/vmlinuz centos7
[root@centos8 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/initrd.img centos7
2.2.2.3 创建centos6的启动文件
[root@centos8 tftpboot]# cp /var/www/html/centos/6/os/x86_64/isolinux/vmlinuz centos6
[root@centos8 tftpboot]# cp /var/www/html/centos/6/os/x86_64/isolinux/initrd.img centos6
2.2.2.4 创建菜单文件
[root@centos8 tftpboot]# cp /var/www/html/centos/8/os/x86_64/isolinux/isolinux.cfg pxelinux.cfg/default
[root@centos8 pxelinux.cfg]# cat default
default menu.c32
timeout 60
menu title CentOS Linux
label linux 8
menu label Auto Install CentOS Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://10.0.0.8/ksdir/ks8.cfg
label linux 7
menu label Auto Install CentOS Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://10.0.0.8/ksdir/ks7.cfg
label linux 6
menu label Auto Install CentOS Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.0.0.8/ksdir/ks6.cfg
label rescue
menu label ^Rescue a CentOS Linux system
kernel vmlinuz
append initrd=initrd.img inst.repo=http://10.0.0.8/centos/8/os/x86_64/ rescue quiet
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
2.3 配置DHCP服务
配置DHCP服务的主要作用:
- 给需要安装系统的主机提供IP
- 给主机提供TFTP服务的地址
2.3.1 安装DHCP软件
首先先将网络已有的DHCP服务,如:mware中的DHCP关闭,防止冲突
#安装DHCP软件包,centos7之前是dhcp,centos8是dhcp-server
[root@centos8 ~]#yum -y install dhcp-server
[root@centos8 ~]# systemctl enable --now dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code.
See "systemctl status dhcpd.service" and "journalctl -xe" for details
显示启动服务失败,是由于配置文件没有配置
[root@centos8 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
2.3.2 配置DHCP配置文件
#首先将DHCP包里带的模板复制到DHCP的配置文件
[root@centos8 ~]# cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite '/etc/dhcp/dhcpd.conf'? y
[root@centos8 ~]#cat /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 180.76.76.76,223.5.5.5;
default-lease-time 86400;
max-lease-time 172800;
# 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.0.0.0 netmask 255.255.255.0 {
range 10.0.0.50 10.0.0.100;
range 10.0.0.150 10.0.0.200;
option routers 10.0.0.2;
next-server 10.0.0.8;
filename "pxelinux.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.example.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.example.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;
# }
#}
[root@centos8 ~]# systemctl restart dhcpd