Skip to content

Instantly share code, notes, and snippets.

@wangxianhe
Last active May 7, 2018 07:02
Show Gist options
  • Save wangxianhe/84bdde95f782b9c93d598c463818e0cd to your computer and use it in GitHub Desktop.
Save wangxianhe/84bdde95f782b9c93d598c463818e0cd to your computer and use it in GitHub Desktop.
pxe网络安装centos

由于详细原理较多,此处只是简略介绍,供自己回顾、使用.

服务

dhcp服务

tftp服务

nfs/http服务

kickstart的原理及组件

kickstart的原理及组件

操作步骤

1 关闭防火墙,selinux

2 安装相关软件包

yum -y install dhcp nfs-utils tftp-server xinetd

3 配置nfs服务

新建目录:

mkdir /tftpboot /centos

挂载镜像文件

mount xxxx.iso /centos

在/etc/exports文件中添加:

/centos *(ro,async,no_root_squash)
/tftpboot *(ro,async,no_root_squash)

重启nfs服务

service nfs restart

4 配置tftp服务

修改/etc/xinetd.d/tftp为如下文件:

# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /tftpboot  #-s 表示用/tftpboot作为tftp目录的根目录
        disable                 = no  ##默认是yes,改为no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

重启xinetd服务:service xinetd restart

5 配置kickstart安装文件

可以使用某台手动安装的centos系统下面的/root/anaconda.cfg作为模板,手动修改;

也可以使用system-config-kickstart命令配置ks.cfg文件:

yum -y install system-config-kickstart

system-config-kickstart命令打开图形界面,导入/root/anaconda.cfg,在此界面配置完后保存到/tftpboot/ks.cfg使用。 下面这个ks.cfg可供借鉴:

#platform=x86, AMD64, 或 Intel EM64T
#version=CentOS7
# Install OS instead of upgrade #全新安装而不是升级
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$y10oqPAp$qCWLZ7KzL/QgFYlg3C2R10 #采用加密记录
#rootpw --plaintext 123456 #采用明文记录
# System language
lang en_US
# Firewall configuration #屏蔽防火墙
firewall --disabled
# System authorization information #设定NIS信息
auth  --useshadow  --passalgo=sha512 --enablenis --nisdomain=mydomain.org --nisserver=192.168.100.254
# Use text mode install #安装方式,文本界面,图形的话graphy
text
# SELinux configuration #关闭SELinux
selinux --disabled
# Do not configure the X Window System #不配置X图形界面
skipx

# Use NFS installation media #设定安装方式
nfs --server=192.168.100.254 --dir=/centos
# Use http installation source
#url --url=http://192.168.100.254/centos/7/
# Network information #采用DHCP获取IP
network  --bootproto=dhcp
# Reboot after installation #安装后自动重启,如果BIOS中设置从PXE首先启动,那么会重复安装,请注意,安装后应设置为从先硬盘启动
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information #清除原有分区
clearpart --all
# Disk partitioning information
part / --fstype="xfs" --size=10000
part /boot --fstype="xfs" --size=200
#part /boot/efi --fstype=efi --size=200 #UEFI引导安装时需要,传统LEGACY引导时可屏蔽此选项
part swap --fstype="swap" --size=8000
part /tmp --fstype="xfs" --grow --size=1

%packages #设定所需要的软件包,按需要调整
@base #@表示一组软件包
@network-server
@performance
@system-admin-tools
sdparm
tree
tuned
tuned-utils
ypbind
nfs-utils
vim-enhanced
#-表示从默认软件包的需要去除的软件包
-lvm2
-nano
-pcmciautils
-plymouth
-rfkill
-rsync
-system-config-firewall-tui
-system-config-network-tui
-unzip
-vconfig
-wireless-tools
%end #%packages结尾

%pre #预案装脚本,非必须

%end #%pre结尾

%post #安装后脚本,非必须

%end #%post结尾

下面这个ks.cfg也可借鉴:

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=cn --xlayouts='cn'
# Root password
rootpw --iscrypted $1$nGpBVRAs$lWRLGzGN1cGrF/zBYm25P.
# System language
lang zh_CN

# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
# SELinux configuration
selinux --disabled

# Use NFS installation media
nfs --server=192.168.143.128 --dir=/centos
# System services
services --enabled="chronyd"
ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=ens33
network  --bootproto=dhcp --device=None
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader  --location=mbr --boot-drive=sda
#autopart --type=lvm
# Partition clearing information
clearpart --all
# Disk partitioning information
part /boot --fstype="xfs" --size=200
part swap --fstype="swap" --size=500
part / --fstype="xfs" --grow --size=1

%packages
@^minimal
@anaconda-tools
@backup-client
@base
@compat-libraries
@console-internet
@core
@debugging
@dial-up
@directory-client
@graphical-admin-tools
@hardware-monitoring
@infiniband
@java-platform
@large-systems
@legacy-unix
@mainframe-access
@network-file-system-client
@network-server
@network-tools
@performance
@perl-runtime
@print-client
@remote-system-management
@ruby-runtime
@scientific
@security-tools
@smart-card
@system-management
OpenIPMI
PyPAM
abrt-java-connector
acpid
aide
arptables
arpwatch
atlas
audispd-plugins
augeas
authconfig-gtk
authd
bacula-client
brltty
cachefilesd
ceph-common
clevis-dracut
clevis-udisks2
compat-dapl
compat-opensm-libs
cryptsetup-reencrypt
cups-lpd
dbxtool
device-mapper-persistent-data
dhcp
dnsmasq
dnssec-trigger
dos2unix
dracut-network
dropwatch
dumpet
ebtables
edac-utils
efax
elinks
fence-agents-all
fetchmail
fftw
fftw-devel
fftw-static
finger
finger-server
freeipmi
freeipmi-bmc-watchdog
freeipmi-ipmidetectd
freeradius
ftp
fwupd
fwupdate
gdb-gdbserver
genisoimage
glibc-utils
gnome-disk-utility
gnome-packagekit
gnuplot
gpm
grub2-efi-ia32
grub2-efi-ia32-modules
grub2-efi-x64
grub2-efi-x64-modules
grub2-pc
gsl-devel
hdparm
hmaccalc
hplip
hplip-gui
i2c-tools
intel-cmt-cat
iperf3
ipmitool
ipset
iptraf-ng
iptstate
irssi
isdn4k-utils
java-1.6.0-openjdk
java-1.8.0-openjdk
kabi-yum-plugins
krb5-pkinit
krb5-workstation
ksh
lapack
ldapjdk
lftp
libatomic
libcgroup
libcgroup-tools
libibcommon
libitm
libpfm
libstoragemgmt-netapp-plugin
libstoragemgmt-nstor-plugin
libstoragemgmt-smis-plugin
libstoragemgmt-targetd-plugin
libstoragemgmt-udev
libusnic_verbs
libvma
linuxptp
lksctp-tools
lm_sensors
logwatch
luksmeta
memtest86+
minicom
mipv6-daemon
mkbootdisk
mksh
mpich-3.0-devel
mpich-3.2-devel
mpitests-mvapich2
mpitests-openmpi
mrtg
mtools
mutt
mvapich2-2.0-devel
mvapich2-2.0-doc
mvapich2-2.0-psm-devel
mvapich2-2.2-devel
mvapich2-2.2-doc
mvapich2-2.2-psm-devel
mvapich2-2.2-psm2-devel
ncompress
ncurses-term
netlabel_tools
netsniff-ng
nmap
nscd
nss-pam-ldapd
ntp
numpy
nvmetcli
oddjob
opencryptoki
openhpi
openldap-clients
openlmi
openlmi-account-doc
openlmi-fan
openlmi-fan-doc
openlmi-hardware-doc
openlmi-logicalfile
openlmi-logicalfile-doc
openlmi-networking-doc
openlmi-powermanagement
openlmi-powermanagement-doc
openlmi-realmd
openlmi-realmd-doc
openlmi-service-doc
openlmi-software-doc
openlmi-storage-doc
openlmi-tools
openlmi-tools-doc
openmpi
openmpi-devel
opensc
openscap
openscap-utils
opensm
openwsman-server
oprofile-jit
oscap-anaconda-addon
pam_krb5
papi
pax
perl-App-cpanminus
perl-DBD-SQLite
perl-Date-Calc
perl-Date-Manip
perl-DateTime
perl-DateTime-Format-DateParse
perl-DateTime-Locale
perl-DateTime-TimeZone
perl-LDAP
perl-Mozilla-LDAP
perl-Perl4-CoreLibs
perl-homedir
policycoreutils-gui
pptp
prelink
python-volume_key
quagga
radvd
rdma-core
rdma-core
rear
redhat-lsb-core
redhat-upgrade-dracut
rp-pppoe
rsh
rsh-server
rsyslog-gnutls
rsyslog-gnutls
rsyslog-gssapi
rsyslog-gssapi
rsyslog-mysql
rsyslog-pgsql
rsyslog-relp
rsyslog-relp
rusers
rusers-server
rwho
samba-krb5-printing
samba-winbind
samba-winbind-clients
sblim-cim-client2
sblim-cmpi-fsvol
sblim-cmpi-network
sblim-cmpi-nfsv3
sblim-cmpi-nfsv4
sblim-cmpi-params
sblim-cmpi-sysfs
sblim-cmpi-syslog
sblim-gather
sblim-gather-provider
sblim-indication_helper
sblim-sfcb
sblim-smis-hba
sblim-wbemcli
scap-security-guide-doc
scap-workbench
scipy
sdparm
setools-console
setroubleshoot
sg3_utils
sgpio
shim-ia32
shim-unsigned-ia32
shim-unsigned-x64
sox
squashfs-tools
sssd-dbus
sssd-kcm
sssd-polkit-rules
sssd-tools
sssd-winbind-idmap
star
strongimcv
stunnel
syslinux
system-config-kdump
system-config-kickstart
system-config-users
system-switch-java
systemtap-client
systemtap-initscript
talk
talk-server
tang
tboot
tcp_wrappers
telnet
telnet-server
tftp
tftp-server
tmpwatch
tncfhh
tpm-quote-tools
tpm-tools
tpm2-tools
trousers
tuned-profiles-cpu-partitioning
tuned-utils
udftools
units
usbguard
usnic-tools
uuidd
volume_key
watchdog
whois
wireshark
wireshark-gnome
wireshark-gnome
wodim
x86info
xinetd
ypbind
ypserv
yum-plugin-aliases
yum-plugin-changelog
yum-plugin-tmprepo
yum-plugin-verify
yum-plugin-versionlock
zsh
-NetworkManager
-NetworkManager-team
-NetworkManager-tui
-NetworkManager-wifi
-aic94xx-firmware
-alsa-firmware
-biosdevname
-dracut-config-rescue
-ivtv-firmware
-iwl100-firmware
-iwl1000-firmware
-iwl105-firmware
-iwl135-firmware
-iwl2000-firmware
-iwl2030-firmware
-iwl3160-firmware
-iwl3945-firmware
-iwl4965-firmware
-iwl5000-firmware
-iwl5150-firmware
-iwl6000-firmware
-iwl6000g2a-firmware
-iwl6000g2b-firmware
-iwl6050-firmware
-iwl7260-firmware
-iwl7265-firmware
-libsysfs
-linux-firmware
-microcode_ctl
-postfix

%end

6 pxe引导配置

PXE启动映像文件由syslinux软件包提供,CentOS镜像中已提供。

cp /usr/share/syslinux/pxelinux.0 /tftpboot/
cp /centos/isolinux/{boot.cat, boot.msg, grub.conf, isolinux.bin, splash.png, TRANS, TBL, vesamenu.c32} /tftpboot
cp /centos/images/pxeboot/{vmlinuz, initrd.img} /tftpboot
mkdir /tftpboot/pxelinux.cfg
cp /centos/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default
chmod 644 /tftpboot/pxelinux.cfg/default

修改/tftpboot/pxelinux.cfg/default

下面可用default,可供参考:

default linux
#prompt 1# 不要提示,直接进行安装
timeout 60 #提示时的等待时间

display boot.msg

menu background splash.png
menu title Welcome to CentOS 7.3 x86_64!

label linux
	menu label ^Install CentOS Linux 7 x86_64
	menu default #注意,这里设置默认启动
	kernel vmlinuz
	append initrd=initrd.img inst.ks=nfs:192.168.100.254:/tftpboot/nodes/ inst.repo=nfs:192.168.100.254:/centos ip=dhcp nameserver=202.38.64.7
	#append initrd=initrd.img inst.ks=nfs:192.168.100.254:/tftpboot/ks.cfg inst.repo=nfs:192.168.100.254:/centos ip=dhcp nameserver=202.38.64.7
#主要为上面设置ks文件、内核引导参数等

#以下check和local内容可不用,只要上面内容即可
label check #可选
	menu label Test this ^media & install CentOS Linux 7 x86_64
# menu default #注意,需屏蔽掉此项默认启动
	kernel vmlinuz
	append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet

label local #可选项,设置从硬盘启动
	menu label Boot from ^local drive
	localboot 0
default vesamenu.c32
timeout 20

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label linux
  menu label ^Install CentOS 7
  menu default
  kernel vmlinuz
append initrd=initrd.img inst.ks=nfs:192.168.143.128:/tftpboot/ks.cfg inst.repo=nfs:192.168.143.128:/centos ip=dhcp nameserver=192.168.143.128

label check
  menu label Test this ^media & install CentOS 7
  kernel vmlinuz
append initrd=initrd.img inst.ks=nfs:192.168.143.128:/tftpboot/ks.cfg inst.repo=nfs:192.168.143.128:/centos ip=dhcp nameserver=192.168.143.128

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
	If the system will not boot, this lets you access files
	and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
	If your system is having issues, a problem with your
	system's memory may be the cause. Use this utility to
	see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

7 配置DHCP服务

修改/etc/dhcp/dhcpd.conf: 参考:

#option definitions common to all supported networks...
option domain-name "mydomain.org"; #域名
option domain-name-servers ns1.ustc.edu.cn; #域名服务器

default-lease-time 600;
max-lease-time 7200;

subnet 192.168.100.0 netmask 255.255.255.0 {
	option routers             192.168.100.254;
	option subnet-mask         255.255.255.0;
	option nis-domain          "mydomain.org";
	option domain-name         "mydomain.org";
	option domain-name-servers 202.38.64.7;

	option time-offset              -18000; # Eastern Standard Time
	range dynamic-bootp 192.168.100.1 192.168.100.253;
	default-lease-time 21600;
	max-lease-time 43200;

	host node148 {
		hardware ethernet a0:42:3f:33:49:5f;
		fixed-address 192.168.100.148;
	}
}

option space PXE;
option PXE.mtftp-ip    code 1 = ip-address;
option PXE.mtftp-cport code 2 = unsigned integer 16;
option PXE.mtftp-sport code 3 = unsigned integer 16;
option PXE.mtftp-tmout code 4 = unsigned integer 8;
option PXE.mtftp-delay code 5 = unsigned integer 8;
option architecture-type code 93 = unsigned integer 16; # RFC4578

class "pxeclients" {
	match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
	next-server 192.168.100.254;
	if option architecture-type = 00:07 {
		filename "shim.efi"; #x86_64 UEFI引导
#	} else if option architecture-type = 00:06 {
#		filename "bootia32.efi"; #ia32 UEFI引导,本文后面未涉及
	} else {
		filename "pxelinux.0"; #传统LEGACY引导
	}
}
ddns-update-style interim;
ignore client-updates;

subnet 192.168.143.0 netmask 255.255.255.0 {

# --- default gateway
	option routers			192.168.143.128;
	option subnet-mask		255.255.255.0;

	option nis-domain		"WANG";
#	option domain-name		"WANG";
#	option domain-name-servers	192.168.143.128;

	option time-offset		-18000;	
# Eastern Standard Time
#	option ntp-servers		192.168.143.128;
#	option netbios-name-servers	192.168.143.128;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#	option netbios-node-type 2;

	range dynamic-bootp 192.168.143.200 192.168.143.254;
	default-lease-time 21600;
	max-lease-time 43200;

####### network installation configuration
	allow booting; 
	allow bootp; 
	class "pxeclients" {
	match if substring(option vendor-class-identifier, 0, 9) = "PXEClient"; 
			next-server 192.168.143.128;
			filename "pxelinux.0"; 
	}
####### network installation configuration



	# we want the nameserver to appear at a fixed address
}

重启动DHCP服务:

service dhcpd restart

服务端配置完毕,开启客户端开始自动化网络安装centos操作系统。

参考资料:

http://hmli.ustc.edu.cn/doc/linux/centos-autoinstall.htm

https://www.cnblogs.com/lyq863987322/p/8249867.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment