使用iptables使多网口开发板可级联上网

需求情况

开发板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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值