阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

OpenHarmony双网卡配置实例

33次阅读
没有评论

共计 4298 个字符,预计需要花费 11 分钟才能阅读完成。

导读 本文带大家了解如何配置双网卡达成同时访问内、外网的目的,该示例相对简单,重点是需要将默认网关配置为外网网关。
前言

在某些特殊场景下,我们可能有这样的网络配置诉求,即:访问内网同时也需要访问外网。
本文主要针对该场景进行梳理,以双网卡配置为例,指导初学者进行网卡配置,达成同时访问内外网。

双网卡配置硬件前提条件:

硬件方面:板卡有 2 个网卡。
双网卡配置规则:1、连接外网 LAN 口,配置外网网段 IP、子网掩码、路由和网关;2、连接内网 LAN 口,配置内网网段 IP、子网掩码和路由。
说明:特别要注意,一台设备不能同时配置两个网关,且网关只能配置为外网网关,否则设备将无法区分外网出口,导致外网访问失败。
以下示例内外网的 IP 分配如下:(具体以你所在的网络为准)

如下为例:

外网:IP 地址 10.61.*** 掩码:255.0.0.0 路由:10.61.*** 网关:10.61.***
内网:IP 地址 192.168.*** 掩码:255.255.255.0 路由:192.168.0.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:15 errors:0 dropped:0 overruns:0 frame:0
TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1320 TX bytes:1320
eth0 Link encap:Ethernet HWaddr 96:d0:79:2d:9b:88 Driver rk_gmac-dwmac
inet addr:10.61.*** Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::94d0:79ff:fe2d:9b88/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:28813 errors:0 dropped:0 overruns:0 frame:0
TX packets:3445 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2706345 TX bytes:330787
Interrupt:35
eth1 Link encap:Ethernet HWaddr c2:5c:62:d7:1f:20 Driver smsc75xx
inet addr:192.168.*** Bcast:192.168.*** Mask:255.255.255.0
inet6 addr: fe80::c05c:62ff:fed7:1f20/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3282 errors:0 dropped:0 overruns:0 frame:0
TX packets:488 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:280938 TX bytes:55862
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.61.*** 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
10.61.*** * 255.255.252.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1

本例中设置 192 网段为内网,10 网段为外网,经过以上配置后,ping 内网 (例如:192.168.***) 正常,ping 外网 (例如:ping www.baidu.com) 正常。说明同时访问内外网成功

Ping 192.168.*** (192.168.***): 56(84) bytes.
64 bytes from 192.168.***: icmp_seq=1 ttl=0 time=1 ms
64 bytes from 192.168.***: icmp_seq=2 ttl=0 time=2 ms
64 bytes from 192.168.***: icmp_seq=3 ttl=0 time=1 ms
--- 192.168.*** ping statistics ---
3 packets transmitted, 3 received, 0% packet loss
round-trip min/avg/max = 0/0/1 ms
# ping 10.61.***
Ping 10.61.*** (10.61.***): 56(84) bytes.
64 bytes from 10.61.***: icmp_seq=1 ttl=0 time=0 ms
64 bytes from 10.61.***: icmp_seq=2 ttl=0 time=0 ms
64 bytes from 10.61.***: icmp_seq=3 ttl=0 time=1 ms
--- 10.61.*** ping statistics ---
3 packets transmitted, 3 received, 0% packet loss
round-trip min/avg/max = 0/0/0 ms

知识总结:
以上已经成功完成了双网卡配置,下面我们来看下相关配置命令。
1、相关调试命令
1)删除路由

route del -net 10.0.0.0 netmask 255.0.0.0 dev eth1
route del -net 10.61.*** netmask 255.255.252.0 dev eth1

2)删除默认网关

route del default

3)配置 IP

ifconfig eth0 192.168.*** // 给 eth0 网卡配置 IP 地址
ifconfig eth0 192.168.*** netmask 255.255.255.0 // 给 eth0 网卡配置 IP,并加 mask
ifconfig eth0 192.168.*** netmask 255.255.255.0 broadcast 192.168.120.255

4)配置默认网关

route add default gw 10.61.*** dev eth0`

5)配置路由

route add -net 10.61.*** netmask 255.255.252.0 dev eth0`

2、路由及 IP 在板子保存位置:
1)路由保存位置:/proc/net/route

# cat proc/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 01C43D0A 0003 0 0 0 00000000 0 0 0
eth0 0000000A 00000000 0001 0 0 0 000000FF 0 0 0
eth0 00C43D0A 00000000 0001 0 0 0 00FCFFFF 0 0 0
eth1 0000A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0

2)静态 IP 保存位置

# cat data/ethernet/eth0
DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=10.61.***
NETMASK=255.255.255.0
GATEWAY=10.61.***
ROUTE=10.61.***
# cat data/ethernet/eth1
DEVICE=eth1
BOOTPROTO=STATIC
IPADDR=192.168.***23
NETMASK=255.255.255.0
GATEWAY=192.168.***
ROUTE=192.168.***

3)动态 IP(dhcp)保存位置:/data/dhcp/eth0.result

# cat /data/dhcp/eth0.result
IP4 1666952418 10.61.*** 10.61.*** 255.255.252.0 10.61.10.6 10.60.10.6 10.61.*** *

问题总结:
【问题现象】:拔掉网线不自动清除 IP 和路由
【原因分析】:拔掉网线后事件触发 StopDhcpClient,但未同步更新网络信息;
【解决方案】:
在 foundation\communication\netmanager_ext\services\ethernetmanager\src\ethernet_management.cpp 的 StopDhcpClient()函数中增加更新网络链接状态。

index 9b03ad8..0c4f708 100644
#NAME?
#NAME?
@@ -569,6 +569,7 @@ void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfac
{NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
ethDhcpController_->StopDhcpClient(dev, false);
+ devState->RemoteUpdateNetLinkInfo();
devState->SetDhcpReqState(false);
}
@@ -581,4 +582,4 @@ void EthernetManagement::SetDevState(sptr<DevInterfaceState> &devState, const st
devState->SetLowerUp(lowerUp);
}
} // namespace NetManagerStandard
-} // namespace OHOS
\ No newline at end of file
+} // namespace OHOS

通过以上示例指导,相信大家已经掌握了如何配置双网卡达成同时访问内、外网的目的,该示例相对简单,重点是需要将默认网关配置为外网网关。同时,通过该示例,相信大家也熟练掌握了网络配置相关命令。当然,同时访问内网和外网的网络结构,不止示例中一种,大家也可以不断尝试,举一反三。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计4298字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中