在 Ubuntu 14.04 中配置 PXE 服务器
PXE 安装 tftp
![](/data/attachment/album/201511/08/233125iu4dcjdgjs4mrgp8.jpg)


PXE（<ruby> 预启动执行环境 <rp>  （ </rp> <rt>  Preboot Execution Environment </rt> <rp>  ） </rp></ruby>）服务器允许用户从网络中启动 Linux 发行版并且可以不需要 Linux ISO 镜像就能同时在数百台 PC 中安装。如果你客户端的计算机没有 CD/DVD 或USB 引导盘，或者如果你想在大型企业中同时安装多台计算机，那么 PXE 服务器可以帮你节省时间和金钱。


在这篇文章中，我们将告诉你如何在 Ubuntu 14.04 配置 PXE 服务器。


### 配置网络


开始前，你需要先设置 PXE 服务器使用静态 IP。在你的系统中要使用静态 IP 地址，需要编辑 “/etc/network/interfaces” 文件。


打开 “/etc/network/interfaces” 文件.



```
sudo nano /etc/network/interfaces

```

作如下修改:



```
# 回环网络接口
auto lo
iface lo inet loopback
# 主网络接口
auto eth0
iface eth0 inet static
address 192.168.1.20
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8

```

保存文件并退出。这将设置其 IP 地址为“192.168.1.20”。然后重新启动网络服务。



```
sudo /etc/init.d/networking restart

```

### 安装 DHCP, TFTP 和 NFS:


DHCP，TFTP 和 NFS 是 PXE 服务器的重要组成部分。首先，需要更新你的系统并安装所有需要的软件包。


为此，运行以下命令：



```
sudo apt-get update
sudo apt-get install isc-dhcp-Server inetutils-inetd tftpd-hpa syslinux nfs-kernel-Server

```

### 配置 DHCP 服务:


DHCP 代表<ruby> 动态主机配置协议 <rp>  （ </rp> <rt>  Dynamic Host Configuration Protocol </rt> <rp>  ） </rp></ruby>，它主要用于动态分配网络配置参数，如用于接口和服务的 IP 地址。在 PXE 环境中，DHCP 服务器允许客户端请求并自动获得一个 IP 地址来访问网络。


1、编辑 “/etc/default/dhcp3-server” 文件.



```
sudo nano /etc/default/dhcp3-server

```

作如下修改:



```
INTERFACES="eth0"

```

保存 (Ctrl + o) 并退出 (Ctrl + x) 文件.


2、编辑 “/etc/dhcp3/dhcpd.conf” 文件:



```
sudo nano /etc/dhcp/dhcpd.conf

```

作如下修改:



```
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.21 192.168.1.240;
option subnet-mask 255.255.255.0;
option routers 192.168.1.20;
option broadcast-address 192.168.1.255;
filename "pxelinux.0";
next-Server 192.168.1.20;
}

```

保存文件并退出。


3、启动 DHCP 服务.



```
sudo /etc/init.d/isc-dhcp-server start

```

### 配置 TFTP 服务器:


TFTP 是一种文件传输协议，类似于 FTP，但它不用进行用户认证也不能列出目录。TFTP 服务器总是监听网络上的 PXE 客户端的请求。当它检测到网络中有 PXE 客户端请求 PXE 服务时，它将提供包含引导菜单的网络数据包。


1、配置 TFTP 时,需要编辑 “/etc/inetd.conf” 文件.



```
sudo nano /etc/inetd.conf

```

作如下修改:



```
tftp dgram udp wait root /usr/sbin/in.tftpd /usr/sbin/in.tftpd -s /var/lib/tftpboot

```

保存文件并退出。


2、编辑 “/etc/default/tftpd-hpa” 文件。



```
sudo nano /etc/default/tftpd-hpa

```

作如下修改:



```
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="[:0.0.0.0:]:69"
TFTP_OPTIONS="--secure"
RUN_DAEMON="yes"
OPTIONS="-l -s /var/lib/tftpboot"

```

保存文件并退出。


3、 使用 `xinetd` 让 boot 服务在每次系统开机时自动启动，并启动tftpd服务。



```
sudo update-inetd --enable BOOT
sudo service tftpd-hpa start

```

4、检查状态。



```
sudo netstat -lu

```

它将如下所示:



```
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:tftp *:*

```

### 配置 PXE 启动文件


现在，你需要将 PXE 引导文件 “pxelinux.0” 放在 TFTP 根目录下。为 TFTP 创建目录结构，并从 “/usr/lib/syslinux/” 复制 syslinux 提供的所有引导程序文件到 “/var/lib/tftpboot/” 下，操作如下：



```
sudo mkdir /var/lib/tftpboot
sudo mkdir /var/lib/tftpboot/pxelinux.cfg
sudo mkdir -p /var/lib/tftpboot/Ubuntu/14.04/amd64/
sudo cp /usr/lib/syslinux/vesamenu.c32 /var/lib/tftpboot/
sudo cp /usr/lib/syslinux/pxelinux.0 /var/lib/tftpboot/

```

#### 设置 PXELINUX 配置文件


PXE 配置文件定义了 PXE 客户端启动时显示的菜单，它能引导并与 TFTP 服务器关联。默认情况下，当一个 PXE 客户端启动时，它会使用自己的 MAC 地址指定要读取的配置文件，所以我们需要创建一个包含可引导内核列表的默认文件。


编辑 PXE 服务器配置文件，使用有效的安装选项。


编辑 “/var/lib/tftpboot/pxelinux.cfg/default”：



```
sudo nano /var/lib/tftpboot/pxelinux.cfg/default

```

作如下修改:



```
DEFAULT vesamenu.c32
TIMEOUT 100
PROMPT 0
MENU INCLUDE pxelinux.cfg/PXE.conf
NOESCAPE 1
LABEL Try Ubuntu 14.04 Desktop
MENU LABEL Try Ubuntu 14.04 Desktop
kernel Ubuntu/vmlinuz
append boot=casper netboot=nfs nfsroot=192.168.1.20:/var/lib/tftpboot/Ubuntu/14.04/amd64
initrd=Ubuntu/initrd.lz quiet splash
ENDTEXT
LABEL Install Ubuntu 14.04 Desktop
MENU LABEL Install Ubuntu 14.04 Desktop
kernel Ubuntu/vmlinuz
append boot=casper automatic-ubiquity netboot=nfs nfsroot=192.168.1.20:/var/lib/tftpboot/Ubuntu/14.04/amd64
initrd=Ubuntu/initrd.lz quiet splash
ENDTEXT

```

保存文件并退出。


编辑 “/var/lib/tftpboot/pxelinux.cfg/pxe.conf” 文件。



```
sudo nano /var/lib/tftpboot/pxelinux.cfg/pxe.conf

```

作如下修改:



```
MENU TITLE PXE Server
NOESCAPE 1
ALLOWOPTIONS 1
PROMPT 0
MENU WIDTH 80
MENU ROWS 14
MENU TABMSGROW 24
MENU MARGIN 10
MENU COLOR border 30;44 #ffffffff #00000000 std

```

保存文件并退出。


### 为 PXE 服务器添加 Ubuntu 14.04 桌面启动镜像


对于这一步需要 Ubuntu 内核和 initrd 文件。要获得这些文件，你需要 Ubuntu 14.04 桌面 ISO 镜像。你可以通过以下命令下载 Ubuntu 14.04 ISO 镜像到 /mnt 目录：



```
sudo cd /mnt
sudo wget http://releases.ubuntu.com/14.04/ubuntu-14.04.3-desktop-amd64.iso

```

**注意**: 下载用的 URL 可能会改变，因为 ISO 镜像会进行更新。如果上面的网址无法访问，看看[这个网站](http://releases.ubuntu.com/14.04/)，了解最新的下载链接。


挂载 ISO 文件，使用以下命令将所有文件复制到 TFTP文件夹中：



```
sudo mount -o loop /mnt/ubuntu-14.04.3-desktop-amd64.iso /media/
sudo cp -r /media/* /var/lib/tftpboot/Ubuntu/14.04/amd64/
sudo cp -r /media/.disk /var/lib/tftpboot/Ubuntu/14.04/amd64/
sudo cp /media/casper/initrd.lz /media/casper/vmlinuz /var/lib/tftpboot/Ubuntu/

```

### 将导出的 ISO 目录配置到 NFS 服务器上


现在，你需要通过 NFS 协议来设置“安装源镜像（ Installation Source Mirrors）”。你还可以使用 HTTP 和 FTP 来安装源镜像。在这里，我已经使用 NFS 输出 ISO 内容。


要配置 NFS 服务器，你需要编辑 “/etc/exports” 文件。



```
sudo nano /etc/exports

```

作如下修改:



```
/var/lib/tftpboot/Ubuntu/14.04/amd64 *(ro,async,no_root_squash,no_subtree_check)

```

保存文件并退出。为使更改生效，输出并启动 NFS 服务。



```
sudo exportfs -a
sudo /etc/init.d/nfs-kernel-server start

```

现在，你的 PXE 服务器已经准备就绪。


### 配置网络引导 PXE 客户端


PXE 客户端可以是任何支持 PXE 网络引导的计算机系统。现在，你的客户端只需要在系统的 BIOS 中设置 “从网络引导（Boot From Network）” 选项就可以启动并安装 Ubuntu 14.04 桌面。


现在准备出发吧 - 用网络引导启动你的 PXE 客户端计算机，你现在应该看到一个子菜单，显示了我们创建的 Ubuntu 14.04 桌面的菜单项。


![pxe](/data/attachment/album/201511/08/233127m0lsvog6wk00solv.png)


### 结论


配置使用 PXE 服务器从网络启动安装能提高效率和节省时间。你可以在本地网络中同时安装数百个客户端。所有你需要的只是一个 PXE 服务器和能启动 PXE 的客户端。试试吧，如果这个对你有用请让我们知道。


参考:


* [PXE Server wiki](https://en.wikipedia.org/wiki/Preboot_Execution_Environment)
* [PXE Server Ubuntu](https://help.ubuntu.com/community/PXEInstallServer)


图片来源: [fupsol*unl*20](https://www.flickr.com/photos/jhcalderon/3681926417/)




---


via: <https://www.maketecheasier.com/configure-pxe-server-ubuntu/>


作者：[Hitesh Jethva](https://www.maketecheasier.com/author/hiteshjethva/) 译者：[strugglingyouth](https://github.com/strugglingyouth) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
PXE (Preboot Execution Environment) Server allows the user to boot a Linux distribution from a network and install it on hundreds of PCs at a time without any Linux iso images. If your client’s computers don’t have CD/DVD or USB drives, or if you want to set up multiple computers at the same time in a large enterprise, then PXE server can be used to save money and time.

In this article we will show you how you can configure a PXE server in Ubuntu 14.04.

## Configure Networking

To get started, you need to first set up your PXE server to use a static IP. To set up a static IP address in your system, you need to edit the “/etc/network/interfaces” file.

1. Open the “/etc/network/interfaces” file.

sudo nano /etc/network/interfaces

Add/edit as described below:

# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 192.168.1.20 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8

Save the file and exit. This will set its IP address to “192.168.1.20”. Restart the network service.

sudo /etc/init.d/networking restart

## Install DHCP, TFTP and NFS:

DHCP, TFTP and NFS are essential components for configuring a PXE server. First you need to update your system and install all necessary packages.

For this, run the following commands:

sudo apt-get update sudo apt-get install isc-dhcp-Server inetutils-inetd tftpd-hpa syslinux nfs-kernel-Server

## Configure DHCP Server:

DHCP stands for Dynamic Host Configuration Protocol, and it is used mainly for dynamically distributing network configuration parameters such as IP addresses for interfaces and services. A DHCP server in PXE environment allow clients to request and receive an IP address automatically to gain access to the network servers.

1. Edit the “/etc/default/dhcp3-server” file.

sudo nano /etc/default/dhcp3-server

Add/edit as described below:

INTERFACES="eth0"

Save (Ctrl + o) and exit (Ctrl + x) the file.

2. Edit the “/etc/dhcp3/dhcpd.conf” file:

sudo nano /etc/dhcp/dhcpd.conf

Add/edit as described below:

default-lease-time 600; max-lease-time 7200; subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.21 192.168.1.240; option subnet-mask 255.255.255.0; option routers 192.168.1.20; option broadcast-address 192.168.1.255; filename "pxelinux.0"; next-Server 192.168.1.20; }

Save the file and exit.

3. Start the DHCP service.

sudo /etc/init.d/isc-dhcp-server start

## Configure TFTP Server:

TFTP is a file-transfer protocol which is similar to FTP. It is used where user authentication and directory visibility are not required. The TFTP server is always listening for PXE clients on the network. When it detects any network PXE client asking for PXE services, then it provides a network package that contains the boot menu.

1. To configure TFTP, edit the “/etc/inetd.conf” file.

sudo nano /etc/inetd.conf

Add/edit as described below:

tftp dgram udp wait root /usr/sbin/in.tftpd /usr/sbin/in.tftpd -s /var/lib/tftpboot

Save and exit the file.

2. Edit the “/etc/default/tftpd-hpa” file.

sudo nano /etc/default/tftpd-hpa

Add/edit as described below:

TFTP_USERNAME="tftp" TFTP_DIRECTORY="/var/lib/tftpboot" TFTP_ADDRESS="[:0.0.0.0:]:69" TFTP_OPTIONS="--secure" RUN_DAEMON="yes" OPTIONS="-l -s /var/lib/tftpboot"

Save and exit the file.

3. Enable boot service for `inetd`

to automatically start after every system reboot and start `tftpd`

service.

sudo update-inetd --enable BOOT sudo service tftpd-hpa start

4. Check status.

sudo netstat -lu

It will show the following output:

Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:tftp *:*

## Configure PXE boot files

Now you need the PXE boot file “pxelinux.0” to be present in the TFTP root directory. Make a directory structure for TFTP, and copy all the bootloader files provided by syslinux from the “/usr/lib/syslinux/” to the “/var/lib/tftpboot/” path by issuing the following commands:

sudo mkdir /var/lib/tftpboot sudo mkdir /var/lib/tftpboot/pxelinux.cfg sudo mkdir -p /var/lib/tftpboot/Ubuntu/14.04/amd64/ sudo cp /usr/lib/syslinux/vesamenu.c32 /var/lib/tftpboot/ sudo cp /usr/lib/syslinux/pxelinux.0 /var/lib/tftpboot/

**Set up PXELINUX configuration file**

The PXE configuration file defines the boot menu displayed to the PXE client when it boots up and contacts the TFTP server. By default, when a PXE client boots up, it will use its own MAC address to specify which configuration file to read, so we need to create that default file that contains the list of kernels which are available to boot.

Edit the PXE Server configuration file with valid installation options.

To edit “/var/lib/tftpboot/pxelinux.cfg/default,”

sudo nano /var/lib/tftpboot/pxelinux.cfg/default

Add/edit as described below:

DEFAULT vesamenu.c32 TIMEOUT 100 PROMPT 0 MENU INCLUDE pxelinux.cfg/PXE.conf NOESCAPE 1 LABEL Try Ubuntu 14.04 Desktop MENU LABEL Try Ubuntu 14.04 Desktop kernel Ubuntu/vmlinuz append boot=casper netboot=nfs nfsroot=192.168.1.20:/var/lib/tftpboot/Ubuntu/14.04/amd64 initrd=Ubuntu/initrd.lz quiet splash ENDTEXT LABEL Install Ubuntu 14.04 Desktop MENU LABEL Install Ubuntu 14.04 Desktop kernel Ubuntu/vmlinuz append boot=casper automatic-ubiquity netboot=nfs nfsroot=192.168.1.20:/var/lib/tftpboot/Ubuntu/14.04/amd64 initrd=Ubuntu/initrd.lz quiet splash ENDTEXT

Save and exit the file.

Edit the “/var/lib/tftpboot/pxelinux.cfg/pxe.conf” file.

sudo nano /var/lib/tftpboot/pxelinux.cfg/pxe.conf

Add/edit as described below:

MENU TITLE PXE Server NOESCAPE 1 ALLOWOPTIONS 1 PROMPT 0 MENU WIDTH 80 MENU ROWS 14 MENU TABMSGROW 24 MENU MARGIN 10 MENU COLOR border 30;44 #ffffffff #00000000 std

Save and exit the file.

## Add Ubuntu 14.04 Desktop Boot Images to PXE Server

For this, Ubuntu kernel and initrd files are required. To get those files, you need the Ubuntu 14.04 Desktop ISO Image. You can download the Ubuntu 14.04 ISO image in the /mnt folder by issuing the following command:

sudo cd /mnt sudo wget http://releases.ubuntu.com/14.04/ubuntu-14.04.3-desktop-amd64.iso

**Note**: the download URL might change as the ISO image is updated. Check out this [website](https://releases.ubuntu.com/14.04/) for the latest download link if the above URL is not working.

Mount the ISO file, and copy all the files to the TFTP folder by issuing the following commands:

sudo mount -o loop /mnt/ubuntu-14.04.3-desktop-amd64.iso /media/ sudo cp -r /media/* /var/lib/tftpboot/Ubuntu/14.04/amd64/ sudo cp -r /media/.disk /var/lib/tftpboot/Ubuntu/14.04/amd64/ sudo cp /media/casper/initrd.lz /media/casper/vmlinuz /var/lib/tftpboot/Ubuntu/

## Configure NFS Server to Export ISO Contents

Now you need to setup Installation Source Mirrors via NFS protocol. You can also use `http`

and `ftp`

for Installation Source Mirrors. Here I have used NFS to export ISO contents.

To configure the NFS server, you need to edit the “/etc/exports” file.

sudo nano /etc/exports

Add/edit as described below:

/var/lib/tftpboot/Ubuntu/14.04/amd64 *(ro,async,no_root_squash,no_subtree_check)

Save and exit the file. For the changes to take effect, export and start NFS service.

sudo exportfs -a sudo /etc/init.d/nfs-kernel-server start

Now your PXE Server is ready.

## Configure Network Boot PXE Client

A PXE client can be any computer system with a PXE network boot enable option. Now your clients can boot and install Ubuntu 14.04 Desktop by enabling “Boot From Network” options from their systems BIOS.

You’re now ready to go – start your PXE Client Machine with the network boot enable option, and you should now see a sub-menu showing for your Ubuntu 14.04 Desktop that we created.

## Conclusion

Configuring network boot installation using PXE server is efficient and a time-saving method. You can install hundreds of client at a time in your local network. All you need is a PXE server and PXE enabled clients. Try it out, and let us know if this works for you.

Reference: [PXE Server wiki](https://en.wikipedia.org/wiki/Preboot_Execution_Environment) [PXE Server Ubuntu](https://help.ubuntu.com/community/PXEInstallServer)

Image credit: [fupsol_unl_20](https://www.flickr.com/photos/jhcalderon/3681926417/)

Our latest tutorials delivered straight to your inbox