dhcpcd

来自 Arch Linux 中文维基

dhcpcd 是 DHCP 和 DHCPv6 客户端,是目前功能最丰富的开源 DHCP 客户端。项目主页包含了完整的功能列表。

注意: Roy Marple 的 dhcpcd (DHCP client daemon) 和 Internet Systems Consortium 的 dhcpd (DHCP (server) daemon) 是不同的软件。

安装[编辑 | 编辑源代码]

安装 软件包 dhcpcd.

dhcpcd-uiAUR 是 dhcpcd 和 GTK 前端,提供了对 wpa_supplicant 的可选支持。功能包括配置对话框、输入无线网络的密码等。 dhcpcd-ui-patchedAURdhcpcd-uiAUR 的补丁版本,使用 AppIndicator 替代了GtkStatusIcon, 基于 gtk3 编译并提供了 KDE 系统托盘图标。

运行[编辑 | 编辑源代码]

要为 全部 网络接口提供服务,start/enable dhcpcd.service.

要仅为某个网络接口提供服务,start/enable 模板单元 dhcpcd@interface.service,其中的 interface 名通过 这里 进行查询。

建议使用模板单元方式,不管是那种方式,都会分配一个动态 IP 地址。要分配静态IP地址,请阅读静态配置部分。

配置[编辑 | 编辑源代码]

主配置文件是 /etc/dhcpcd.conf,详情请参考 dhcpcd.conf(5),下面会介绍一些常用选项。

DHCP 静态路由[编辑 | 编辑源代码]

如果要在客户端设置静态路由,请使用 /etc/dhcpcd.exit-hook. 下面的例子中添加了一个 VPN 子网 10.11.12.0/24 到网关 192.168.192.5 的静态路由:

/etc/dhcpcd.exit-hook
ip route add 10.11.12.0/24 via 192.168.192.5

可以在文件中配置多个路由。

DHCP 客户标识[编辑 | 编辑源代码]

服务器可以通过下列方式不同的 DHCP 客户端:

  1. 主机名 (或客户端发送的主机名),
  2. 所用网卡的 MAC 地址,
  3. Identity Association ID (身份关联ID IAID),区分不同使用场景或接口的标识,
  4. DHCP 唯一标识 (DUID).

详情请参考 RFC 3315.

DHCP 服务器通过配置决定申请 DHCP ID 租约时,哪些是必须的,哪些是可选的。

注意: dhcpcd 默认配置的是最常用的方式。服务器会自动确认上述标识,只有在出现问题时才需要额外配置。

如果无法通过 dhcpcd 默认配置获取 IP 地址,可以在 dhcpcd.conf 中尝试如下配置:

  • hostname 发送 /etc/hostname 中配置的主机名
  • clientid 发送 MAC 地址作为标识
  • iaid <interface> 生成并发送 IAID,可以在接口块(interface <interface>)中使用,参考: [1]),下面的选项更常用:
  • duid 将联合使用 DUID 和 IAID 作为标识.

DUID 的数值配置在 /var/lib/dhcpcd/duid 中,为了保证 DHCP 租约的有效性,需要保证 DUID 在整个网络中是唯一的,而 IAID 需要能区分每一个接口(RFC 4361).

如果运行的是 动态 DNS,请确保三个都是唯一的。如果网络中出现重复的 DUID,例如克隆的虚拟机中,有不同的主机名和 MAC 地址,但是没有修改 DUID 时,最新获取IP地址的客户端会清除之前 DUID 获取的地址。

静态配置[编辑 | 编辑源代码]

需要的配置在 网络配置 中均有说明,通常包含 网络接口 名称、IP 地址、路由地址和域名服务器。

/etc/dhcpcd.conf 中为 dhcpcd 配置静态地址:

/etc/dhcpcd.conf
interface eth0
static ip_address=192.168.0.10/24	
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8

还可以进行更复杂的配置,比如 arping 选项,详情请参考 dhcpcd.conf(5)

备用配置[编辑 | 编辑源代码]

可以为 dhcpcd 添加备用设置,当 DHCP 续租失败时使用,在 无显示设备机器 特别有用,动态地址无法获取时,使用静态地址作为备用,确保机器有可用的网络连接。

下面示例为 static_eth0 配置了 192.168.1.23 静态地址, 192.168.1.1 网关,并将此配置设置为 eth0 的备用配置。

/etc/dhcpcd.conf
# define static profile
profile static_eth0
static ip_address=192.168.1.23/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
interface eth0
fallback static_eth0

钩子[编辑 | 编辑源代码]

dhcpcd 会按字母顺序执行 /usr/lib/dhcpcd/dhcpcd-hooks/ 中配置的钩子,详情请参考 dhcpcd.conf(5)dhcpcd-run-hooks(8)

注意:
  • 可以在 dhcpcd.conf 中使用 nohook 禁用钩子.
  • 可以用 env 选项为所有钩子设置环境变量。例如要强制设置主机名,请使用 env force_hostname=YES.

Tango-view-fullscreen.png这篇文章的某些内容需要扩充。Tango-view-fullscreen.png

原因: describe (at least some) provided hooks. (在 Talk:Dhcpcd 中讨论)

10-wpa_supplicant[编辑 | 编辑源代码]

Enable this hook by creating a symbolic link, which ensures the current version is used, even after package updates:

# ln -s /usr/share/dhcpcd/hooks/10-wpa_supplicant /usr/lib/dhcpcd/dhcpcd-hooks/

The 10-wpa_supplicant hook, if enabled, automatically launches wpa_supplicant on wireless interfaces. It is started only if:

  • no wpa_supplicant process is already listening on that interface.
  • a wpa_supplicant configuration file exists. dhcpcd checks
/etc/wpa_supplicant/wpa_supplicant-interface.conf
/etc/wpa_supplicant/wpa_supplicant.conf
/etc/wpa_supplicant-interface.conf
/etc/wpa_supplicant.conf

by default, in that order, but a custom path can be set by adding env wpa_supplicant_conf=configuration_file_path into /etc/dhcpcd.conf.

注意: The hook stops at the first configuration file found, thus you should take this into consideration if you have several wpa_supplicant configuration files, otherwise dhcpcd might end up using the wrong file.

If you manage wireless connections with wpa_supplicant itself, the hook may create unwanted connection events. For example, if you stop wpa_supplicant the hook may bring the interface up again. Also, if you use netctl-auto, wpa_supplicant is started automatically with /run/network/wpa_supplicant_interface.conf for config, so starting it again from the hook is unnecessary and may result in boot-time parse errors of the /etc/wpa_supplicant/wpa_supplicant.conf file, which only contains dummy values in the default packaged version.

To disable the hook remove the symbolic link you added, or add nohook wpa_supplicant to dhcpcd.conf.

小技巧[编辑 | 编辑源代码]

Speed up DHCP by disabling ARP probing[编辑 | 编辑源代码]

dhcpcd contains an implementation of a recommendation of the DHCP standard (RFC 2131) to verify via ARP if the assigned IP is not used by something else. This is usually not needed in home networks, so it is possible to save about 5 seconds on every connect by disabling it:

/etc/dhcpcd.conf
noarp

This is equivalent to passing --noarp to dhcpcd, and disables the described ARP probing, speeding up connections to networks with DHCP.

Remove old DHCP lease[编辑 | 编辑源代码]

The file /var/lib/dhcpcd/interface.lease, where interface is the name of the interface on which you have a lease, contains the actual DHCP lease reply sent by the DHCP server. For a wireless interface, the filename is /var/lib/dhcpcd/interface-ssid.lease, where ssid is the name of the wireless network. It is used to determine the last lease from the server, and its mtime attribute is used to determine when it was issued. This last lease information is then used to request the same IP address previously held on a network, if it is available. If you do not want that, simply delete this file.

If the DHCP server still assigns the same IP address, this may happen because it is configured to keep the assignment stable and recognizes the requesting DHCP client id or DUID (see #DHCP Client Identifier). You can test it by stopping dhcpcd and removing or renaming /var/lib/dhcpcd/duid. dhcpcd will generate a new one on next run.

Keep in mind that the DUID is intended as persistent machine identifier across reboots and interfaces. If you are transferring the system to new computer, preserving this file should make it appear as old one.

Different IPs when multi-booting[编辑 | 编辑源代码]

If you are dualbooting Arch and macOS or Windows and want each to receive different IP addresses, you can exert control about the IPs leased by specifying a different DUID in each operating system installation.

In Windows the DUID should be stored in the

\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\Dhcpv6DUID 

registry key.

On macOS it is directly accessible in Network\adapter\dhcp preferences panel.

If you are using a dnsmasq DHCP server, the different DUIDs can be used in appropriate dhcp-host= rules in its configuration.

/etc/resolv.conf[编辑 | 编辑源代码]

If resolvconf is available DNS information will be sent to it, if not, then dhcpcd itself will write to /etc/resolv.conf.

/etc/resolv.conf overwriting can be stopped by disabling the hook /usr/lib/dhcpcd/dhcpcd-hooks/20-resolv.conf. Do so by adding the following to the last section of /etc/dhcpcd.conf:

nohook resolv.conf

Alternatively, you can create a file called /etc/resolv.conf.head containing your DNS servers. dhcpcd will prepend this file to the beginning of /etc/resolv.conf.

Or you can configure dhcpcd to use the same DNS servers every time. To do this, add the following line at the end of your /etc/dhcpcd.conf, where dns-server-ip-addressses is a space separated list of DNS IP addresses.

static domain_name_servers=dns-server-ip-addresses

For example, to set it to Google's DNS servers:

static domain_name_servers=8.8.8.8 8.8.4.4
提示:When using openresolv, DNS servers can instead be set in /etc/resolvconf.conf. This way they will not get overwritten by any resolvconf supporting software.

问题解决[编辑 | 编辑源代码]

Client ID[编辑 | 编辑源代码]

If you are on a network with DHCPv4 that filters Client IDs based on MAC addresses, you may need to change the following line:

/etc/dhcpcd.conf
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 Client ID as per RFC4361. 
duid

To:

/etc/dhcpcd.conf
# Use the hardware address of the interface for the Client ID (DHCPv4).
clientid

Else, you may not obtain a lease since the DHCP server may not read your DHCPv6-style Client ID correctly. See RFC 4361 for more information.

Check DHCP problem by releasing IP first[编辑 | 编辑源代码]

A problem may occur when DHCP gets a wrong IP assignment, such as when two routers are tied together through a VPN. The router that is connected through the VPN may be assigning IP address. To fix it, as root, release the IP address:

# dhcpcd -k

Then request a new one:

# dhcpcd

You may have to run those two commands many times.

Problems with noncompliant routers[编辑 | 编辑源代码]

For some (noncompliant) routers, you will not be able to connect properly unless you comment the line

require dhcp_server_identifier

in /etc/dhcpcd.conf. This should not cause issues unless you have multiple DHCP servers on your network (not typical); see this page for more information.

dhcpcd and systemd network interfaces[编辑 | 编辑源代码]

dhcpcd.service can be enabled without specifying an interface. This may, however, create a race condition at boot with systemd-udevd trying to apply a predictable network interface name:

error changing net interface name wlan0 to wlp4s0: Device or resource busy" 

To avoid it, enable dhcpcd per interface it should bind to as described in #Running. The downside of the template unit is, however, that it does not support hot-plugging of a wired connection and will fail if the network cable is not connected. To work-around the failure, see #Timeout delay.

It is also possible to use denyinterfaces or allowinterfaces in dhcpcd.conf(5) to stop dhcpcd from binding to kernel names, for example

denyinterfaces wlan* eth*

Timeout delay[编辑 | 编辑源代码]

If dhcpcd operates on a single interface and fails to obtain a lease after 30 seconds (for example when the server is not ready or the cable not plugged), it will exit with an error.

To have dhcpcd wait indefinitely for one-time, edit the unit and set the timeout option to 0:

/etc/systemd/system/dhcpcd@.service.d/timeout.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dhcpcd -w -q -t 0 %I

To have it wait indefinitely, let the unit restart after it exited:

/etc/systemd/system/dhcpcd@.service.d/dhcpcdrestart.conf
[Service]
Restart=always

已知问题[编辑 | 编辑源代码]

dhcpcd@.service 降低启动速度[编辑 | 编辑源代码]

在默认配置中, dhcpcd@.service 会在获取 IP 地址后进入守护进程(dhcpcd -w )。启用单元后,会等待 IP 地址的分配,影响启动速度,要修正这个问题,请创建 下面的 drop-in file:

/etc/systemd/system/dhcpcd@.service.d/no-wait.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dhcpcd -b -q %I

See also FS#49685.

参阅[编辑 | 编辑源代码]