由于我的是树莓派3, 自带wifi网卡,然后又加了一个usb网卡
现在分配的是, 如果检测到usb无线网卡插入,那么就使用usb无线网卡发射热点
如果eth0或者wlan0任何一个有网的情况下, 做路由转发,使热点能连上网
安装依赖
sudo apt install hostapd dnsmasq
配置hostapd
#/etc/hostapd/hostapd.conf# Basic configuration interface=wlan1ssid=TestWifiAPchannel=1#bridge=br0# WPA and WPA2 configuratiomacaddr_acl=0auth_algs=1ignore_broadcast_ssid=0wpa=3wpa_passphrase=12345678wpa_key_mgmt=WPA-PSKwpa_pairwise=TKIPrsn_pairwise=CCMP# Hardware configurationdriver=nl80211#driver=rtl871xdrv#ieee80211n=1hw_mode=g#device_name=RTL8192CU#manufacturer=Realtek
配置dnsmasq
# /etc/dnsmasq.conf#interface=wlan1bind-interfacesserver=114.114.114.114server=8.8.8.8domain-neededbogus-privdhcp-range=10.10.10.10,10.10.10.100,12h
配置interfaces
# /etc/network/interfacesauto loiface lo inet loopbackauto wlan0allow-hotplug wlan0iface wlan0 inet manualwpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
如果网卡分配有问题,请配置70-persistent-net.rules文件, mac地址记得改掉
# /etc/udev/rules.d/70-persistent-net.rulesSUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="eth*", NAME="eth0"SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="wlan*", NAME="wlan0"SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:00:00:00:00:00", KERNEL=="wlan*", NAME="wlan1"
发射热点脚本/usr/local/bin/wifi-ap
#!/bin/bash#===============================================================================## FILE: wifi-ap# # USAGE: ./wifi-ap # # DESCRIPTION: # # OPTIONS: ---# REQUIREMENTS: ---# BUGS: ---# NOTES: ---# AUTHOR: Yehun (), yehunhk@163.com# ORGANIZATION: Yehun# CREATED: 03/04/2018 01:53:05 AM# REVISION: ---#===============================================================================ETH0="eth0"WLAN0="wlan0"WLAN1="wlan1"if ifconfig | grep $WLAN1 > /dev/null ;then ifconfig $WLAN1 down ifconfig $WLAN1 10.10.10.1 netmask 255.255.255.0 up #iwconfig $WLAN1 power off hostapd -B /etc/hostapd/hostapd.conf #service hostapd start if ifconfig | grep $WLAN0 > /dev/null ;then if [ "down" == `cat /sys/class/net/$WLAN0/operstate` ]; then wpa_supplicant -B -i $WLAN0 -c /etc/wpa_supplicant/wpa_supplicant.conf fi if [ "up" == `cat /sys/class/net/$WLAN0/operstate` ]; then echo "start setting wlan0 iptables" sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward" iptables -t nat -APOSTROUTING -o $WLAN0 -j MASQUERADE iptables -A FORWARD -i $WLAN0 -o $WLAN1 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i $WLAN1 -o $WLAN0 -j ACCEPT elif [ "up" == `cat /sys/class/net/$ETH0/operstate` ]; then echo "start setting eth0 iptables" sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward" iptables -t nat -APOSTROUTING -o $ETH0 -j MASQUERADE iptables -A FORWARD -i $ETH0 -o $WLAN1 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i $WLAN1 -o $ETH0 -j ACCEPT fi fifi
把wifi-ap加入rc.local开机启动
#!/bin/sh -e## rc.local## This script is executed at the end of each multiuser runlevel.# Make sure that the script will "exit 0" on success or any other# value on error.## In order to enable or disable this script just change the execution# bits.## By default this script does nothing.# Print the IP address_IP=$(hostname -I) || trueif [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP"fi/usr/local/bin/wifi-apexit 0