1、让同一设备内的其它网口上网
下图的功能,其实就是一个4G路由器,可以有线上网,也可以无线上网。
可以通过NAT规则轻松实现。
比如,LTE模块拨号成功,网口名称为 lte0
针对wifi ap :
service isc-dhcp-server stop
sed "/INTERFACES=/c\INTERFACES=wlan0" -i /etc/default/isc-dhcp-server
sed "/interface=/c\interface=wlan0" -i /etc/hostapd/hostapd.conf
hostapd -B /etc/hostapd/hostapd.conf
sleep 1
ifconfig wlan0 10.5.5.1/24
service isc-dhcp-server restart
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o lte0 -j MASQUERADE
iptable执行Masquerade,把源IP更改为宿主机网卡的IP,然后向外网服务器发送请求。
原理大概如下:
$ cat /etc/default/isc-dhcp-server
# Defaults for isc-dhcp-server initscript
# sourced by /etc/init.d/isc-dhcp-server
# installed at /etc/default/isc-dhcp-server by the maintainer scripts
#
# This is a POSIX shell fragment
#
# Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
#DHCPD_CONF=/etc/dhcp/dhcpd.conf
# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
#DHCPD_PID=/var/run/dhcpd.pid
# Additional options to start dhcpd with.
# Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="wlp1s0"
$ cat /etc/hostapd/hostapd.conf
interface=wlP2p1s0
driver=nl80211
ssid=test
channel=6
hw_mode=g
auth_algs=1
wpa=3
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP