dhcpd (简体中文)

From ArchWiki

Tango-preferences-desktop-locale.png本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队Tango-preferences-desktop-locale.png

附注: 请完成翻译。(在 Talk:Dhcpd (简体中文)# 中讨论)

dhcpd 是 Internet Systems Consortium DHCP 服务器。它可用作局域网环境中的路由管理。

注意: dhcpd (DHCP (server) daemon) 不是 dhcpcd (DHCP client daemon).

安装

安装 dhcp 包。

使用

dhcpd 包括两个单元文件:dhcpd4.servicedhcpd6.service,可用于控制守护进程。它们分别在 IPv4 和 IPv6 的所有 network interfaces 上启动守护进程。可查看 #Listening on only one interface[损坏的链接:无效的章节] 了解单一网口配置。

配置

为你想使用的接口分配静态 IPv4 地址(在实例中我们将使用 eth0)。这个地址的前 3 字节不能和其他接口的完全相同。

# ip link set up dev eth0
# ip addr add 139.96.30.100/24 dev eth0 # arbitrary address
Tip: 通常有三个预留的网段用于私有网络,它们不会与任何互联网中的主机发生冲突:
  • 192.168/16 (subnet 192.168.0.0, netmask 255.255.0.0)
  • 172.16/12 (subnet 172.16.0.0, netmask 255.240.0.0)
  • 10/8 (for large networks; subnet 10.0.0.0, netmask 255.0.0.0)
另请参阅 RFC 1918

要在启动时分配静态 IP 地址,查看网络配置#静态_IP_地址

默认的 dhcpd.conf 文件包含许多注释的例子,复制一份该文件:

# mv /etc/dhcpd.conf /etc/dhcpd.conf.example

最精简的配置文件像这样:

/etc/dhcpd.conf
option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 139.96.30.100;
subnet 139.96.30.0 netmask 255.255.255.0 {
  range 139.96.30.150 139.96.30.250;
}

如果你要为一台特定的设备提供一个固定的 IP 地址,你可以定义主机块:

/etc/dhcpd.conf
option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 139.96.30.100;
subnet 139.96.30.0 netmask 255.255.255.0 {
  range 139.96.30.150 139.96.30.250;
}
host macbookpro{
  hardware ethernet 70:56:81:22:33:44;
  fixed-address 139.96.30.199;
}

domain-name-servers 选项包含提供给客户的 DNS 服务器地址,这个例子中使用了谷歌的公共 DNS 服务器。如果你知道一个本地的DNS服务器(例如,由你的服务商提供的),那么你应该使用这个 DNS。如果 DNS 服务器部署在本地设备上,应该使用子网络中的地址(如 139.96.30.100)。

subnet-mask and routers defines a subnet mask and a list of available routers on the subnet. In most cases for small networks you can use 255.255.255.0 as a mask and specify an IP address of the machine on which you are configuring DHCP server as a router.

subnet blocks defines options for separate subnets, which are mapped to the network interfaces on which dhcpd is running. In our example this is one subnet 139.96.30.0/24 for single interface eth0, for which we defined the range of available IP addresses. Addresses from this range will be assigned to the connecting clients.

只侦听单一网口

如果你的计算机已经是一个或多个网络中的一部分,那在你的计算机给其他网络的计算机分配 IP 地址时可能会有问题。这可以通过配置 dhcpd 或使用 systemctl 以守护进程的形式启动。

配置 dhcpd

In order to exclude an interface, you must create an empty declaration for the subnet that will be configured on that interface.

This is done by editing the configuration file (for example):

/etc/dhcpd.conf
# No DHCP service in DMZ network (192.168.2.0/24)
subnet 192.168.2.0 netmask 255.255.255.0 {
}

服务文件

There is no service files provided by default to use dhcpd only on one interface so you need to create one. Start by copying the existing service:

# cp /usr/lib/systemd/system/dhcpd4.service /etc/systemd/system/dhcpd4@.service

Then edit the ExecStart line to include the interface:

/etc/systemd/system/dhcpd4@.service
...
[Service]
...
ExecStart=/usr/bin/dhcpd -4 -q -cf /etc/dhcpd.conf -pf /run/dhcpd4/dhcpd.pid %I
...

This is a template unit, which binds it to a particular interface, for example dhcpd4@eth0.service where eth0 is an interface shown with ip link.

用于 PXE

PXE Configuration is done with the following two options:

/etc/dhcpd.conf
next-server 192.168.0.2;
filename "/pxelinux.0";

This section can either be in an entire subnet or just in a host definition. next-server is the IP of the TFTP Server, and filename is the filename of the image to boot. For more information see PXE.

参阅