BusyBox 搭建 mini rootfs 完全指南 / 第 6 章:网络工具
第 6 章:网络工具
6.1 网络工具概览
BusyBox 提供了丰富的网络工具,覆盖网络配置、诊断、传输等场景。
6.1.1 工具列表
| Applet | 用途 | 类别 |
|---|---|---|
ifconfig | 网络接口配置 | 配置 |
route | 路由表管理 | 配置 |
ip | 高级网络配置(iproute2 兼容) | 配置 |
udhcpc | DHCP 客户端 | 配置 |
ntpd | NTP 时间同步 | 服务 |
wget | HTTP/HTTPS 文件下载 | 传输 |
ping | ICMP 连通性测试 | 诊断 |
nslookup | DNS 查询 | 诊断 |
netstat | 网络状态查看 | 诊断 |
nc (ncat) | TCP/UDP 连接工具 | 诊断 |
telnet | Telnet 客户端 | 诊断 |
tftp | TFTP 文件传输 | 传输 |
arp | ARP 表管理 | 配置 |
brctl | 网桥管理 | 配置 |
iptables | 防火墙规则 | 安全 |
6.2 ifconfig — 网络接口配置
6.2.1 基本用法
# 查看所有接口
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:11:22:33:44:55
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1234 errors:0 dropped:0 overruns:0 frame:0
TX packets:567 errors:0 dropped:0 overruns:0 carrier:0
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
# 查看特定接口
$ ifconfig eth0
# 查看所有接口(包括未激活的)
$ ifconfig -a
6.2.2 配置接口
# 设置 IP 地址
$ ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
# 设置 IP 和广播地址
$ ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 up
# 启用接口
$ ifconfig eth0 up
# 禁用接口
$ ifconfig eth0 down
# 设置 MTU
$ ifconfig eth0 mtu 9000
# 设置 MAC 地址
$ ifconfig eth0 hw ether 00:11:22:33:44:55
# 添加 IPv6 地址(部分 BusyBox 版本支持)
$ ifconfig eth0 add 2001:db8::1/64
6.2.3 调试模式
# 启用混杂模式(抓包用)
$ ifconfig eth0 promisc
# 禁用混杂模式
$ ifconfig eth0 -promisc
# 查看接口统计
$ ifconfig eth0 | grep -E "RX|TX"
RX packets:1234 errors:0 dropped:0 overruns:0 frame:0
TX packets:567 errors:0 dropped:0 overruns:0 carrier:0
6.2.4 虚拟接口
# 创建 VLAN 接口
$ vconfig add eth0 100
$ ifconfig eth0.100 192.168.100.1 netmask 255.255.255.0 up
# 创建子接口
$ ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0 up
$ ifconfig eth0:1 192.168.1.102 netmask 255.255.255.0 up
6.3 route — 路由管理
6.3.1 查看路由表
# 查看路由表
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
# 解析主机名
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
6.3.2 添加/删除路由
# 添加默认网关
$ route add default gw 192.168.1.1
# 添加网络路由
$ route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1
# 添加主机路由
$ route add -host 10.0.0.1 gw 192.168.1.1
# 删除路由
$ route del default gw 192.168.1.1
$ route del -net 10.0.0.0 netmask 255.0.0.0
# 添加到特定接口的路由
$ route add -net 172.16.0.0 netmask 255.255.0.0 dev eth1
6.3.3 完整网络配置脚本
#!/bin/sh
# /etc/init.d/network - 网络初始化脚本
case "$1" in
start)
echo "Starting network..."
# 启用回环
ifconfig lo 127.0.0.1 up
# 配置 eth0
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
# 设置默认网关
route add default gw 192.168.1.1
# 配置 DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
echo "Network started."
;;
stop)
echo "Stopping network..."
route del default
ifconfig eth0 down
ifconfig lo down
;;
restart)
$0 stop
sleep 1
$0 start
;;
status)
echo "=== Network Status ==="
ifconfig eth0 | grep "inet addr"
route -n | head -5
;;
esac
6.4 udhcpc — DHCP 客户端
6.4.1 基本用法
# 启动 DHCP(前台运行)
$ udhcpc -i eth0
# 后台运行
$ udhcpc -i eth0 -b
# 指定主机名
$ udhcpc -i eth0 -b -h mydevice
# 指定客户 ID
$ udhcpc -i eth0 -b -c "my_client_id"
# 释放租约
$ udhcpc -i eth0 -n
# 续约
$ udhcpc -i eth0 -n -q
6.4.2 DHCP 租约脚本
udhcpc 需要一个脚本来处理获取到的网络配置。默认脚本路径 /usr/share/udhcpc/default.script:
#!/bin/sh
# /usr/share/udhcpc/default.script - udhcpc 配置脚本
# 配置文件路径
RESOLV_CONF="/etc/resolv.conf"
case "$1" in
deconfig)
echo "udhcpc: deconfiguring interface $interface"
ifconfig $interface 0.0.0.0
;;
renew|bound)
echo "udhcpc: configuring interface $interface"
# 设置 IP 地址
ifconfig $interface $ip netmask ${subnet:-255.255.255.0}
# 设置默认网关
if [ -n "$router" ]; then
# 删除旧网关
route del default 2>/dev/null
# 添加新网关
for r in $router; do
route add default gw $r
done
fi
# 配置 DNS
if [ -n "$dns" ]; then
echo -n > $RESOLV_CONF
[ -n "$domain" ] && echo "search $domain" >> $RESOLV_CONF
for d in $dns; do
echo "nameserver $d" >> $RESOLV_CONF
done
fi
# 配置主机名
if [ -n "$hostname" ]; then
hostname $hostname
fi
echo "udhcpc: IP=$ip, Gateway=$router, DNS=$dns"
;;
leasefail)
echo "udhcpc: DHCP lease failed"
;;
nak)
echo "udhcpc: DHCP NAK received"
;;
*)
echo "udhcpc: unknown action '$1'"
exit 1
;;
esac
exit 0
6.4.3 安装 DHCP 脚本
# 创建脚本目录
$ mkdir -p /usr/share/udhcpc
# 复制脚本
$ cp default.script /usr/share/udhcpc/
$ chmod 755 /usr/share/udhcpc/default.script
# BusyBox 默认脚本路径(编译时配置)
# Settings → Networking Utilities → udhcpc default script path
6.4.4 DHCP 高级配置
# 静态租约文件
# /etc/udhcpd.conf(服务器端)
start 192.168.1.100
end 192.168.1.200
interface eth0
pidfile /var/run/udhcpd.pid
lease_file /var/lib/misc/udhcpd.leases
# 客户端高级选项
# 指定请求的 IP
$ udhcpc -i eth0 -r 192.168.1.100
# 指定 vendor class
$ udhcpc -i eth0 -V "MyDevice"
# 指定超时时间
$ udhcpc -i eth0 -T 5 -A 10
# 获取后退出
$ udhcpc -i eth0 -n -q
6.5 ntpd — 时间同步
6.5.1 基本用法
# 前台运行 NTP
$ ntpd -n -p pool.ntp.org
# 后台运行
$ ntpd -p pool.ntp.org
# 指定多个 NTP 服务器
$ ntpd -p "0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org"
# 只查询不同步(dry run)
$ ntpd -n -p pool.ntp.org -q
# 立即同步一次
$ ntpd -n -p pool.ntp.org -q -N
6.5.2 启动脚本集成
# 在 /etc/init.d/rcS 中添加
# 时间同步
if [ -x /sbin/ntpd ]; then
echo "Starting NTP client..."
ntpd -p "0.pool.ntp.org 1.pool.ntp.org" -N &
fi
# 或使用一次性同步
ntpd -p pool.ntp.org -q -N
6.5.3 手动时间设置
# 使用 date 命令设置时间
$ date -s "2024-01-01 12:00:00"
# 查看当前时间
$ date
Mon Jan 1 12:00:00 CST 2024
# 保存时间到硬件时钟
$ hwclock -w
# 从硬件时钟读取
$ hwclock -r
6.6 wget — 文件下载
6.6.1 基本用法
# 下载文件
$ wget http://example.com/file.txt
Connecting to example.com (93.184.216.34:80)
saving to 'file.txt'
file.txt 100% |*****************************| 1256 0:00:00 ETA
# 指定输出文件名
$ wget -O /tmp/output.txt http://example.com/file.txt
# 后台下载
$ wget -b http://example.com/largefile.iso
# 继续中断的下载
$ wget -c http://example.com/largefile.iso
# 限制下载速度(KB/s)
$ wget --limit-rate=100k http://example.com/file.txt
6.6.2 高级选项
# 设置超时
$ wget -T 30 http://example.com/file.txt
# 设置重试次数
$ wget -t 3 http://example.com/file.txt
# 静默模式
$ wget -q http://example.com/file.txt
# 使用 HTTP 头
$ wget --header="Authorization: Bearer token" http://api.example.com/data
# 下载并显示到 stdout
$ wget -O - http://example.com/file.txt | grep "pattern"
# 使用代理
$ wget -e "http_proxy=http://proxy:8080" http://example.com/file.txt
6.6.3 HTTPS 注意事项
# BusyBox wget 对 HTTPS 的支持取决于编译选项
# 需要启用 TLS/SSL 支持
# 检查是否支持 HTTPS
$ wget https://example.com 2>&1 | head -5
# 如果不支持会报错:
# wget: TLS not compiled in
# 使用 --no-check-certificate 忽略证书验证(不推荐)
$ wget --no-check-certificate https://self-signed.example.com/file
6.7 ping — 连通性测试
6.7.1 基本用法
# ping IP 地址
$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=115 time=10.1 ms
64 bytes from 8.8.8.8: seq=1 ttl=115 time=10.3 ms
# ping 域名
$ ping -c 3 google.com
PING google.com (142.250.80.46): 56 data bytes
64 bytes from 142.250.80.46: seq=0 ttl=115 time=5.2 ms
# 限制 ping 次数
$ ping -c 5 8.8.8.8
# 限制超时(秒)
$ ping -W 2 8.8.8.8
# 指定包大小
$ ping -s 1024 8.8.8.8
# 洪泛 ping(需要 root)
$ ping -f 8.8.8.8
# 指定源接口
$ ping -I eth0 8.8.8.8
6.7.2 网络诊断脚本
#!/bin/sh
# network_diag.sh - 网络诊断脚本
echo "=== Network Diagnostics ==="
echo ""
# 1. 接口状态
echo "--- Interface Status ---"
ifconfig eth0 | grep -E "Link|inet"
echo ""
# 2. 路由表
echo "--- Routing Table ---"
route -n
echo ""
# 3. DNS 测试
echo "--- DNS Test ---"
nslookup google.com 2>&1 | head -5
echo ""
# 4. Ping 测试
echo "--- Ping Test ---"
for host in 192.168.1.1 8.8.8.8 google.com; do
if ping -c 1 -W 2 $host >/dev/null 2>&1; then
echo " $host: OK"
else
echo " $host: FAILED"
fi
done
echo ""
# 5. 端口测试
echo "--- Port Test ---"
for port in 80 443 22; do
if nc -z -w2 google.com $port 2>/dev/null; then
echo " google.com:$port: OPEN"
else
echo " google.com:$port: CLOSED"
fi
done
6.8 nslookup — DNS 查询
# 查询 A 记录
$ nslookup google.com
Server: 8.8.8.8
Address 1: 8.8.8.8
Name: google.com
Address 1: 142.250.80.46
# 指定 DNS 服务器
$ nslookup google.com 1.1.1.1
# 查询 MX 记录
$ nslookup -type=MX google.com
# 查询 TXT 记录
$ nslookup -type=TXT google.com
6.9 netstat — 网络状态
# 查看所有监听端口
$ netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 123/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 456/httpd
# 查看所有连接
$ netstat -tnp
# 查看 UDP
$ netstat -ulnp
# 查看路由表
$ netstat -rn
# 查看接口统计
$ netstat -i
6.10 nc (ncat) — 网络连接工具
6.10.1 基本用法
# 测试 TCP 连接
$ nc -zv google.com 80
google.com (142.250.80.46:80) open
# 简单 TCP 服务器
$ nc -l -p 8080
# 简单 TCP 客户端
$ nc 192.168.1.100 8080
# 端口扫描
$ nc -zv 192.168.1.100 20-100
# 文件传输(服务器端)
$ nc -l -p 9999 > received_file
# 文件传输(客户端)
$ nc 192.168.1.100 9999 < file_to_send
6.10.2 HTTP 测试
# 发送 HTTP 请求
$ echo -e "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n" | nc example.com 80
HTTP/1.0 200 OK
Content-Type: text/html
...
6.11 网络配置文件
6.11.1 完整网络配置
# /etc/network/interfaces(自定义格式)
# 静态 IP 配置
IP=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS=8.8.8.8
# /etc/hostname
miniroot
# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search localdomain
# /etc/hosts
127.0.0.1 localhost
192.168.1.100 miniroot
6.11.2 Wi-Fi 配置(wpa_supplicant)
# 安装 wpa_supplicant(需要编译 BusyBox 时启用)
# /etc/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="MyWiFi"
psk="password123"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
}
# 启动 Wi-Fi
$ wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
$ udhcpc -i wlan0
6.12 防火墙(iptables)
# BusyBox iptables 是简化版,功能有限
# 查看规则
$ iptables -L
# 允许已建立的连接
$ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许 SSH
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许 HTTP
$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 拒绝其他入站
$ iptables -A INPUT -j DROP
# 删除规则
$ iptables -D INPUT 3
# 清空规则
$ iptables -F
# 保存规则
$ iptables-save > /etc/iptables.rules
# 恢复规则
$ iptables-restore < /etc/iptables.rules
6.13 网络调试技巧
# 1. 检查物理连接
$ mii-tool eth0
eth0: negotiated 100baseTx-FD, link ok
# 2. 查看 ARP 表
$ arp -a
? (192.168.1.1) at 00:11:22:33:44:55 [ether] on eth0
# 3. 跟踪路由
$ traceroute 8.8.8.8
# 4. 查看网络统计
$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets
eth0: 1234567 1234 0 0 0 0 0 0 567890 567
# 5. 抓包(需要 tcpdump 或 BusyBox 的 tcpdump)
$ tcpdump -i eth0 -n port 80
6.14 本章小结
| 工具 | 用途 | 常用选项 |
|---|---|---|
| ifconfig | 接口配置 | -a, up/down |
| route | 路由管理 | -n, add/del |
| udhcpc | DHCP 客户端 | -i, -b, -q |
| ntpd | 时间同步 | -p, -q |
| wget | 文件下载 | -O, -c, -b |
| ping | 连通性测试 | -c, -W |
| nslookup | DNS 查询 | -type= |
| netstat | 网络状态 | -tlnp, -rn |
扩展阅读
- BusyBox Networking Utilities
- Linux Network Administration Guide
- DHCP Protocol RFC 2131
- NTP Protocol RFC 5905
上一章: 第 5 章 — Init 系统详解
下一章: 第 7 章 — ash Shell