Centos搭建PXE,安装部署操作系统
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系统了!