dhcpcd
dhcpcd 是 DHCP 和 DHCPv6 客戶端,是目前功能最豐富的開源 DHCP 客戶端。項目主頁包含了完整的功能列表。
安裝[編輯 | 編輯原始碼]
dhcpcd-uiAUR 是 dhcpcd 和 GTK 前端,提供了對 wpa_supplicant 的可選支持。功能包括配置對話框、輸入無線網絡的密碼等。 dhcpcd-ui-patchedAUR 是 dhcpcd-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 客戶端:
- 主機名 (或客戶端發送的主機名),
- 所用網卡的 MAC 地址,
- Identity Association ID (身份關聯ID IAID),區分不同使用場景或接口的標識,
- DHCP 唯一標識 (DUID).
詳情請參考 RFC 3315.
DHCP 服務器通過配置決定申請 DHCP ID 租約時,哪些是必須的,哪些是可選的。
如果無法通過 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
.
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
.
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
/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.