您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux中如何部署PXE网络
## 1. PXE网络概述
### 1.1 什么是PXE
PXE(Preboot eXecution Environment)是由Intel设计的协议,它使计算机能够通过网络启动操作系统。PXE工作于客户端/服务器模式,允许客户端计算机从服务器获取启动镜像和配置文件,无需本地存储设备。
### 1.2 PXE的工作原理
PXE启动过程分为几个关键阶段:
1. 客户端发送DHCP请求获取IP地址
2. DHCP服务器返回IP地址并告知TFTP服务器位置
3. 客户端连接TFTP服务器下载引导程序(如pxelinux.0)
4. 通过引导程序加载内核和初始化内存盘
5. 最终启动完整操作系统
### 1.3 PXE的应用场景
- 大规模操作系统部署
- 无盘工作站环境
- 系统恢复和维护
- 自动化测试环境搭建
## 2. 部署前的准备工作
### 2.1 硬件需求
- 服务器:至少2核CPU,4GB内存,100GB存储空间
- 网络:千兆以太网环境
- 客户端:支持PXE启动的网卡
### 2.2 软件需求
- Linux发行版(推荐CentOS/RHEL或Ubuntu Server)
- DHCP服务器(ISC DHCP或dnsmasq)
- TFTP服务器(tftpd-hpa或atftpd)
- HTTP/NFS服务器(用于存放安装镜像)
- Syslinux工具包
### 2.3 网络规划
| 项目 | 示例值 |
|---------------|------------------|
| 服务器IP | 192.168.1.100 |
| 子网掩码 | 255.255.255.0 |
| DHCP地址池 | 192.168.1.150-200|
| 网关地址 | 192.168.1.1 |
## 3. 安装和配置必要服务
### 3.1 安装DHCP服务器
```bash
# CentOS/RHEL
sudo yum install dhcp -y
# Ubuntu/Debian
sudo apt install isc-dhcp-server -y
配置示例(/etc/dhcp/dhcpd.conf):
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.150 192.168.1.200;
option routers 192.168.1.1;
option domain-name-servers 8.8.8.8;
filename "pxelinux.0";
next-server 192.168.1.100;
}
# CentOS/RHEL
sudo yum install tftp-server syslinux-tftpboot -y
# Ubuntu/Debian
sudo apt install tftpd-hpa syslinux-common -y
配置示例(/etc/default/tftpd-hpa):
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS=":69"
TFTP_OPTIONS="--secure"
sudo cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
sudo mkdir -p /var/lib/tftpboot/pxelinux.cfg
/var/lib/tftpboot/pxelinux.cfg/default:
DEFAULT menu.c32
PROMPT 0
TIMEOUT 300
ONTIMEOUT local
MENU TITLE PXE Boot Menu
LABEL local
MENU LABEL Boot from local disk
LOCALBOOT 0
LABEL centos7
MENU LABEL Install CentOS 7
KERNEL centos7/vmlinuz
APPEND initrd=centos7/initrd.img inst.repo=http://192.168.1.100/centos7 ks=http://192.168.1.100/ks.cfg
LABEL ubuntu2004
MENU LABEL Install Ubuntu 20.04
KERNEL ubuntu2004/linux
APPEND initrd=ubuntu2004/initrd.gz auto=true url=http://192.168.1.100/ubuntu2004/preseed.cfg
以CentOS 7为例:
sudo mkdir -p /var/www/html/centos7
sudo mount -o loop CentOS-7-x86_64-DVD-2009.iso /mnt
sudo cp -r /mnt/* /var/www/html/centos7/
sudo umount /mnt
# 复制内核和initrd
sudo cp /var/www/html/centos7/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/
/var/www/html/ks.cfg:
install
url --url="http://192.168.1.100/centos7"
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp
rootpw --plaintext mypassword
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr
clearpart --all --initlabel
part / --fstype ext4 --size 10240
part swap --size 2048
part /home --fstype ext4 --size 5120
%packages
@^minimal
@core
vim-enhanced
%end
%post
# 添加自定义脚本
%end
/var/www/html/ubuntu2004/preseed.cfg:
d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layoutcode string us
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string ubuntu
d-i netcfg/get_domain string local
d-i mirror/country string manual
d-i mirror/http/hostname string archive.ubuntu.com
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean true
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i passwd/user-fullname string Ubuntu User
d-i passwd/username string ubuntu
d-i passwd/user-password password insecure
d-i passwd/user-password-again password insecure
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
tasksel tasksel/first multiselect standard
d-i pkgsel/update-policy select none
d-i pkgsel/include string openssh-server vim
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
sudo firewall-cmd --permanent --add-service=dhcp
sudo firewall-cmd --permanent --add-service=tftp
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
sudo setsebool -P tftp_home_dir on
sudo setsebool -P httpd_read_user_content on
sudo chcon -R -t tftpdir_rw_t /var/lib/tftpboot
sudo systemctl enable --now dhcpd tftp httpd
sudo systemctl status dhcpd tftp httpd
客户端无法获取IP:
TFTP超时错误:
引导文件加载失败:
# 添加x86和ARM支持
sudo mkdir -p /var/lib/tftpboot/efi64
sudo cp /usr/share/syslinux/efi64/* /var/lib/tftpboot/efi64/
配置iSCSI目标:
sudo yum install targetcli -y
sudo targetcli
/> backstores/block create name=disk1 dev=/dev/sdb
/> iscsi/ create iqn.2023-08.com.example:pxe.diskless
/> iscsi/iqn.2023-08.com.example:pxe.diskless/tpg1/luns/ create /backstores/block/disk1
/> iscsi/iqn.2023-08.com.example:pxe.diskless/tpg1/acls/ create iqn.2023-08.com.example:client1
/> exit
与Ansible集成示例:
- name: Deploy PXE Server
hosts: pxeserver
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop:
- dhcp
- tftp-server
- httpd
- syslinux
- name: Configure DHCP
template:
src: dhcpd.conf.j2
dest: /etc/dhcp/dhcpd.conf
notify: restart dhcpd
- name: Deploy OS images
unarchive:
src: "{{ item }}"
dest: /var/www/html/
remote_src: yes
loop: "{{ os_images }}"
通过本文的步骤,您已经成功搭建了一个功能完整的PXE服务器环境。这种部署方式特别适合需要频繁安装操作系统的场景,可以显著提高系统部署效率。随着技术的演进,PXE也可以与现代化的部署工具如Cobbler或Foreman结合,构建更加强大的自动化部署平台。
提示:在实际生产环境中,建议先在小规模网络中进行测试,确认所有配置无误后再推广到整个网络环境。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。