Difference between revisions of "Dhcpcd (简体中文)"

From ArchWiki
Jump to: navigation, search
(flagged broken section links (interactive))
(Tag: wiki-scripts)
(Resync with English. What a pity. Please re-read ArchWiki Translation Team (简体中文) before starting a new page.)
 
Line 9: Line 9:
 
{{Related|dhcpd}}
 
{{Related|dhcpd}}
 
{{Related articles end}}
 
{{Related articles end}}
{{TranslationStatus (简体中文)|Dhcpcd|2016-12-26|460097}}
+
{{TranslationStatus (简体中文)|Dhcpcd|2018-07-11|460097}}
 
{{Translateme (简体中文)|reason|section}}
 
{{Translateme (简体中文)|reason|section}}
''dhcpcd'' is a DHCP and DHCPv6 client. It is currently the most feature-rich open source DHCP client, see the [http://roy.marples.name/projects/dhcpcd/ home page] for the full list of features.
+
''dhcpcd'' is a DHCP and DHCPv6 client. It is currently the most feature-rich open source DHCP client, see the [https://roy.marples.name/projects/dhcpcd home page] for the full list of features.
  
{{提示|''dhcpcd'' (DHCP '''client''' daemon) is not the same as [[dhcpd]] (DHCP '''(server)''' daemon).}}
+
{{Note|''dhcpcd'' (DHCP '''client''' daemon) is not the same as [[dhcpd]] (DHCP '''(server)''' daemon).}}
  
== 安装 ==
+
== Installation ==
  
The {{Pkg|dhcpcd}} package is available to be [[install]]ed. It is part of the {{Grp|base}} group, so it is likely already installed on your system.
+
The {{Pkg|dhcpcd}} package is part of the {{Grp|base}} group, so it is likely already installed on your system.
  
You might be interested in {{AUR|dhcpcd-ui}}, which is a [[GTK+]] frontend for the ''dhcpcd'' daemon (and optionally [[WPA supplicant]]). It features a configuration dialogue and the ability to enter a pass phrase for wireless networks.
+
{{AUR|dhcpcd-ui}} is a [[GTK+]] frontend for the ''dhcpcd'' daemon, and optionally [[WPA supplicant]]. It features a configuration dialogue and the ability to enter a pass phrase for wireless networks.
  
== 运行 ==
+
{{AUR|dhcpcd-ui-patched}} is a patched version of the {{AUR|dhcpcd-ui}} package. It uses AppIndicator instead of GtkStatusIcon and compiles with gtk3. Has a sharp tray icon when used with [[KDE]].
  
''dhcpcd'' includes two unit files which can be used to [[Enable|control]] the daemon:
+
== Running ==
  
* {{ic|dhcpcd.service}} starts the daemon for ''all'' [[network interfaces]];
+
To start the daemon for ''all'' network interfaces, [[start/enable]] {{ic|dhcpcd.service}}.
* the template unit {{ic|dhcpcd@.service}} binds it to a particular interface, for example {{ic|dhcpcd@''interface''.service}} where ''interface'' is an interface shown with {{ic|ip link}}.
 
  
Using the template unit is recommended; see [[#dhcpcd and systemd network interfaces]]{{Broken section link}} for details.
+
To start the daemon for a specific interface alone, [[start/enable]] the template unit {{ic|dhcpcd@''interface''.service}}, where ''interface'' can be found with [[Network configuration#Listing network interfaces]].
  
To start ''dhcpcd'' manually, run the following command:
+
Using the template unit is recommended; see [[#dhcpcd and systemd network interfaces]] for details.
  
{{hc|# dhcpcd ''eth0''|
+
Alternatively, to start ''dhcpcd'' manually, run the following command:
dhcpcd: version 5.1.1 starting
+
 
dhcpcd: ''eth0'': broadcasting for a lease
+
# dhcpcd ''interface''
...
+
 
dhcpcd: ''eth0'': leased 192.168.1.70 for 86400 seconds
+
In all of the above, you will be assigned a dynamic IP address. To assign a static IP address, see [[#Static profile]].
}}
 
  
== 配置 ==
+
== Configuration ==
  
The main configuration is done in {{ic|/etc/dhcpcd.conf}}, see {{man|5|dhcpcd.conf}}for details. Some of the frequently used options are highlighted below.
+
The main configuration is done in {{ic|/etc/dhcpcd.conf}}. See {{man|5|dhcpcd.conf}} for details. Some of the frequently used options are highlighted below.
  
=== DHCP 静态路由 ===
+
=== DHCP static route(s) ===
  
If you need to add a static route client-side, create a new dhcpcd hook-script in {{ic|/usr/lib/dhcpcd/dhcpcd-hooks}}. The example shows a new hook-script which adds a static route to a VPN subnet on {{ic|10.11.12.0/24}} via a gateway machine at {{ic|192.168.192.5}}:
+
If you need to add a static route client-side, add it to {{ic|/etc/dhcpcd.exit-hook}}. The example shows a new hook-script which adds a static route to a VPN subnet on {{ic|10.11.12.0/24}} via a gateway machine at {{ic|192.168.192.5}}:
  
{{hc|/usr/lib/dhcpcd/dhcpcd-hooks/40-vpnroute|
+
{{hc|/etc/dhcpcd.exit-hook|
 
ip route add 10.11.12.0/24 via 192.168.192.5
 
ip route add 10.11.12.0/24 via 192.168.192.5
 
}}
 
}}
  
The {{ic|40}} prefix means that it is the final hook-script to run when dhcpcd starts.
+
You can add multiple routes to this file.
 
+
=== DHCP Client Identifier ===
=== DHCP 客户端标识 ===
 
  
 
The DHCP client may be uniquely identified in different ways by the server:  
 
The DHCP client may be uniquely identified in different ways by the server:  
Line 63: Line 60:
  
 
It depends on the DHCP-server configuration which options are optional or required to request a DHCP IP lease.  
 
It depends on the DHCP-server configuration which options are optional or required to request a DHCP IP lease.  
{{提示|The ''dhcpcd'' default configuration should be sufficient usually. The listed identifiers are determined automatically and manual configuration changes only be required in case of problems.}}  
+
{{Note|The ''dhcpcd'' default configuration should be sufficient usually. The listed identifiers are determined automatically and manual configuration changes only be required in case of problems.}}  
  
 
If the ''dhcpcd'' default configuration fails to obtain an IP, the following options are available to use in {{ic|dhcpcd.conf}}:  
 
If the ''dhcpcd'' default configuration fails to obtain an IP, the following options are available to use in {{ic|dhcpcd.conf}}:  
Line 71: Line 68:
 
* {{ic|duid}} triggers using a combination of DUID and IAID as identifier.  
 
* {{ic|duid}} triggers using a combination of DUID and IAID as identifier.  
  
The DUID value is set in {{ic|/etc/dhcpcd.duid}}. For efficient DHCP lease operation it is important that it is unique for the system and applies to all network interfaces alike, while the IAID represents an identifier for each of the systems' interfaces (see [http://tools.ietf.org/html/rfc4361#section-6.1 RFC 4361]).  
+
The DUID value is set in {{ic|/var/lib/dhcpcd/duid}}. For efficient DHCP lease operation it is important that it is unique for the system and applies to all network interfaces alike, while the IAID represents an identifier for each of the systems' interfaces (see [https://tools.ietf.org/html/rfc4361#section-6.1 RFC 4361]).  
  
 
Care must be taken on a network running [[Wikipedia:Dynamic DNS|Dynamic DNS]] to ensure that all three IDs are unique. If duplicate DUID values are presented to the DNS server, e.g. in the case where a virtual machine has been cloned and the hostname and MAC have been made unique but the DUID has not been changed, then the result will be that as each client with the duplicated DUID requests a lease the server will remove the predecessor from the DNS record.
 
Care must be taken on a network running [[Wikipedia:Dynamic DNS|Dynamic DNS]] to ensure that all three IDs are unique. If duplicate DUID values are presented to the DNS server, e.g. in the case where a virtual machine has been cloned and the hostname and MAC have been made unique but the DUID has not been changed, then the result will be that as each client with the duplicated DUID requests a lease the server will remove the predecessor from the DNS record.
  
=== 禁用 ARP 检测以加速 DHCP ===
+
=== Static profile ===
  
''dhcpcd'' contains an implementation of a recommendation of the DHCP standard ([http://www.ietf.org/rfc/rfc2131.txt RFC2131] section 2.2) to check via ARP if the assigned IP address is really not taken. This seems mostly useless in home networks, so you can save about 5 seconds on every connect by adding the following line to {{ic|/etc/dhcpcd.conf}}:
+
Required settings are explained in [[Network configuration]]. These typically include the [[network interface]] name, ''IP address'', ''router address'', and ''name server''.
 
 
noarp
 
 
 
This is equivalent to passing {{ic|--noarp}} to {{ic|dhcpcd}}, and disables the described ARP probing, speeding up connections to networks with DHCP.
 
 
 
=== 静态配置文件 ===
 
 
 
Required settings are explained in [[Network configuration#Static IP address]]{{Broken section link}}. These typically include the [[Network_configuration#Device_names|device name]]{{Broken section link}}, ''IP address'', ''router address'', and ''name server''.
 
  
 
Configure a static profile for ''dhcpcd'' in {{ic|/etc/dhcpcd.conf}}, for example:  
 
Configure a static profile for ''dhcpcd'' in {{ic|/etc/dhcpcd.conf}}, for example:  
Line 96: Line 85:
 
}}
 
}}
  
More complicated configurations are possible, for example combining with the {{ic|arping}} option. See {{man|5|dhcpcd.conf}}for details.
+
More complicated configurations are possible, for example combining with the {{ic|arping}} option. See {{man|5|dhcpcd.conf}} for details.
  
==== Fallback 配置文件 ====
+
==== Fallback profile ====
  
It is possible to configure a static profile within ''dhcpcd'' and fall back to it when DHCP lease fails. This is useful particularly for [[wikipedia:Headless computer|headless machines]] such as [[Raspberry Pi]], where the static profile can be used as "recovery" profile to ensure that it is always possible to connect to the machine.
+
It is possible to configure a static profile within ''dhcpcd'' and fall back to it when DHCP lease fails. This is useful particularly for [[wikipedia:Headless computer|headless machines]], where the static profile can be used as "recovery" profile to ensure that it is always possible to connect to the machine.
  
 
The following example configures a {{ic|static_eth0}} profile with {{ic|192.168.1.23}} as IP address, {{ic|192.168.1.1}} as gateway and name server, and makes this profile fallback for interface {{ic|eth0}}.
 
The following example configures a {{ic|static_eth0}} profile with {{ic|192.168.1.23}} as IP address, {{ic|192.168.1.1}} as gateway and name server, and makes this profile fallback for interface {{ic|eth0}}.
Line 116: Line 105:
 
}}
 
}}
  
== 钩子 ==
+
== Hooks ==
  
''dhcpcd'' executes all scripts found in {{ic|/usr/lib/dhcpcd/dhcpcd-hooks/}} in a lexical order. See {{man|5|dhcpcd|url=http://roy.marples.name/man/html5/dhcpcd.conf.html}}{{Dead link|2017|08|08}} and {{man|8|dhcpcd-run-hooks}}for details.
+
''dhcpcd'' executes all scripts found in {{ic|/usr/lib/dhcpcd/dhcpcd-hooks/}} in a lexical order. See {{man|5|dhcpcd.conf}} and {{man|8|dhcpcd-run-hooks}} for details.
  
{{提示|
+
{{Note|
 
* Each script can be disabled using the {{ic|nohook}} option in {{ic|dhcpcd.conf}}.
 
* Each script can be disabled using the {{ic|nohook}} option in {{ic|dhcpcd.conf}}.
 
* The {{ic|env}} option can be used to set an environment variable for '''all''' hooks. For example, you can force the hostname hook to always set the hostname with {{ic|1=env force_hostname=YES}}.
 
* The {{ic|env}} option can be used to set an environment variable for '''all''' hooks. For example, you can force the hostname hook to always set the hostname with {{ic|1=env force_hostname=YES}}.
Line 129: Line 118:
 
=== 10-wpa_supplicant ===
 
=== 10-wpa_supplicant ===
  
创建一个符号链接以激活这个钩子(即便包已更新也应先确认在用版本为最新版):
+
Enable this hook by creating a symbolic link (to ensure that always the current version is used, even after package updates):
  
 
  # ln -s /usr/share/dhcpcd/hooks/10-wpa_supplicant /usr/lib/dhcpcd/dhcpcd-hooks/
 
  # ln -s /usr/share/dhcpcd/hooks/10-wpa_supplicant /usr/lib/dhcpcd/dhcpcd-hooks/
  
{{提示|从 {{Pkg|dhcpcd}}-6.10.0-1 版开始,钩子默认不再自动激活,参阅 [http://roy.marples.name/projects/dhcpcd/info/5d7b3cbea2808602], [http://roy.marples.name/projects/dhcpcd/info/28fd82a29a6d54ad], [http://roy.marples.name/projects/dhcpcd/info/9b0662ecd9a8b839].}}
+
The {{ic|10-wpa_supplicant}} hook, if enabled, automatically launches [[WPA supplicant]] on wireless interfaces. It is started only if:
 
 
{{ic|10-wpa_supplicant}} 这个钩子一旦激活,它将在满足下列条件时自动加载相应无线网络接口的 [[WPA supplicant (简体中文)|WPA supplicant]]
 
  
* 当前没有 ''wpa_supplicant'' 进程侦听该接口;
+
* no ''wpa_supplicant'' process is already listening on that interface.
* 存在某个 ''wpa_supplicant'' 配置文件。''dhcpcd'' 将检查下列文件:
+
* a ''wpa_supplicant'' configuration file exists. ''dhcpcd'' checks
  
 
  /etc/wpa_supplicant/wpa_supplicant-''interface''.conf
 
  /etc/wpa_supplicant/wpa_supplicant-''interface''.conf
Line 145: Line 132:
 
  /etc/wpa_supplicant.conf
 
  /etc/wpa_supplicant.conf
  
上列为默认检查顺序,但可以向 {{ic|/etc/dhcpcd.conf}} 中添加 {{ic|1=env wpa_supplicant_conf=''<配置文件路径>''}} 配置项以自定义搜索路径。
+
by default, in that order, but a custom path can be set by adding {{ic|1=env wpa_supplicant_conf=''configuration_file_path''}} into {{ic|/etc/dhcpcd.conf}}.
 +
 
 +
{{Note|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#Special systemd units|netctl-auto]], ''wpa_supplicant'' is started automatically with {{ic|/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 {{ic|/etc/wpa_supplicant/wpa_supplicant.conf}} file, which only contains dummy values in the default packaged version.
 +
 
 +
To disable the hook, add {{ic|nohook wpa_supplicant}} to {{ic|dhcpcd.conf}}.
 +
 
 +
== Tips and tricks ==
  
{{提示|钩子在找到第一个配置文件后将停止搜索,因此当存在多个''wpa_supplicant'' 配置文件时应做出适当安排,否则 ''dhcpcd'' 可能使用非预期的错误文件。}}
+
=== Speed up DHCP by disabling ARP probing ===
  
如果同时还用 ''wpa_supplicant'' 自身管理无线网络连接,钩子会创建非预期的连接。例如,如果你停用了  ''wpa_supplicant'' ,钩子会再次启动接口。还有,如果使用[[Netctl (简体中文)#自动切换配置|自动切换配置]]{{Broken section link}},''wpa_supplicant'' 将使用 {{ic|/run/network/wpa_supplicant_''interface''.conf}} 配置文件自动启动,因此不仅没必要用钩子再次启动它,而且会导致引导时解析 {{ic|/etc/wpa_supplicant/wpa_supplicant.conf}} 文件错误,因其只包含默认包版本的范例配置。
+
''dhcpcd'' contains an implementation of a recommendation of the DHCP standard ([https://www.ietf.org/rfc/rfc2131.txt RFC2131] section 2.2) to check via ARP if the assigned IP address is really not taken. This seems mostly useless in home networks, so you can save about 5 seconds on every connect by adding the following line to {{ic|/etc/dhcpcd.conf}}:
  
在 {{ic|dhcpcd.conf}} 中添加 {{ic|nohook wpa_supplicant}} 可禁用钩子。
+
noarp
  
== 提示与技巧 ==
+
This is equivalent to passing {{ic|--noarp}} to {{ic|dhcpcd}}, and disables the described ARP probing, speeding up connections to networks with DHCP.
  
=== 移除旧的 DHCP 租约 ===
+
=== Remove old DHCP lease ===
  
The file {{ic|/var/lib/dhcpcd/dhcpcd-''interface''.lease}}, where {{ic|''interface''}} is the name of the interface on which you have a lease, contains the actual DHCP lease reply sent by the DHCP server. It is used to determine the last lease from the server, and its {{ic|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.  
+
The file {{ic|/var/lib/dhcpcd/''interface''.lease}}, where {{ic|''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 {{ic|/var/lib/dhcpcd/''interface''-''ssid''.lease}}, where {{ic|''ssid''}} is the name of the wireless network. It is used to determine the last lease from the server, and its {{ic|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]]{{Broken section link}}). You can test it by stopping ''dhcpcd'' and removing or renaming {{ic|/etc/dhcpcd.duid}}. ''dhcpcd'' will generate a new one on next run.  
+
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 {{ic|/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.
 
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.
  
=== 多重时引导使用不同 IP ===
+
=== Different IPs when multi-booting ===
  
 
If you are dualbooting Arch and OS X 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.  
 
If you are dualbooting Arch and OS X 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.  
Line 175: Line 170:
 
If you are using a [[dnsmasq]] DHCP server, the different DUIDs can be used in appropriate {{ic|1=dhcp-host=}} rules in its configuration.
 
If you are using a [[dnsmasq]] DHCP server, the different DUIDs can be used in appropriate {{ic|1=dhcp-host=}} rules in its configuration.
  
== 排错 ==
+
=== resolv.conf ===
 +
 
 +
''dhcpcd''' by default overwrites [[resolv.conf]].
  
=== 客户端 ID ===
+
This can be stopped by adding the following to the last section of {{ic|/etc/dhcpcd.conf}}:
 +
 
 +
nohook resolv.conf
 +
 
 +
Alternatively, you can create a file called {{ic|/etc/resolv.conf.head}} containing your DNS servers. ''dhcpcd'' will prepend this file to the beginning of {{ic|/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 {{ic|/etc/dhcpcd.conf}}, where {{ic|''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
 +
 
 +
== Troubleshooting ==
 +
 
 +
=== 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:
 
If you are on a network with DHCPv4 that filters Client IDs based on MAC addresses, you may need to change the following line:
Line 193: Line 206:
 
}}
 
}}
  
Else, you may not obtain a lease since the DHCP server may not read your [[wikipedia:DHCPv6|DHCPv6-style]] Client ID correctly. See [http://tools.ietf.org/html/rfc4361 RFC 4361] for more information.
+
Else, you may not obtain a lease since the DHCP server may not read your [[wikipedia:DHCPv6|DHCPv6-style]] Client ID correctly. See [https://tools.ietf.org/html/rfc4361 RFC 4361] for more information.
  
=== 检查 DHCP 问题时首先释放 IP ===
+
=== 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:
 
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:
Line 207: Line 220:
 
You may have to run those two commands many times.
 
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
 
For some (noncompliant) routers, you will not be able to connect properly unless you comment the line
Line 213: Line 226:
 
  require dhcp_server_identifier
 
  require dhcp_server_identifier
  
in {{ic|/etc/dhcpcd.conf}}. This should not cause issues unless you have multiple DHCP servers on your network (not typical); see [http://technet.microsoft.com/en-us/library/cc977442.aspx this page] for more information.
+
in {{ic|/etc/dhcpcd.conf}}. This should not cause issues unless you have multiple DHCP servers on your network (not typical); see [https://technet.microsoft.com/en-us/library/cc977442.aspx this page] for more information.
  
=== dhcpcd systemd 网络接口 ===
+
=== dhcpcd and systemd network interfaces ===
  
 
{{ic|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:  
 
{{ic|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"  
 
  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]]{{Broken section link}}. 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]]{{Broken section link}}.
+
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]].
  
=== 超时延迟 ===
+
=== 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.  
 
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.  
Line 234: Line 247:
 
}}
 
}}
  
To have it wait indefinetely, let the unit restart after it exited:  
+
To have it wait indefinitely, let the unit restart after it exited:  
  
 
{{hc|/etc/systemd/system/dhcpcd@.service.d/dhcpcdrestart.conf|2=
 
{{hc|/etc/systemd/system/dhcpcd@.service.d/dhcpcdrestart.conf|2=
Line 243: Line 256:
 
After making changes, [[systemd#Editing provided units|reload the configuration]].
 
After making changes, [[systemd#Editing provided units|reload the configuration]].
  
== 参阅 ==
+
== Known issues ==
 +
 
 +
=== dhcpcd@.service causes slow startup ===
 +
 
 +
By default the {{ic|dhcpcd@.service}} waits to get an IP address before forking into the background via the {{ic|-w}} flag for ''dhcpcd''. If the unit is enabled, this may cause the boot to wait for an IP address before continuing. To fix this, create a [[systemd#Drop-in files|drop-in file]] for the unit with the following:
 +
 
 +
{{hc|/etc/systemd/system/dhcpcd@.service.d/no-wait.conf|2=
 +
[Service]
 +
ExecStart=
 +
ExecStart=/usr/bin/dhcpcd -b -q %I
 +
}}
 +
 
 +
See also {{Bug|49685}}.
 +
 
 +
== See also ==
  
* {{man|8|dhcpcd}}* {{man|5|dhcpcd.conf}}
+
* {{man|8|dhcpcd}}
 +
* {{man|5|dhcpcd.conf}}

Latest revision as of 05:08, 11 July 2018

翻译状态: 本文是英文页面 Dhcpcd翻译,最后翻译时间:2018-07-11,点击这里可以查看翻译后英文页面的改动。

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: reason

dhcpcd is a DHCP and DHCPv6 client. It is currently the most feature-rich open source DHCP client, see the home page for the full list of features.

Note: dhcpcd (DHCP client daemon) is not the same as dhcpd (DHCP (server) daemon).

Installation

The dhcpcd package is part of the base group, so it is likely already installed on your system.

dhcpcd-uiAUR is a GTK+ frontend for the dhcpcd daemon, and optionally WPA supplicant. It features a configuration dialogue and the ability to enter a pass phrase for wireless networks.

dhcpcd-ui-patchedAUR is a patched version of the dhcpcd-uiAUR package. It uses AppIndicator instead of GtkStatusIcon and compiles with gtk3. Has a sharp tray icon when used with KDE.

Running

To start the daemon for all network interfaces, start/enable dhcpcd.service.

To start the daemon for a specific interface alone, start/enable the template unit dhcpcd@interface.service, where interface can be found with Network configuration#Listing network interfaces.

Using the template unit is recommended; see #dhcpcd and systemd network interfaces for details.

Alternatively, to start dhcpcd manually, run the following command:

# dhcpcd interface

In all of the above, you will be assigned a dynamic IP address. To assign a static IP address, see #Static profile.

Configuration

The main configuration is done in /etc/dhcpcd.conf. See dhcpcd.conf(5) for details. Some of the frequently used options are highlighted below.

DHCP static route(s)

If you need to add a static route client-side, add it to /etc/dhcpcd.exit-hook. The example shows a new hook-script which adds a static route to a VPN subnet on 10.11.12.0/24 via a gateway machine at 192.168.192.5:

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

You can add multiple routes to this file.

DHCP Client Identifier

The DHCP client may be uniquely identified in different ways by the server:

  1. hostname (or the hostname value sent by the client),
  2. MAC address of the network interface controller through which the connection is being made, linked to this is the third,
  3. Identity Association ID (IAID), which is an abstraction layer to differentiate different use-cases and/or interfaces on a single host,
  4. DHCP Unique Identifier (DUID).

For a further description, see RFC 3315.

It depends on the DHCP-server configuration which options are optional or required to request a DHCP IP lease.

Note: The dhcpcd default configuration should be sufficient usually. The listed identifiers are determined automatically and manual configuration changes only be required in case of problems.

If the dhcpcd default configuration fails to obtain an IP, the following options are available to use in dhcpcd.conf:

  • hostname sends the hostname set in /etc/hostname
  • clientid sends the MAC address as identifier
  • iaid <interface> derives the IAID to use for DHCP discovery. It has to be used in an interface block (started by interface <interface>, see [1]), but more frequently the next option is used:
  • duid triggers using a combination of DUID and IAID as identifier.

The DUID value is set in /var/lib/dhcpcd/duid. For efficient DHCP lease operation it is important that it is unique for the system and applies to all network interfaces alike, while the IAID represents an identifier for each of the systems' interfaces (see RFC 4361).

Care must be taken on a network running Dynamic DNS to ensure that all three IDs are unique. If duplicate DUID values are presented to the DNS server, e.g. in the case where a virtual machine has been cloned and the hostname and MAC have been made unique but the DUID has not been changed, then the result will be that as each client with the duplicated DUID requests a lease the server will remove the predecessor from the DNS record.

Static profile

Required settings are explained in Network configuration. These typically include the network interface name, IP address, router address, and name server.

Configure a static profile for dhcpcd in /etc/dhcpcd.conf, for example:

/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

More complicated configurations are possible, for example combining with the arping option. See dhcpcd.conf(5) for details.

Fallback profile

It is possible to configure a static profile within dhcpcd and fall back to it when DHCP lease fails. This is useful particularly for headless machines, where the static profile can be used as "recovery" profile to ensure that it is always possible to connect to the machine.

The following example configures a static_eth0 profile with 192.168.1.23 as IP address, 192.168.1.1 as gateway and name server, and makes this profile fallback for interface 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

Hooks

dhcpcd executes all scripts found in /usr/lib/dhcpcd/dhcpcd-hooks/ in a lexical order. See dhcpcd.conf(5) and dhcpcd-run-hooks(8) for details.

Note:
  • Each script can be disabled using the nohook option in dhcpcd.conf.
  • The env option can be used to set an environment variable for all hooks. For example, you can force the hostname hook to always set the hostname with env force_hostname=YES.

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: describe (at least some) provided hooks. (Discuss in Talk:Dhcpcd (简体中文)#)

10-wpa_supplicant

Enable this hook by creating a symbolic link (to ensure that always 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.

Note: 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, add nohook wpa_supplicant to dhcpcd.conf.

Tips and tricks

Speed up DHCP by disabling ARP probing

dhcpcd contains an implementation of a recommendation of the DHCP standard (RFC2131 section 2.2) to check via ARP if the assigned IP address is really not taken. This seems mostly useless in home networks, so you can save about 5 seconds on every connect by adding the following line to /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 OS X 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 (post XP) the DUID should be stored in the

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

registry key.

On OS X 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.

resolv.conf

dhcpcd' by default overwrites resolv.conf.

This can be stopped 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

Troubleshooting

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.

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, 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

After making changes, reload the configuration.

Known issues

dhcpcd@.service causes slow startup

By default the dhcpcd@.service waits to get an IP address before forking into the background via the -w flag for dhcpcd. If the unit is enabled, this may cause the boot to wait for an IP address before continuing. To fix this, create a drop-in file for the unit with the following:

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

See also FS#49685.

See also