dhcpcd 移植

/************************************************************************
 *                             dhcpcd移植
 * dhcpcd是DHCP client的实现,可以作为后台守护进程运行。
 * 本文介绍移植过程,其中的配置文件并没有深究。
 * 参考链接:
 *     http://www.cnblogs.com/zengjfgit/p/5435126.html
 *     https://wiki.archlinux.org/index.php/Dhcpcd#Running
 *     http://www.linuxfromscratch.org/blfs/view/svn/basicnet/dhcpcd.html
 *                                                          
 *                                                Tony, 2016-5-4,Shenzhen
 ***********************************************************************/
1.下载
    ftp://roy.marples.name/pub/dhcpcd
    我下载的版本是:dhcpcd-6.9.4.tar.xz

2.编译
    解压压缩包 

    ./configure --libexecdir=/lib/dhcpcd --dbdir=/var/lib/dhcpcd

    make CC=arm-linux-gnueabihf-gcc

3.移植
    1. 编译生成的dhcpcd添加到板中

    2. dhcpcd.conf添加到/etc目录 

    3. 将dhcpcd-run-hooks脚本添加到开发板/lib/dhcpcd/中。
       dhcpcd-hooks目录及其内容移动到放到开发板/lib/dhcpcd/目录.
       dhcpcd-run-hooks脚本运行时会调用dhcpcd-hooks中的脚本。

4.运行
    dhcpcd 
    会保持在后台运行。重新插上网线,就能够获取ip。

5.存在问题
5.1 电脑网卡和板子直连,运行dhcpcd也会配置ip
    电脑网卡ip:192.168.2.110
    root@tony:# dhcpcd  
    control_open: Connection refused
    all: IPv6 kernel autoconf disabled
    eth0: IPv6 kernel autoconf disabled
    eth0: adding address fe80::6eec:ebff:fef9:1731
    if_addaddress6: Operation not supported
    script_runreason: /lib/dhcpcd/dhcpcd-run-hooks: WEXITSTATUS 127
    script_runreason: /lib/dhcpcd/dhcpcd-run-hooks: WEXITSTATUS 127
    DUID 00:01:00:01:1c:62:04:1e:6c:ec:eb:f9:17:31
    eth0: IAID eb:f9:17:31
    eth0: soliciting a DHCP lease
    eth0: probing for an IPv4LL address
    eth0: using IPv4LL address 169.254.144.185
    eth0: adding route to 169.254.0.0/16
    eth0: adding default route
    script_runreason: /lib/dhcpcd/dhcpcd-run-hooks: WEXITSTATUS 127
    forked to background, child pid 1922
    root@tony:# ifconfig
    eth0      Link encap:Ethernet  HWaddr 6C:EC:EB:F9:17:31  
              inet addr:169.254.144.185  Bcast:169.254.255.255  Mask:255.255.0.0
              UP BROADCAST RUNNING ALLMULTI MULTICAST  MTU:1500  Metric:1
              RX packets:1142 errors:0 dropped:0 overruns:0 frame:0
              TX packets:578 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:310397 (303.1 KiB)  TX bytes:70897 (69.2 KiB)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:32421 errors:0 dropped:0 overruns:0 frame:0
              TX packets:32421 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:549649752 (524.1 MiB)  TX bytes:549649752 (524.1 MiB)
    
5.2 dhcpcd eth0 
    这样启动的时候,如果没有连接网线,会导致dhcpcd配置超时,启动失败,程序退出。
    root@tony:# dhcpcd eth0    
    control_open: Connection refused
    all: IPv6 kernel autoconf disabled
    eth0: IPv6 kernel autoconf disabled
    eth0: adding address fe80::6eec:ebff:fef9:1731
    if_addaddress6: Operation not supported
    script_runreason: /lib/dhcpcd/dhcpcd-run-hooks: WEXITSTATUS 127
    script_runreason: /lib/dhcpcd/dhcpcd-run-hooks: WEXITSTATUS 127
    eth0: waiting for carrier
    timed out
    dhcpcd exited

 

### 关于 `dhcpcd` 命令的用法及其问题解决 `dhcpcd` 是一款轻量级且强大的 DHCP 客户端工具,广泛用于 Linux 和其他类 Unix 系统中。以下是有关其基本用法以及如何解决常见问题的具体说明。 --- #### 1. `dhcpcd` 的基本命令语法 `dhcpcd` 支持丰富的命令行选项,允许用户灵活地管理和配置网络接口。以下是一些常见的用法示例: - **启动 DHCP 守护程序** 默认情况下,`dhcpcd` 将自动扫描所有可用的网络接口并为其请求 IP 地址: ```bash sudo dhcpcd ``` - **指定单个网络接口** 如果只想为某个特定的网络接口启用 DHCP 功能,可以显式提供接口名称: ```bash sudo dhcpcd eth0 ``` 此处假设目标接口为 `eth0`[^1]。 - **显示帮助信息** 用户可以通过 `-h` 参数查看完整的命令行选项列表: ```bash dhcpcd -h ``` - **禁用特定接口上的 DHCP** 当需要手动配置某些接口时,可以使用 `-n` 参数跳过这些接口: ```bash sudo dhcpcd -n wlan0 ``` - **重新加载配置文件** 在修改了 `/etc/dhcpcd.conf` 文件之后,可以通过 `--reconfigure` 参数让 `dhcpcd` 应用新设置: ```bash sudo dhcpcd --reconfigure eth0 ``` --- #### 2. 配置文件详解 (`/etc/dhcpcd.conf`) 为了自定义 `dhcpcd` 的行为,可以在 `/etc/dhcpcd.conf` 中添加各种指令。例如: - 设置静态 IP 地址: ```plaintext interface eth0 static ip_address=192.168.1.100/24 static routers=192.168.1.1 static domain_name_servers=8.8.8.8 8.8.4.4 ``` - 排除某些域名解析服务器: ```plaintext nohook resolv.conf ``` - 添加额外的路由条目: ```plaintext metric 200 ``` 以上配置片段展示了如何通过编辑配置文件实现更精细的网络管理[^2]。 --- #### 3. 故障排除技巧 如果发现 `dhcpcd` 出现异常或者未能按预期工作,可以尝试以下几种方法定位问题根源: - 查看实时日志输出: ```bash journalctl -u dhcpcd.service -f ``` - 强制进入调试模式运行客户端: ```bash sudo dhcpcd -d eth0 ``` - 清理缓存并重试: 删除旧有的 lease 数据可能有助于修复错误状态: ```bash sudo rm /var/lib/dhcpcd/*.lease sudo systemctl restart dhcpcd ``` - 检验依赖的服务是否正常运作: 确保诸如 `networking` 或者 `systemd-networkd` 不会干扰到 `dhcpcd` 的执行环境[^3]。 --- #### 4. 实际案例分析 假设有这样一种情景——虚拟机宿主机与容器共享同一张网卡却都获得了独立的公网IP地址分配。这可能是由于两者分别启用了各自的DHCP协议栈所引起冲突所致。此时应该考虑统一由单一实体负责整个链路上下游通信事务处理过程[^4]。 ```bash # Example of disabling container's internal dhcp client docker run --net=host ... ``` 上述 Docker 示例演示了如何利用 `--net=host` 参数避免双重 NAT 层次结构带来的麻烦。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值