马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
前面已经先容过 sophpi 的启动过程和 eth0 静态 IP 所在设置。不外静态 IP 在使用的时间比力不通用,本文先容 eth0 主动使能并设置 dhcp 功能。
udhcpc
udhcpc 是 BusyBox 工具会集的一个组件,用于从 DHCP 服务器获取网络设置信息,如 IP 所在、子网掩码、默认网关和 DNS 服务器等。它是一个轻量级的工具,非常得当资源受限的嵌入式体系。
手工设置网络接口
在使用 udhcpc 之前,须要确保网络接口已经启用并处于活动状态,我们要设置的网络接口是 eth0。
- [root@sg200x]~# ifconfig eth0 up
复制代码 这将启动 udhcpc 客户端,实验在名为 eth0 的网络接口上获取 IP 所在。
- [root@sg200x]~# udhcpc -h
- udhcpc: option requires an argument: h
- BusyBox v1.33.0 (2024-08-20 07:13:20 CST) multi-call binary.
- Usage: udhcpc [-fbqRB] [-a[MSEC]] [-t N] [-T SEC] [-A SEC/-n]
- [-i IFACE] [-s PROG] [-p PIDFILE]
- [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...
- -i IFACE Interface to use (default eth0)
- -s PROG Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
- -p FILE Create pidfile
- -B Request broadcast replies
- -t N Send up to N discover packets (default 3)
- -T SEC Pause between packets (default 3)
- -A SEC Wait if lease is not obtained (default 20)
- -b Background if lease is not obtained
- -n Exit if lease is not obtained
- -q Exit after obtaining lease
- -R Release IP on exit
- -f Run in foreground
- -S Log to syslog too
- -a[MSEC] Validate offered address with ARP ping
- -r IP Request this IP address
- -o Don't request any options (unless -O is given)
- -O OPT Request option OPT from server (cumulative)
- -x OPT:VAL Include option OPT in sent packets (cumulative)
- Examples of string, numeric, and hex byte opts:
- -x hostname:bbox - option 12
- -x lease:3600 - option 51 (lease time)
- -x 0x3d:0100BEEFC0FFEE - option 61 (client id)
- -x 14:'"dumpfile"' - option 14 (shell-quoted)
- -F NAME Ask server to update DNS mapping for NAME
- -V VENDOR Vendor identifier (default 'udhcp VERSION')
- -C Don't send MAC as client identifier
- Signals:
- USR1 Renew lease
- USR2 Release lease
复制代码 在 udhcpc 常用下令可选参数如下:
- -i:指定接口名
- -s:指定指定一个脚本文件,该文件将在收到 DHCP 下发的参数时被实验
- -d:答应 udhcpc 运行时打印出调试信息
udhcpc还支持很多高级选项,比方:
- -t:指定实验获取IP所在的次数。
- -T:指定每次实验之间的延长时间(秒)。
比方:
- [root@sg200x]~# udhcpc -i eth0 -t 5 -T 2 -s /path/to/custom/script
复制代码 使用 udhcpc 获取 IP 所在
接下来,我们使用udhcpc来获取IP所在和其他网络设置信息。
- [root@sg200x]~# udhcpc -i eth0
- udhcpc (v1.30.1) started
- udhcpc: started, v1.33.0
- udhcpc: sending discover
- udhcpc: sending select for 192.168.188.167
- udhcpc: lease of 192.168.188.167 obtained, lease time 864000
- deleting routers
- adding dns 192.168.188.1
复制代码 udhcpc 将主动从 DHCP 服务器获取设置信息。再次使用 ifconfig 检察网络信息:
- [root@sg200x]~# ifconfig
- eth0 Link encap:Ethernet HWaddr 72:94:A5:19:79:23
- inet addr:192.168.188.167 Bcast:192.168.188.255 Mask:255.255.255.0
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- RX packets:202 errors:0 dropped:130 overruns:0 frame:0
- TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:51828 (50.6 KiB) TX bytes:684 (684.0 B)
- Interrupt:21
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- UP LOOPBACK RUNNING MTU:65536 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
复制代码 已经乐成获取到 IP所在,而且已经添加了默认网关和 DNS 服务器。
- [root@sg200x]~# vi /etc/resolv.conf
- search fritz.box # eth0
- nameserver 192.168.188.1 # eth0
复制代码 并可以正常使用 ping 测试:
- [root@sg200x]~# ping www.baidu.com
- PING www.baidu.com (180.101.50.242): 56 data bytes
- 64 bytes from 180.101.50.242: seq=0 ttl=50 time=13.834 ms
- 64 bytes from 180.101.50.242: seq=1 ttl=50 time=12.120 ms
- 64 bytes from 180.101.50.242: seq=2 ttl=50 time=12.001 ms
- 64 bytes from 180.101.50.242: seq=3 ttl=50 time=11.658 ms
- ^C
- --- www.baidu.com ping statistics ---
- 4 packets transmitted, 4 packets received, 0% packet loss
- round-trip min/avg/max = 11.658/12.403/13.834 ms
复制代码 主动启动方式一
前文 从零开始学习 sg200x 多核开辟之 eth0 主动使能并设置静态IP 已经先容了怎样在开机时启动 eth0,可以修改 /etc/network/interfaces 文件,添加如下内容:
- [root@sg200x]~# vi /etc/network/interfaces
- # interface file auto-generated by buildroot
- auto lo
- iface lo inet loopback
- auto eth0
- iface eth0 inet dhcp
复制代码
- auto eth0: 表现网卡随机启动;
- iface eth0 inet dhcp: 表现网卡 ip 所在主动获取
重启后可以发现 eth0 已经获取到了 IP 所在:
- [root@sg200x]~# ifconfig
- eth0 Link encap:Ethernet HWaddr 16:4B:9E:5B:2D:6A
- inet addr:192.168.188.168 Bcast:192.168.188.255 Mask:255.255.255.0
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- RX packets:136 errors:0 dropped:74 overruns:0 frame:0
- TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:46826 (45.7 KiB) TX bytes:684 (684.0 B)
- Interrupt:21
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- UP LOOPBACK RUNNING MTU:65536 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
复制代码 通过 /etc/init.d/ 下的 S40network 脚本步伐主动启动的。
主动启动方式二
方式一是通过修改 /etc/network/interfaces 文件,也可以修改 /etc/init.d/S40network 文件完成启动。
添加如下内容:
- [root@sg200x]~# vi /etc/init.d/S40network
- #!/bin/sh
- #
- # Start the network....
- #
- # Debian ifupdown needs the /run/network lock directory
- mkdir -p /run/network
- case "$1" in
- start)
- printf "Starting network: "
- /sbin/ifup -a
- /sbin/ifconfig eth0 up
- printf "Starting eth0: "
- start-stop-daemon -b -q -S -x /sbin/udhcpc -- -b -i eth0 -R
- [ $? = 0 ] && echo "OK" || echo "FAIL"
- ;;
- stop)
- printf "Stopping network: "
- /sbin/ifdown -a
- [ $? = 0 ] && echo "OK" || echo "FAIL"
- ;;
- restart|reload)
- "$0" stop
- "$0" start
- ;;
- *)
- echo "Usage: $0 {start|stop|restart}"
- exit 1
- esac
- exit $?
复制代码 start-stop-daemon
- start-stop-daemon -b -q -S -x /sbin/udhcpc -- -b -i eth0 -R
复制代码 start-stop-daemon(start and stop system daemon programs) 是将一个平凡步伐酿成守卫历程。
用法
- [root@sg200x]~# start-stop-daemon
- BusyBox v1.33.0 (2024-08-20 07:13:20 CST) multi-call binary.
- Usage: start-stop-daemon [OPTIONS] [-S|-K] ... [-- ARGS...]
- Search for matching processes, and then
- -K: stop all matching processes
- -S: start a process unless a matching process is found
- Process matching:
- -u USERNAME|UID Match only this user's processes
- -n NAME Match processes with NAME
- in comm field in /proc/PID/stat
- -x EXECUTABLE Match processes with this command
- in /proc/PID/cmdline
- -p FILE Match a process with PID from FILE
- All specified conditions must match
- -S only:
- -x EXECUTABLE Program to run
- -a NAME Zeroth argument
- -b Background
- -N N Change nice level
- -c USER[:[GRP]] Change user/group
- -m Write PID to pidfile specified by -p
- -K only:
- -s SIG Signal to send
- -t Match only, exit with 0 if found
- Other:
- -o Exit with status 0 if nothing is done
- -v Verbose
- -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创建;
- # 启动程序:
- $ start-stop-daemon -S -x 用户命令 -- 用户参数
- # 关闭程序:
- $ start-stop-daemon -K -x 用户命令
复制代码 |