iptables在维基百科上的解释是:ptables是一个配置Linux内核防火墙的命令行工具,是netfilter项目的一部分,可以检测、修改、转发、重定向和丢弃IPv4数据包。ip6tables用于ipv6。我这里介绍的是在linux平台实现网络数据的转发和网络数据共享的功能。

    整体结构和测试环境如下图所示。

iptables转发端口 ip6tables 端口转发_iptables转发端口

电脑/终端:
    win7 系统,一个网卡eth0,通过有线连接到linux 终端设备的eth0网口
Linux 终端设备:
    linux系统,三个网口:eth0有线连接网口,IP:192.168.20.168;eht1为3G/4G拨号上网网口;wlan0 WIFI热点使用网口。
移动设备:
    手机,平板,手提电脑等,通过WIFI连接到linux终端设备的wlan0网口
    所有的设备经过iptable处理过后,都可以共享linux终端设备的3g/4g网络,实现数据共享上网的功能。


(一)有线数据共享

    电脑通过有线连接到linux终端的eth0网口,实现共享linux终端4G数据。

(1)电脑端

    设置电脑自动获取IP。

(2)linux终端设备

    (a)启动eht1网口

      启动linux设备的3G/4G模块,使设备能够正常的拨号上网,并且域名等参数设置正常,可以解析域名地址。

    (b)启动eth0 动态分配IP功能:

      该功能是为有线连接的设备自动分配IP使用

#创建udhcpd.leases 文件
    touch /var/lib/misc/udhcpd.leases
    #启动eth0自动分配IP功能
    udhcpd -fS /usr/share/wifi/dhcp_eth0.conf &

    dhcp_eth0.conf 配置文件内容为:

start 192.168.20.2                                                                                             
end 192.168.20.25
interface eth0
opt dns 202.96.128.86
opt dns 120.80.88.88
opt dns 223.5.5.5
opt dns 8.8.8.8
opt subnet 255.255.255.0
opt router 192.168.20.168
opt lease 864000

    注意,这里需要设置网关, opt router 192.168.20.168。这里设置的是电脑通过有线连接的linux终端后的网关,这个网关也就是所连接linux设备eth0网口的IP 192.168.20.168。如果这里网关设置不正确,电脑端的数据将发送不出去。

    (c)启动网络转发功能

#使能转发功能
    echo '1' >> /proc/sys/net/ipv4/ip_forward
    #将有线连接192.168.20.2/25 之间的数据转发到eth1网卡,通过3G/4G 收发数据 
    iptables -t nat -A POSTROUTING -s 192.168.20.2/25  -o eth1 -j MASQUERADE

(二)无线数据共享

    移动设备端的手机平板等设备,通过WIFI连接到linux 终端设备,实现共享3G/4G网络上网。

     (a)linux设备的WIFI网卡设置:

#启动网卡
    ifconfig wlan0 up
    #设置IP
    ifconfig wlan0 192.168.0.1
    #wifi配置
    hostapd /usr/share/wifi/rtl_hostapd_2G.conf -B

    rtl_hostapd_2G.conf 的内容为 :

##### hostapd configuration file ##############################################

interface=wlan0
ctrl_interface=/var/run/hostapd
ssid=LiCaibiao_Debug
channel=6
wpa=2
wpa_passphrase=87654321
bridge=br0

##### Wi-Fi Protected Setup (WPS) #############################################

eap_server=1

# WPS state
# 0 = WPS disabled (default)
# 1 = WPS enabled, not configured
# 2 = WPS enabled, configured
wps_state=2

uuid=12345678-9abc-def0-1234-56789abcdef0

# Device Name
# User-friendly description of device; up to 32 octets encoded in UTF-8
device_name=RTL8192CU

# Manufacturer
# The manufacturer of the device (up to 64 ASCII characters)
manufacturer=Realtek

# Model Name
# Model of the device (up to 32 ASCII characters)
model_name=RTW_SOFTAP

# Model Number
# Additional device description (up to 32 ASCII characters)
model_number=WLAN_CU

# Serial Number
# Serial number of the device (up to 32 characters)
serial_number=12345

# Primary Device Type
# Used format: <categ>-<OUI>-<subcateg>
# categ = Category as an integer value
# OUI = OUI and type octet as a 4-octet hex-encoded value; 0050F204 for
#       default WPS OUI
# subcateg = OUI-specific Sub Category as an integer value
# Examples:
#   1-0050F204-1 (Computer / PC)
#   1-0050F204-2 (Computer / Server)
#   5-0050F204-1 (Storage / NAS)
#   6-0050F204-1 (Network Infrastructure / AP)
device_type=6-0050F204-1

# OS Version
# 4-octet operating system version number (hex string)
os_version=01020300

# Config Methods
# List of the supported configuration methods
config_methods=label display push_button keypad


##### default configuration #######################################

driver=rtl871xdrv
beacon_int=100
hw_mode=g
ieee80211n=1
wme_enabled=1
ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+]
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
max_num_sta=8
wpa_group_rekey=86400

(b)启动wlan0网口的自动分配IP功能

    udhcpd -fS /usr/share/wifi/dhcp.conf &

start 192.168.0.20                                                                                                             
end 192.168.0.254
interface wlan0
opt dns 202.96.128.86
opt dns 120.80.88.88
opt dns 223.5.5.5
opt dns 8.8.8.8
opt subnet 255.255.255.0
opt router 192.168.0.1
opt lease 864000

    同样这里也需要注意网关的设置,opt router 192.168.0.1 要与ifconfig wlan0 192.168.0.1 的地址对应。 

    在linux设备中可以启动多个udhcpd 对不同网口的设备进行IP的自动分配,只是㤇设置不同的配置文件就可以了。比如上面启动了的两个网口:

udhcpd -fS /usr/share/wifi/dhcp.conf &

udhcpd -fS /usr/share/wifi/dhcp_eth0.conf &

(c)设置wlan0网口的数据转发:

    将wlan0网口192.168.0.20~254之间的IP数据通过eth1网口也就是3G/4G网卡转发出去。 

iptables -t nat -A POSTROUTING -s 192.168.0.20/254  -o eth1 -j MASQUERADE

    到这里所有的网络共享和数据转发都已实现。在我设备上测试可行,记录在这里以备将来查询。有需要的可根据自己实际需求更改设置。 这里设置的linuxWIFI功能,实际也就相当于一个路由器的功能。

----------------------------------------------------------------2022.08.28----------------------------------------------------------------

 新的文章内容和附件工程文件