需求情况
开发板A有两个网口eth0 eth1,分别属于不同网段eth0在10.130.x.x ,而eth1在192.168.1.x
开发板A eth0在192.168.1.x 那么板B eth0与 板A eth1 相连可以ping通, 那么板B如何通过板A连到10.130.x.x网段
如下图:
使用开源工具iptables即可实现
1,下载编译iptables
下载不再细说,网上搜索iptables即可
./configure --host=arm-linux --prefix=iptables-1.6.1iptables/install --enable-static --disable-shared --with-ksource=xxxx
make
make install
拷贝iptables执行文件到开发板
2,内核配置
打开如下选项
Networking support
Networking options --->
[*] IP: advanced router
[*] Network packet filtering framework (Netfilter) --->
Core Netfilter Configuration --->
<*> Netfilter connection tracking support
<*> NetBIOS name service protocol support
-*- Netfilter Xtables support (required for ip_tables)
IP: Netfilter Configuration --->
<*> IPv4 connection tracking support (required for NAT)
<*> IP tables support (required for filtering/masq/NAT)
<*> IPv4 NAT
<*> MASQUERADE target support
<*> NETMAP target support
<*> REDIRECT target support
3.开发板A上执行
ifconfig eth0 10.130.30.38
ifconfig eth1 192.168.1.22
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 10.130.30.38
echo 1 > /proc/sys/net/ipv4/ip_forward
开发板B执行
ifconfig eth0 192.168.1.23
route add default gw 192.168.1.22
开发板B即可ping通10.130.x.x网段
此外,如果板A接4g模块上网,板B也想通过板A访问外网。可使用类似命令,指定动态地址。eth1为4g网卡接口名
iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 -o eth1 -j MASQUERADE