udhcpc
此页面记录了 Busybox 中默认 DHCP 客户端 udhcpc
的工作原理。
配置
当在文件 /etc/network/interfaces 中的接口名称中添加 "dhcp
" 时,busybox 中的默认 DHCP 客户端 udhcpc
会被网络脚本 /sbin/ifup 和 /sbin/ifdown 调用。
udhcpc 的默认行为由脚本 /usr/share/udhcpc/default.script 驱动
/etc/network/interfaces 中 DHCP 接口的条目将驱动 udhcpc
命令行。例如
/etc/network/interfaces 的内容
将在命令行上设置这些参数
udhcpc -i eth0 -x hostname:myhostname
hostname 将把 DHCP 选项发送到服务器,以告知服务器此客户端的名称。
您可以将任意命令行参数添加到 /etc/network/interfaces 中的 udhcpc_opts
设置中。 请参阅主网络文章中 udhcpc_opts
的示例。
udhcpc
的默认配置可能会被 /etc/udhcpc/udhcpc.conf 覆盖。 请参阅 /usr/share/udhcpc/default.script 以了解如何使用这些值。
udhcpc.conf 接受以下键:值对
键 | 默认值 | 可能的值 | 含义 |
---|---|---|---|
RESOLV_CONF | /etc/resolv.conf | no ; NO ; - | 不覆盖或 resolv.conf 的替代路径 |
NO_DNS | - | <接口名称列表> | 防止在每个接口上覆盖 resolv.conf |
NO_GATEWAY | - | <接口名称列表> | 忽略 DHCP 路由的接口列表 |
IF_METRIC | - | <度量值> | 路由度量的偏移值 |
IF_PEER_DNS | yes | <除 yes 以外的任何内容> | 请改用 RESOLV_CONF 或 NO_DNS |
/etc/udhcpc/udhcpc.conf 示例
/etc/udhcpc/udhcpc.conf 的内容
自定义脚本可以作为 /etc/udhcpc/pre-* 和 /etc/udhcpc/post-* 添加,以便在 DHCP 事件 deconfig/renew/bound 之前/之后运行。 自定义脚本必须设置为 root 可执行,例如 chmod 744
。 参考自定义脚本示例
故障排除

更改 ADSL 的 MTU
例如,/etc/udhcpc/post-bound/mtu 可能包含以下内容,将接口 MTU 从默认值 (1500) 更改为 1492,这在使用 ADSL(使用 PPPoE)时很有用(PPPoE 为其自己的标头使用 8 个字节)
r=$(/sbin/ip route | grep ^default | head -n 1) # 需要 {{pkg|iproute2}} 软件包,而不是 busybox 的 "ip",来更改 mtu /sbin/ip route replace $r mtu 1492
当客户端绑定到新 IP 时,我需要重启我的防火墙(它替换了 Alpine 中的 iptables
脚本),所以我将以下行添加到函数 bound()
中,紧跟在 'resolvconf
' 之后
# rc-service iptables reload
重新加载会删除所有防火墙规则,重新获取内部和外部 IP,并重写规则。 我确信有更好的方法。