从零开始学习 sg200x 多核开辟之 eth0 dhcpc 设置

[复制链接]
发表于 2025-12-25 04:48:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前面已经先容过 sophpi 的启动过程和 eth0 静态 IP 所在设置。不外静态 IP 在使用的时间比力不通用,本文先容 eth0 主动使能并设置 dhcp 功能。
udhcpc

udhcpc 是 BusyBox 工具会集的一个组件,用于从 DHCP 服务器获取网络设置信息,如 IP 所在、子网掩码、默认网关和 DNS 服务器等。它是一个轻量级的工具,非常得当资源受限的嵌入式体系。
手工设置网络接口

在使用 udhcpc 之前,须要确保网络接口已经启用并处于活动状态,我们要设置的网络接口是 eth0。
  1. [root@sg200x]~# ifconfig eth0 up
复制代码
这将启动 udhcpc 客户端,实验在名为 eth0 的网络接口上获取 IP 所在。
  1. [root@sg200x]~# udhcpc -h
  2. udhcpc: option requires an argument: h
  3. BusyBox v1.33.0 (2024-08-20 07:13:20 CST) multi-call binary.
  4. Usage: udhcpc [-fbqRB] [-a[MSEC]] [-t N] [-T SEC] [-A SEC/-n]
  5.         [-i IFACE] [-s PROG] [-p PIDFILE]
  6.         [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...
  7.         -i IFACE        Interface to use (default eth0)
  8.         -s PROG         Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
  9.         -p FILE         Create pidfile
  10.         -B              Request broadcast replies
  11.         -t N            Send up to N discover packets (default 3)
  12.         -T SEC          Pause between packets (default 3)
  13.         -A SEC          Wait if lease is not obtained (default 20)
  14.         -b              Background if lease is not obtained
  15.         -n              Exit if lease is not obtained
  16.         -q              Exit after obtaining lease
  17.         -R              Release IP on exit
  18.         -f              Run in foreground
  19.         -S              Log to syslog too
  20.         -a[MSEC]        Validate offered address with ARP ping
  21.         -r IP           Request this IP address
  22.         -o              Don't request any options (unless -O is given)
  23.         -O OPT          Request option OPT from server (cumulative)
  24.         -x OPT:VAL      Include option OPT in sent packets (cumulative)
  25.                         Examples of string, numeric, and hex byte opts:
  26.                         -x hostname:bbox - option 12
  27.                         -x lease:3600 - option 51 (lease time)
  28.                         -x 0x3d:0100BEEFC0FFEE - option 61 (client id)
  29.                         -x 14:'"dumpfile"' - option 14 (shell-quoted)
  30.         -F NAME         Ask server to update DNS mapping for NAME
  31.         -V VENDOR       Vendor identifier (default 'udhcp VERSION')
  32.         -C              Don't send MAC as client identifier
  33. Signals:
  34.         USR1    Renew lease
  35.         USR2    Release lease
复制代码
在 udhcpc 常用下令可选参数如下:


  • -i:指定接口名
  • -s:指定指定一个脚本文件,该文件将在收到 DHCP 下发的参数时被实验
  • -d:答应 udhcpc 运行时打印出调试信息
udhcpc还支持很多高级选项,比方:


  • -t:指定实验获取IP所在的次数。
  • -T:指定每次实验之间的延长时间(秒)。
比方:
  1. [root@sg200x]~# udhcpc -i eth0 -t 5 -T 2 -s /path/to/custom/script
复制代码
使用 udhcpc 获取 IP 所在

接下来,我们使用udhcpc来获取IP所在和其他网络设置信息。
  1. [root@sg200x]~# udhcpc -i eth0
  2. udhcpc (v1.30.1) started
  3. udhcpc: started, v1.33.0
  4. udhcpc: sending discover
  5. udhcpc: sending select for 192.168.188.167
  6. udhcpc: lease of 192.168.188.167 obtained, lease time 864000
  7. deleting routers
  8. adding dns 192.168.188.1
复制代码
udhcpc 将主动从 DHCP 服务器获取设置信息。再次使用 ifconfig 检察网络信息:
  1. [root@sg200x]~# ifconfig
  2. eth0      Link encap:Ethernet  HWaddr 72:94:A5:19:79:23  
  3.           inet addr:192.168.188.167  Bcast:192.168.188.255  Mask:255.255.255.0
  4.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  5.           RX packets:202 errors:0 dropped:130 overruns:0 frame:0
  6.           TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
  7.           collisions:0 txqueuelen:1000
  8.           RX bytes:51828 (50.6 KiB)  TX bytes:684 (684.0 B)
  9.           Interrupt:21
  10. lo        Link encap:Local Loopback  
  11.           inet addr:127.0.0.1  Mask:255.0.0.0
  12.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  13.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  14.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  15.           collisions:0 txqueuelen:1000
  16.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
复制代码
已经乐成获取到 IP所在,而且已经添加了默认网关和 DNS 服务器。
  1. [root@sg200x]~# vi /etc/resolv.conf
  2. search fritz.box # eth0
  3. nameserver 192.168.188.1 # eth0
复制代码
并可以正常使用 ping 测试:
  1. [root@sg200x]~# ping www.baidu.com
  2. PING www.baidu.com (180.101.50.242): 56 data bytes
  3. 64 bytes from 180.101.50.242: seq=0 ttl=50 time=13.834 ms
  4. 64 bytes from 180.101.50.242: seq=1 ttl=50 time=12.120 ms
  5. 64 bytes from 180.101.50.242: seq=2 ttl=50 time=12.001 ms
  6. 64 bytes from 180.101.50.242: seq=3 ttl=50 time=11.658 ms
  7. ^C
  8. --- www.baidu.com ping statistics ---
  9. 4 packets transmitted, 4 packets received, 0% packet loss
  10. round-trip min/avg/max = 11.658/12.403/13.834 ms
复制代码
主动启动方式一

前文 从零开始学习 sg200x 多核开辟之 eth0 主动使能并设置静态IP 已经先容了怎样在开机时启动 eth0,可以修改 /etc/network/interfaces 文件,添加如下内容:
  1. [root@sg200x]~# vi /etc/network/interfaces
  2. # interface file auto-generated by buildroot
  3. auto lo
  4. iface lo inet loopback
  5. auto eth0
  6. iface eth0 inet dhcp
复制代码


  • auto eth0: 表现网卡随机启动;
  • iface eth0 inet dhcp: 表现网卡 ip 所在主动获取
重启后可以发现 eth0 已经获取到了 IP 所在:
  1. [root@sg200x]~# ifconfig
  2. eth0      Link encap:Ethernet  HWaddr 16:4B:9E:5B:2D:6A  
  3.           inet addr:192.168.188.168  Bcast:192.168.188.255  Mask:255.255.255.0
  4.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  5.           RX packets:136 errors:0 dropped:74 overruns:0 frame:0
  6.           TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
  7.           collisions:0 txqueuelen:1000
  8.           RX bytes:46826 (45.7 KiB)  TX bytes:684 (684.0 B)
  9.           Interrupt:21
  10. lo        Link encap:Local Loopback  
  11.           inet addr:127.0.0.1  Mask:255.0.0.0
  12.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  13.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  14.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  15.           collisions:0 txqueuelen:1000
  16.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
复制代码
通过 /etc/init.d/ 下的 S40network 脚本步伐主动启动的。
主动启动方式二

方式一是通过修改 /etc/network/interfaces 文件,也可以修改 /etc/init.d/S40network 文件完成启动。
添加如下内容:
  1. [root@sg200x]~# vi /etc/init.d/S40network
  2. #!/bin/sh
  3. #
  4. # Start the network....
  5. #
  6. # Debian ifupdown needs the /run/network lock directory
  7. mkdir -p /run/network
  8. case "$1" in
  9.   start)
  10.         printf "Starting network: "
  11.         /sbin/ifup -a
  12.         /sbin/ifconfig eth0 up
  13.         printf "Starting eth0: "
  14.         start-stop-daemon -b -q -S -x /sbin/udhcpc -- -b -i eth0 -R
  15.         [ $? = 0 ] && echo "OK" || echo "FAIL"
  16.         ;;
  17.   stop)
  18.         printf "Stopping network: "
  19.         /sbin/ifdown -a
  20.         [ $? = 0 ] && echo "OK" || echo "FAIL"
  21.         ;;
  22.   restart|reload)
  23.         "$0" stop
  24.         "$0" start
  25.         ;;
  26.   *)
  27.         echo "Usage: $0 {start|stop|restart}"
  28.         exit 1
  29. esac
  30. exit $?
复制代码
start-stop-daemon

  1. start-stop-daemon -b -q -S -x /sbin/udhcpc -- -b -i eth0 -R
复制代码
start-stop-daemon(start and stop system daemon programs) 是将一个平凡步伐酿成守卫历程。
用法

  1. [root@sg200x]~# start-stop-daemon
  2. BusyBox v1.33.0 (2024-08-20 07:13:20 CST) multi-call binary.
  3. Usage: start-stop-daemon [OPTIONS] [-S|-K] ... [-- ARGS...]
  4. Search for matching processes, and then
  5. -K: stop all matching processes
  6. -S: start a process unless a matching process is found
  7. Process matching:
  8.         -u USERNAME|UID Match only this user's processes
  9.         -n NAME         Match processes with NAME
  10.                         in comm field in /proc/PID/stat
  11.         -x EXECUTABLE   Match processes with this command
  12.                         in /proc/PID/cmdline
  13.         -p FILE         Match a process with PID from FILE
  14.         All specified conditions must match
  15. -S only:
  16.         -x EXECUTABLE   Program to run
  17.         -a NAME         Zeroth argument
  18.         -b              Background
  19.         -N N            Change nice level
  20.         -c USER[:[GRP]] Change user/group
  21.         -m              Write PID to pidfile specified by -p
  22. -K only:
  23.         -s SIG          Signal to send
  24.         -t              Match only, exit with 0 if found
  25. Other:
  26.         -o              Exit with status 0 if nothing is done
  27.         -v              Verbose
  28.         -q              Quiet
复制代码
start-stop-daemon [options] command
COMMANDS:
-S, --start [–] arguments:查抄特定的历程是否存在,假如存在 start-stop-daemon 退出并返回状态 “1” (假如设置了–oknodo,返回"0"); 假如不存在则由 --exec 大概 --startas 指定的下令,全部跟在 – 反面的参数都将通报给要启动的下令。
-K, --stop:查抄特定的历程是否存在,假如存在start-stop-daemon发送一个由–signal指定的信号并返回"0";假如不存在返回"1",假如–retry被设置,还要查抄是不是历程已经退出。
-p, --pidfile pid-file: 指定 pid 文件,至于 pid 文件的用途就多了,stop,status 都少不了它。
-x, --exec executable (/proc/pid/exe): 真正要实验的历程
-n, --name process-name (/proc/pid/stat):假如没有指定 pid 文件,那么就要通过指定 name 来克制历程了
-u, --user username|uid:指定脚本用哪个用户或用户组实验,init脚本是必须使用root权限来实验的,但是它fork出来的子历程我们一样平常会选择一个权限较低的用户。
OPTIONS:
-g, --group group|gid
-s, --signal signal (default TERM)
-R, --retry timeout|schedule: 等候 timeout 的时间,查抄历程是否克制,假如没有发送 KILL 信号;schedule 可以设置为:signal/time/KILL/time,retry后假如还存在历程将返回"2"。
-a, --startas pathname
If not specified, defaults to the argument given to --exec
-o, --oknodo
-q, --quiet
-c, -chuid username|uid:改变用户运行下令
-r, --chroot root:在某些安全性要求较高的情况下,我们就须要用到chroot将历程工作情况与物理情况完全隔脱离来。
-d, --chdir path:启动历程前切换到目次path
-b, --background:配景运行
-k, --umask mask
-m, --make-pidfile:当下令本身不创建pidfile时,由start-stop-daemon创建;
  1. # 启动程序:
  2. $ start-stop-daemon -S -x 用户命令 -- 用户参数
  3. # 关闭程序:
  4. $ start-stop-daemon -K -x 用户命令
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表