dhclient (提供了一种用于配置一个或使用动态主机配置协议多个网络接口)

瑞兹 发表于 2021-01-13 17:57
浏览次数:
在手机上阅读

互联网系统联盟DHCP客户端,dhclient的,提供了一种用于配置一个或使用动态主机配置协议多个网络接口,一个装置BOOTP协议,或者如果这些协议失败,由静态分配一个地址。

查看英文版

目录

1 dhclient 运行系统环境

2 dhclient 描述

3 dhclient 语法

4 dhclient 例子

dhclient 运行系统环境

Unix&Linux

dhclient 描述

DHCP协议允许主机与维护可在一个或多个子网中分配的IP地址列表的中央服务器联系。DHCP客户端可以从该池中请求一个地址,然后将其临时用于网络上的通信。DHCP协议还提供了一种机制,客户端可以通过该机制来了解有关其所连接的网络的重要信息,例如默认路由器的位置,名称服务器的位置等。

启动时,dhclient读取文件dhclient.conf以获取配置说明。然后,它将获取当前系统中配置的所有网络接口的列表。对于每个接口,它尝试使用DHCP协议配置接口。

为了跟踪系统重新启动和服务器重新启动之间的租约,dhclient会在dhclient.leases文件中保留分配给它的租约列表。启动时,读取dhclient.conf文件后,dhclient读取dhclient.leases文件以刷新其内存,了解分配的租约。

当获取新的租约时,它将被附加到dhclient.leases文件的末尾。为了防止文件任意增大,dhclient会不时从其核心租用数据库中创建一个新的dhclient.leases文件。dhclient.leases文件的旧版本将保留为dhclient.leases〜名称,直到下次dhclient重写数据库为止。

万一第一次调用dhclient时(通常在初始系统引导过程中),DHCP服务器不可用,则保留旧租约。在这种情况下,将测试来自dhclient.leases文件的尚未到期的旧租约,如果确定它们是有效的,则将使用这些租约,直到它们过期或DHCP服务器可用为止。

有时可能需要访问不存在DHCP服务器的网络的移动主机可能会预先租用该网络上固定地址的租约。当所有尝试联系DHCP服务器的尝试均失败时,dhclient将尝试验证静态租约,如果成功,将使用该租约直到重新启动。

移动主机也可能会访问某些DHCP不可用但BOOTP可用的网络。在这种情况下,与网络管理员安排BOOTP数据库中的条目可能是有利的,以便主机可以在该网络上快速启动,而不是循环浏览旧租赁列表。

The DHCP protocol allows a host to contact a central server which maintains a list of IP addresses which may be assigned on one or more subnets. A DHCP client may request an address from this pool, and then use it on a temporary basis for communication on network. The DHCP protocol also provides a mechanism whereby a client can learn important details about the network to which it is attached, such as the location of a default router, the location of a name server, etc.

On startup, dhclient reads the file dhclient.conf for configuration instructions. It then gets a list of all the network interfaces that are configured in the current system. For each interface, it attempts to configure the interface using the DHCP protocol.

To keep track of leases across system reboots and server restarts, dhclient keeps a list of leases it was assigned in the dhclient.leases file. On startup, after reading the dhclient.conf file, dhclient reads the dhclient.leases file to refresh its memory about what leases it was assigned.

When a new lease is acquired, it is appended to the end of the dhclient.leases file. To prevent the file from becoming arbitrarily large, from time to time dhclient creates a new dhclient.leases file from its in-core lease database. The old version of the dhclient.leases file is retained under the name dhclient.leases~ until the next time dhclient rewrites the database.

Old leases are kept around in case the DHCP server is unavailable when dhclient is first invoked (generally during the initial system boot process). In that event, old leases from the dhclient.leases file which have not yet expired are tested, and if they are determined to be valid, they are used until either they expire or the DHCP server becomes available.

A mobile host which may sometimes need to access a network on which no DHCP server exists may be preloaded with a lease for a fixed address on that network. When all attempts to contact a DHCP server have failed, dhclient will try to validate the static lease, and if it succeeds, will use that lease until it is restarted.

A mobile host may also travel to some networks on which DHCP is not available but BOOTP is. In that case, it may be advantageous to arrange with the network administrator for an entry on the BOOTP database, so that the host can boot quickly on that network rather than cycling through the list of old leases.

查看英文版

查看中文版

dhclient 语法

dhclient [ -4 | -6 ] [ -S ] [ -N [ -N... ] ] [ -T [ -T... ] ] [ -P [ -P... ] ]
         [ -p port ] [ -d ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ]
         [ -lf lease-file ] [ -pf pid-file ] [ -cf config-file ]
         [ -sf script-file ] [ -s server ] [ -g relay ] [ -n ] [ -nc ] [ -nw ]
         [ -w ] [ -B ] [ -I dhcp-client-identifier ] [ -H host-name ]
         [ -F fqdn.fqdn ] [ -V vendor-class-identifier ]
         [ -R request-option-list ] [ -timeout timeout ] [ -v ] [ --version ]
         [ if0 [ ...ifN ] ]

选件

-4

使用DHCPv4协议获取IPv4地址和配置参数(默认)。

-6

使用DHCPv6协议获取可用的任何IPv6地址以及配置参数。可以使用-S-T-N选项修改DHCPv6模式的功能。

-S

通过DHCPv6执行仅信息请求,以获取无状态配置参数。不建议将此选项与-N-P-T选项结合使用,或在不同的操作模式之间共享租用文件。仅在-6选项下有效。

-N

通过DHCPv6执行常规(IA_NA)地址查询。不建议将此选项与-P-S-T选项结合使用,或在不同的操作模式之间共享租用文件。仅在-6选项下有效。

-T

通过DHCPv6执行临时(IA_TA)地址查询(禁用普通地址查询)。不建议将此选项与-N-P-S选项结合使用,或在不同的操作模式之间共享租用文件。仅在-6选项下有效。

-P

启用IPv6前缀委托(禁用普通地址查询)。不建议将此选项与-N-S-T选项结合使用,或在不同的操作模式之间共享租用文件。仅在-6选项下有效。

-p 

DHCP客户端应侦听和传输的UDP端口号。如果未指定,则dhclient使用默认端口68。此选项主要用于调试目的。如果为客户端指定了其他端口以进行侦听和传输,则客户端还将使用其他目标端口:比指定端口少一个。

-d

强制dhclient作为前台 进程运行。在调试器下运行客户端或在System V系统上的inittab中运行客户端时,此功能很有用。

-e VAR =value

定义其他环境变量的环境,这里的dhclient脚本执行。您可以在命令行上指定多个-e选项。例如:-e IF_METRIC = 1

-q

禁止显示所有终端和日志输出(错误消息除外)。

-1

尝试一次以获得租约。一次失败,退出并输入代码2。

-r

告诉dhclient从服务器释放其当前的租约。DHCP协议不需要这样做,但是某些ISP要求其客户端如果希望释放分配的IP地址,则通知服务器。

-lf 

租约数据库文件的路径。如果未指定,则使用默认的/var/lib/dhclient/dhclient.leases

-pf 

进程标识文件的路径。如果未指定,则使用默认的/var/run/dhclient.pid

-cf 

客户端配置文件的路径。如果未指定,则使用默认的/etc/dhcp/dhclient.conf

-sf 

dhclient租用时调用的网络配置脚本的路径。如果未指定,则使用默认的/ sbin / dhclient-script

-s 

指定服务器IP地址或标准域名以传输DHCP协议消息。通常,dhclient将这些消息传输到255.255.255.255(IP受限广播地址)。覆盖它对于调试目的最有用。

-g 

仅用于调试。将客户端发送的所有数据包的giaddr字段设置为指定的IP地址。不应期望它以任何一致或有用的方式工作。

-n

不要配置任何接口。与-w选项结合使用时最有用。

-nc

不要放弃功能。

通常,如果dhclient在libcap-ng支持下进行编译,则dhclient在启动时会立即放弃大多数功能。虽然更安全,但这极大地限制了dhclient-script中的钩子可以执行的其他操作。(例如,dhclient-script启动或重新启动的所有守护程序也会继承受限的功能,这可能会干扰其正确的操作。)因此,-nc选项可用于防止dhclient丢弃功能。

-nc如果dhclient的未经编译选项被忽略的libcap-ng的支持。

-nw

立即(不等待)成为守护进程,而不是等到获取IP地址后再进行。

-w

即使未找到网络接口,也要继续运行。该omshell程序可以用来通知客户端时,网络接口被添加或移除,因此它可以尝试在接口上配置的IP地址。

-B

在请求数据包中设置BOOTP广播标志,以便服务器将始终广播答复。

-I 

指定dhcp-client-identifier选项以发送到DHCP服务器。

-H 

指定主机名选项以发送到DHCP服务器。该主机名字符串仅包含客户端的主机名前缀,到服务器将追加DDNS,域名域名的选择,如果有的话,以获得客户端的完全合格的域名。该-H选项不能与使用-F选项。

-F 

指定fqdn.fqdn选项以发送到DHCP服务器。此选项不能与-H选项一起使用。该fqdn.fqdn选项必须指定客户端主机,该服务器可以使用动态DNS更新的完整域名。

-V 

指定vendor-class-identifier选项以发送到DHCP服务器。

-R  [  ...]

指定客户端要从服务器请求的选项列表。选项列表必须是单个字符串,由至少由一个命令分隔的选项名称和可选的空格字符组成。默认选项列表为:

subnet-maskbroadcast-addresstime-offsetroutersdomain-namedomain-name-servershost-namenis-domainnis-serversntp-serversinterface-mtu

-R选项不附加选项为默认请求时,它会覆盖默认请求列表。如果您要请求默认请求列表之外的其他选项,请记住这一点。您必须为-R参数指定所有选项名称。

-timeout 

指定在未收到任何响应时dhclient决定无法联系DHCP服务器的时间。

-v

启用详细日志消息。

如果客户端被信号杀死(例如,在关闭或重新启动时),则它将不会在退出时执行dhclient-script。但是,如果使用-r-x正常关闭客户端,则会在关闭时执行dhclient-script,具体原因是需要调用环境表中设置的脚本。

奥马皮

DHCP客户端提供了一些在运行时对其进行控制而不停止它的功能。使用OMAPI(一种用于处理远程对象的API)提供此功能。OMAPI客户端使用TCP / IP连接到客户端,进行身份验证,然后可以检查客户端的当前状态并对其进行更改。

用户程序应该直接使用dhcpctl API或OMAPI本身,而不是直接实现基础的OMAPI协议。Dhcpctl是一个包装程序,用于处理OMAPI不会自动执行的一些内务处理工作。您想要使用客户端执行的大多数操作都可以直接使用omshell命令完成,而不必编写特殊的程序。

控制对象

控制对象使您可以关闭客户端,释放其持有的所有租约并删除其可能添加的任何DNS记录。它还允许您暂停客户端-这将取消配置客户端正在使用的所有接口。然后,您可以重新启动它,这将导致它重新配置这些接口。通常,您会先使客户端暂停,然后再进入休眠模式或在便携式计算机上睡觉。重新上电后,您便可以恢复它。这样,可以在计算机处于休眠或睡眠状态时关闭PC卡,然后在计算机退出休眠或睡眠状态后将其重新初始化为以前的状态。

控制对象具有一个属性-状态属性。要关闭客户端,请将其state属性设置为2。它将自动执行DHCPRELEASE。要暂停它,请将其state属性设置为3。要恢复它,请将其state属性设置为4

dhclient [ -4 | -6 ] [ -S ] [ -N [ -N... ] ] [ -T [ -T... ] ] [ -P [ -P... ] ]
         [ -p port ] [ -d ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ]
         [ -lf lease-file ] [ -pf pid-file ] [ -cf config-file ]
         [ -sf script-file ] [ -s server ] [ -g relay ] [ -n ] [ -nc ] [ -nw ]
         [ -w ] [ -B ] [ -I dhcp-client-identifier ] [ -H host-name ]
         [ -F fqdn.fqdn ] [ -V vendor-class-identifier ]
         [ -R request-option-list ] [ -timeout timeout ] [ -v ] [ --version ]
         [ if0 [ ...ifN ] ]

Options

-4

Use the DHCPv4 protocol to obtain an IPv4 address and configuration parameters (default).

-6

Use the DHCPv6 protocol to obtain whatever IPv6 addresses are available along with configuration parameters. The functionality of DHCPv6 mode may be modified with the -S-T, and -N options.

-S

Perform an information-only request over DHCPv6 to get stateless configuration parameters. It is not recommended to combine this option with the -N-P, or -T options or share lease files between different modes of operation. Only valid with the -6 option.

-N

Perform a normal (IA_NA) address query over DHCPv6. It is not recommended to combine this option with the -P-S, or -T options or share lease files between different modes of operation. Only valid with the -6 option.

-T

Perform a temporary (IA_TA) address query over DHCPv6 (disables normal address query). It is not recommended to combine this option with the -N-P, or -S options or share lease files between different modes of operation. Only valid with the -6 option.

-P

Enable IPv6 prefix delegation (disables normal address query). It is not recommended to combine this option with the -N-S, or -T options or share lease files between different modes of operation. Only valid with the -6 option.

-p 

The UDP port number the DHCP client should listen and transmit. If unspecified, dhclient uses the default port 68. This option is mostly useful for debugging purposes. If a different port is specified for the client to listen and transmit on, the client will also use a different destination port: one less than the specified port.

-d

Force dhclient to run as a foreground process. This is useful when running the client under a debugger, or when running it out of inittab on System V systems.

-e VAR=value

Define additional environment variables for the environment where dhclient-script executes. You may specify multiple -e options on the command line. For example: -e IF_METRIC=1

-q

Suppress all terminal and log output except error messages.

-1

Try once to get a lease. One failure, exit with code 2.

-r

Tell dhclient to release the current lease it has from the server. This is not required by the DHCP protocol, but some ISPs require their clients to notify the server if they wish to release an assigned IP address.

-lf 

Path to the lease database file. If unspecified, the default /var/lib/dhclient/dhclient.leases is used.

-pf 

Path to the process ID file. If unspecified, the default /var/run/dhclient.pid is used.

-cf 

Path to the client configuration file. If unspecified, the default /etc/dhcp/dhclient.conf is used.

-sf 

Path to the network configuration script invoked by dhclient when it gets a lease. If unspecified, the default /sbin/dhclient-script is used.

-s 

Specify the server IP address or fully qualified domain name to transmit DHCP protocol messages. Normally, dhclient transmits these messages to 255.255.255.255 (the IP limited broadcast address). Overriding this is mostly useful for debugging purposes.

-g 

Only for debugging. Set the giaddr field of all packets the client sends to the IP address specified. This should not be expected to work in any consistent or useful way.

-n

Do not configure any interfaces. Most useful combined with the -w option.

-nc

Do not drop capabilities.

Normally, if dhclient was compiled with libcap-ng support, dhclient drops most capabilities immediately upon startup. While more secure, this greatly restricts the additional actions that hooks in dhclient-script can take. (For example, any daemons that dhclient-script starts or restarts will inherit the restricted capabilities as well, which may interfere with their correct operation.) Thus, the -nc option can be used to prevent dhclient from dropping capabilities.

The -nc option is ignored if dhclient was not compiled with libcap-ng support.

-nw

Become a daemon process immediately (nowait) rather than waiting until an IP address is acquired.

-w

Keep running even if no network interfaces are found. The omshell program can be used to notify the client when a network interface is added or removed so it can attempt to configure an IP address on that interface.

-B

Set the BOOTP broadcast flag in request packets so servers will always broadcast replies.

-I 

Specify the dhcp-client-identifier option to send to the DHCP server.

-H 

Specify the host-name option to send to the DHCP server. The host-name string only contains the client's hostname prefix, to which the server will append the ddns-domainname or domain-name options, if any, to derive the fully qualified domain name of the client. The -H option cannot be used with the -F option.

-F 

Specify the fqdn.fqdn option to send to the DHCP server. This option cannot be used with the -H option. The fqdn.fqdn option must specify the complete domain name of the client host, which the server may use for dynamic DNS updates.

-V 

Specify the vendor-class-identifier option to send to the DHCP server.

-R [,...]

Specify the list of options the client is to request from the server. The option list must be a single string consisting of option names separated by at least one command and optional space characters. The default option list is:

subnet-maskbroadcast-addresstime-offsetroutersdomain-namedomain-name-servershost-namenis-domainnis-serversntp-serversinterface-mtu

The -R option does not append options to the default request, it overrides the default request list. Keep this in mind if you want to request an additional option besides the default request list. You have to specify all option names for the -R parameter.

-timeout 

Specify the time after which dhclient will decide that no DHCP servers can be contacted when no responses have been received.

-v

Enable verbose log messages.

If the client is killed by a signal (for example at shutdown or reboot), it will not execute the dhclient-script at exit. However, if you shut the client down gracefully with -r or -x it will execute dhclient-script at shutdown with the specific reason for calling the script set in the environment table.

Omapi

The DHCP client provides some ability to control it while it is running, without stopping it. This capability is provided using OMAPI, an API for manipulating remote objects. OMAPI clients connect to the client using TCP/IP, authenticate, and can then examine the client's current status and make changes to it.

Rather than implementing the underlying OMAPI protocol directly, user programs should use the dhcpctl API or OMAPI itself. Dhcpctl is a wrapper that handles some of the housekeeping chores that OMAPI does not do automatically. Most things you'd want to do with the client can be done directly using the omshell command, rather than having to write a special program.

The control object

The control object allows you to shut the client down, releasing all leases that it holds and deleting any DNS records it may have added. It also allows you to pause the client - this unconfigures any interfaces the client is using. You can then restart it, which causes it to reconfigure those interfaces. You would normally pause the client before going into hibernation or sleep on a laptop computer. You would then resume it after the power comes back. This allows PC cards to be shut down while the computer is hibernating or sleeping, and then reinitialized to their previous state once the computer comes out of hibernation or sleep.

The control object has one attribute - the state attribute. To shut the client down, set its state attribute to 2. It will automatically do a DHCPRELEASE. To pause it, set its state attribute to 3. To resume it, set its state attribute to 4.

查看英文版

查看中文版

dhclient 例子

dhclient eth0

更新主以太网设备的动态分配的IP地址。

dhclient eth0

Renews the dynamically-assigned IP address of a primary Ethernet device.

查看英文版

查看中文版

其他命令行

date | du | dpost | dmesg | dirname | dircmp | dd | deluser | depmod | deroff | df | diff | dos2unix | dig |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条