1.使用到的iptable命令:
清除防火墙规则
iptables -F
iptables -F -t nat
查询防火墙规则
iptables -L
iptables -L -t nat
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o eth5 -j MASQUERADE
2.Ubuntu环境下搭建
首先开启两个网卡(桥接),一个接电脑的有线(eth2),一个接电脑的无线网卡(eth5)。使用eth2作为lan口,eth5作为wan口。
设置eth2 ip 192.168.4.1 路由地址,去除网关。
设置eth5 ip --- 可连接外网ip地址,添加网关如:
route add default gw 192.168.1.1 dev eth5
设置转发命令
#echo 1 >> /proc/sys/net/ipv4/ip_forward
设置iptable命令
iptables -F
iptables -F -t nat
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o eth5 -j MASQUERADE
这样将电脑的有线口接到交换机口,其他机器接到交换机都可以上网。
3.安卓机器
使用4G网卡ppp0作为wan口,有线eth0作为lan口,eth0设置ip为
busybox ifconfig eth0 192.168.4.1 不要设置网关。
4G网关和ip自动设置。
设置转发命令
#echo 1 >> /proc/sys/net/ipv4/ip_forward
设置iptable命令
iptables -F -t nat ;iptables -F; iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
5.连接路由的客户端网络参数获取:
静态IP设置模式
虚拟机设置一个ip如:桥接到有线网卡中,与安卓机器eth0用同一个交换机
ifconfig eth5 192.168.4.102 ;route del default; route add default gw 192.168.4.1 dev eth5
同时虚拟机也需要设置一下DNS,用getprop命令查看安卓系统dns,然后将该DNS设置到虚拟机中(Ubuntu)。
动态IP设置模式
Dhcp服务器
使用busybox本身的dhcp功能,从busybox-1.16.1\examples\udhcp 目录中获取到
udhcpd.conf修改内容:
start 192.168.4.20
end 192.168.4.254
opt dns 210.21.4.130 221.5.88.88
option subnet 255.255.255.0
opt router 192.168.4.1
#opt wins 192.168.10.10
#option dns 129.219.13.81 # appened to above DNS servers for a total of 3
option domain local
option lease 864000 # 10 days of seconds
放入安卓系统中(/etc),执行启动服务器命令:
busybox udhcpd -fS /etc/udhcpd.conf &
对于iptable的详细介绍请点击点击打开链接