centos系統搭建PXE網路安裝centos+ubuntu+Windows Centos搭建PXE,安裝部署操作系統 一 . 原理: 1.什麼是PXE: PXE(Pre-boot Execution Environment,預啟動執行環境)是由Intel公司開發的最新技術,工作於Client/Se ...
centos系統搭建PXE網路安裝centos+ubuntu+Windows
Centos搭建PXE,安裝部署操作系統
一 . 原理:
1.什麼是PXE:
PXE(Pre-boot Execution Environment,預啟動執行環境)是由Intel公司開發的最新技術,工作於Client/Server的網路模式,支持工作站通過網路從遠端伺服器下載映像,並由此支持通過網路啟動操作系統,在啟動過程中,終端要求伺服器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)協議下載一個啟動軟體包到本機記憶體中執行,由這個啟動軟體包完成終端基本軟體設置,從而引導預先安裝在伺服器中的終端操作系統。
嚴格來說,PXE 並不是一種安裝方式,而是一種引導方式。進行 PXE 安裝的必要條件是在要安裝的電腦中必須包含一個 PXE 支持的網卡(NIC),即網卡中必須要有 PXE Client。PXE 協議可以使電腦通過網路啟動。此協議分為 Client端和 Server 端,而PXE Client則在網卡的 ROM 中。當電腦引導時,BIOS 把 PXE Client 調入記憶體中執行,然後由 PXE Client 將放置在遠端的文件通過網路下載到本地運行。運行 PXE 協議需要設置 DHCP 伺服器和 TFTP 伺服器。DHCP 伺服器會給 PXE Client(將要安裝系統的主機)分配一個 IP 地址,由於是給 PXE Client 分配 IP 地址,所以在配置 DHCP 伺服器時需要增加相應的 PXE 設置。此外,在 PXE Client 的 ROM 中,已經存在了 TFTP Client,那麼它就可以通過 TFTP 協議到 TFTP Server 上下載所需的文件了。
2.PXE的工作過程:
1. PXE Client 從自己的PXE網卡啟動,向本網路中的DHCP伺服器索取IP;
2. DHCP 伺服器返回分配給客戶機的IP 以及PXE文件的放置位置(該文件一般是放在一臺TFTP伺服器上) ;
3. PXE Client 向本網路中的TFTP伺服器索取pxelinux.0 文件;
4. PXE Client 取得pxelinux.0 文件後之執行該文件;
5. 根據pxelinux.0 的執行結果,通過TFTP伺服器載入內核和文件系統 ;
6. 進入安裝畫面, 此時可以通過選擇HTTP、FTP、NFS 方式之一進行安裝;
詳細工作流程,請參考下麵這幅圖:
二.配置步驟:
1.基本環境:
①PXE搭建系統:CentOS Linux release 7.2.1511 (Core)
②IP地址:192.168.1.1(靜態)
更改項:
BOOTPROTO= static
ONBOOT= yes
添加項:
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=192.168.1.1
DNS2=114.114.114.114
③關閉防火牆:systemctl stop firewalld.service
[root@localhost ~]# systemctl stop firewalld.service ##關閉firewalld防火牆
[root@localhost ~]# systemctl disable firewalld ##關閉firewalld防火牆自啟
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
④關閉selinux:
編輯配置文件:/etc/sysconfig/selinux
[root@localhost ~]# vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX= disabled ##關閉 SELinux,只能重啟生效。
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
因為更改配置文件需要重啟後才能生效,所以使用命令臨時關閉selinux:這種修改立時生效,但重啟後失效。
[root@localhost ~]# getenforce
Enforcing ##強制模式。違反 SELinux 規則的行為將被阻止並記錄到日誌中。
[root@localhost ~]# setenforce 0 ##設置selinux放鬆, 這種修改立時生效,但重啟後失效。
[root@localhost ~]# getenforce
Permissive ##寬容模式。違反 SELinux 規則的行為只會記錄到日誌中。一般為調試用。
⑤因為我是使用的VM虛擬機所以我以光碟機的形式掛在了光碟:
SR0對應:CentOS-7-x86_64-DVD-1511.iso
SR1對應:ubuntu-16.04.6-server-amd64.iso
2.安裝所需服務: dhcp xinetd tftp-server httpd syslinux
yum install dhcp xinetd tftp-server httpd syslinux -y
為了便於編輯配置文件,我提前安裝了vim: yum install -y vim
3.配置TFTP所需環境:
vim /etc/xinetd.d/tftp ##編輯xinetd配置文件管理tftp
[root@localhost ~]# vim /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 /var/lib/tftpboot -c ##所指tftp根目錄
disable = no ##更改為no
per_source = 11
cps = 100 2
flags = IPv4
}
重啟xinetd服務和TFTP服務並使其開機自啟:
[root@localhost ~]# systemctl restart xinetd ##重啟xinetd服務
[root@localhost ~]# systemctl restart tftp ##重啟tftp服務
[root@localhost ~]# systemctl enable tftp ##使tftp服務開機自啟
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
[root@localhost ~]# systemctl enable xinetd ##使xinetd服務開機自啟
根據需求複製指定引導文件到指定位置(請跳轉至第6):
[root@localhost ~]# cp /usr/share/syslinux/* /var/lib/tftpboot/
4.配置DHCP所需環境:
編輯DHCP配置文件:/etc/dhcp/dhcpd.conf
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
allow booting; #定義能夠PXE啟動
allow bootp;
log-facility local4;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.210 192.168.1.220;
option routers 192.168.1.10;
option subnet-mask 255.255.255.0;
filename "pxelinux.0";
default-lease-time 86400;
max-lease-time 172800;
host ns {
next-server 192.168.1.10;
# hardware ethernet 88:51:fb:59:1c:9b;
}
}
重啟DHCP服務並使其開機自啟:
[root@localhost ~]# systemctl restart dhcpd ##重啟dhcp服務
[root@localhost ~]# systemctl enable dhcpd ##使dhcp服務開機自啟
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service. :
5.配置FTP所需環境(本次實驗把HTTP服務改成了FTP服務,給網起設備提供系統):
安裝FTP服務:yum install -y vsftpd
[root@localhost ~]# yum install -y vsftpd
編輯/etc/vsftpd/vsftpd.conf,確保以下設置(ftp根目錄沒有更改,依舊是/var/ftp/):
anonymous_enable=yes
anon_upload_enable=YES ##預設註釋掉了需要取消註釋
anon_umask=022 ##預設local_umask=022也可以
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
重啟vsftpd服務並使其開機自啟:
[root@localhost ~]# systemctl restart vsftpd ##重啟FTP服務
[root@localhost ~]# systemctl enable vsftpd ##使FTP服務開機自啟
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
創建目錄用於掛載centos7系統iso鏡像文件:/var/ftp/c7-64
[root@localhost ~]# mkdir -p /var/ftp/c7-64
掛載centos7系統iso鏡像:
[root@localhost ~]# mount /dev/sr0 /var/ftp/c7-64/
為了每次開機都不用再去掛載推薦設置為自動掛載:
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan 16 16:30:28 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=c6af63a6-4574-481c-aa4d-50cc710ed5bb /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sr0 /var/ftp/c7-64 auto auto 0 0 ##添加這一行
~
mount: /dev/sr0 is write-protected, mounting read-only
6.配置準備系統安裝引導所需文件+環境:
[root@localhost ~]# cp /var/ftp/c7-64/images/pxeboot/vmlinuz /var/lib/tftpboot/vmlinuz.c7-64
[root@localhost ~]# cp /var/ftp/c7-64/images/pxeboot/initrd.img /var/lib/tftpboot/initrd.img.c7-64
[root@localhost ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost pxelinux.cfg]# vim /var/lib/tftpboot/pxelinux.cfg/default
default c7
prompt 1
timeout 100
display boot.msg
label c7
kernel vmlinuz.c7-64
append initrd=initrd.img.c7-64 method=ftp://192.168.1.10/c7-64 devfs=nomount
創建/var/lib/tftpboot/boot.msg用於顯示信息:
[root@localhost ~]# vim /var/lib/tftpboot/boot.msg
####################################################
# Input: #
# c7 to install CentOS7-64 #
# #
# Type Enter directly to install default OS #
# Default is c7 #
###################################################
ok!到此需要安裝系統的機器就可以開機使用PXE啟動安裝centos系統了!