473 字
2 分钟
开源鸿蒙系统配置网络
NOTE

为哪吒 D1 开发板 构建适配的开源鸿蒙系统 后,默认情况下系统不带 dhcp 服务,连接网线后无法自动配置 IP 地址,需要手动进行配置

查看网口信息#

输入 ifconfig -a 命令查看网口信息。

ifconfig -a

输出示例:

eth0      Link encap:Ethernet  HWaddr 56:22:6c:6a:59:9b  Driver sunxi-gmac
          BROADCAST MULTICAST  MTU:1500  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 TX bytes:0 
          Interrupt:62 

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  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 TX bytes:0 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet6 addr: ::1/128 Scope: Host
          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 TX bytes:0

从返回的信息可以看到默认有 eth0 有线网口,下面对其进行配置。

配置有线网络#

配置 IP 地址#

使用以下命令配置 IP 地址,注意将 x.x.x.x 替换为实际的 IP 地址:

ifconfig eth0 x.x.x.x netmask 255.255.255.0 up

配置默认网关#

使用以下命令配置默认网关,注意将 x.x.x.x 替换为实际的网关地址:

route add default gw x.x.x.x

配置 DNS 服务器#

编辑 /etc/resolv.conf 文件,添加 DNS 服务器地址:

echo "nameserver 223.5.5.5" > /etc/resolv.conf

开放 ping 权限#

WARNING

默认情况下,系统未开放 ping 权限,直接使用 ping 命令会提示 Permission denied 错误。

直接使用 ping 会出现错误:

# ping www.baidu.com
ping: socket SOCK_DGRAM 1: Permission denied
Kernel bug workaround (as root):
echo 0 9999999 > /proc/sys/net/ipv4/ping_group_range

报错提示可用以下命令开放 ping 权限:

echo "0 9999999" > /proc/sys/net/ipv4/ping_group_range

测试网络连接#

使用 ping 命令测试网络连接:

ping www.baidu.com

出现类似输出即表示网络配置成功:

# ping www.baidu.com
Ping www.baidu.com (183.2.172.17): 56(84) bytes.
64 bytes from 183.2.172.17: icmp_seq=1 ttl=0 time=27 ms
64 bytes from 183.2.172.17: icmp_seq=2 ttl=0 time=27 ms
64 bytes from 183.2.172.17: icmp_seq=3 ttl=0 time=26 ms

--- 183.2.172.17 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss
round-trip min/avg/max = 0/0/26 ms

参考#

  1. 【OH 干货】给 OpenHarmony 开发板配置网络
开源鸿蒙系统配置网络
https://blog.wely.fun/posts/开源鸿蒙系统配置网络/
作者
Wely
发布于
2025-09-30
许可协议
CC BY-NC-SA 4.0