原理:Pi使用有线连入网络,然后接USB无线网卡作为热点,提供Wifi接入。
1、USB无线网卡驱动
如果接上USB无线网卡,使用ifconfig命令,能直接看到wlan0
2、修改wlan0为静态IP
相当于设置路由器lan口IP,即我们访问路由器通常使用的:192.168.1.1
sudo vim /etc/network/interfaces
把原来关于wlan0的注释掉:(可能跟这个不一样,跟wlan0有关的注释掉即可)
#auto wlan0
#iface wlan0 inet dhcp
#wpa-ssid "360WiFi-li"
#wpa-psk "xiaolizi"
添加下面的:
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
gateway 192.168.0.1
完成之后需要重启
3、安装hostapd
官方的hostapd不支持8188CUS,后面需要重新卸载安装新的
笔者测试貌似这里必须先装旧的,然后后面卸了装新的,否则也不能用
sudo apt-get install hostapd
1)编辑hostapd 默认配置文件:
sudo vim /etc/default/hostapd
找到#DAEMON_CONF= “”,修改为:
DAEMON_CONF=”/etc/hostapd/hostapd.conf”
2)然后编辑:sudo vim /etc/hostapd/hostapd.conf
增加以下代码:
# Basic configuration
interface=wlan0
ssid=raspberrywifi
channel=1
#bridge=br0
# WPA and WPA2 configuration
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
# Hardware configuration
driver=rtl871xdrv
ieee80211n=1
hw_mode=g
device_name=RTL8192CU
manufacturer=Realtek
修改wifi名和密码
ssid=raspberrywifi
wpa_passphrase=12345678
3)保存退出,然后重启服务:
sudo service hostapd restart
或者执行以下命令生效
sudo hostapd -dd /etc/hostapd/hostapd.conf
4)如果你使用的网卡提示一下信息
Configuration file: /etc/hostapd/hostapd.conf
nl80211: ‘nl80211’ generic netlink not found
Failed to initialize driver ‘nl80211’
rmdir[ctrl_interface]: No such file or directory
那么,还是要使用第三方的hostapd。
4、安装新的hostapd
1)删除原来的hostapd(笔者测试,不卸载貌似也行)
sudo apt-get autoremove hostapd
2)下载第三方驱动并安装
wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz
tar -zxvf v1.1.tar.gz
3)编译:
cd RTL8188-hostapd-1.1/hostapd
sudo make
sudo make install
4)然后再重启服务,应该提示成功:
$ sudo service hostapd restart
[ ok ] Stopping advanced IEEE 802.11 management: hostapd.
[ ok ] Starting advanced IEEE 802.11 management: hostapd.
5)将hostapd加入开机自启动
sudo service hostapd start
sudo update-rc.d hostapd enable
笔者这里提示的还是失败,但是重启后网络确实建立成功,用手机可以搜到这个网络
5、安装DHCP服务
以上步骤建立起了wifi热点,但是无法自动获取ip,需要以下步骤
sudo apt-get install udhcpd
1)编辑配置文件:
sudo vim /etc/udhcpd.conf //修改以下信息,start和end是重点,注意跟第一步的静态ip在一个网段
start 192.168.0.20
end 192.168.0.200
interface wlan0
2)接下来编辑/etc/default/udhcpd并且将下面这行注释掉,以使DHCP Server正常工作:
#DHCPD_ENABLED="no"
3)启动dhcp服务器
sudo service udhcpd start
sudo update-rc.d udhcpd enable
经过此步手机已经可以接入wifi网络,并且自动获取ip
6、配置路由转发
1)开起内核转发:
sudo vim /etc/sysctl.conf
找到下面两行:
#Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1
把net.ipv4.ip_forward 前面的#去掉,保存退出。
然后:sudo sysctl -p
2)1)设置路由映射规则
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state –state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
到这里路由的NAT功能已经被启用,我们将刚才配置的iptables保存下来以便于下次使用:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
编辑
/etc/network/interfaces
然后在最后加上下面这行以使每次启动都自动加载iptables配置:
up iptables-restore < /etc/iptables.ipv4.nat
7、其它问题
最近经常发现无线网卡配置的DHCP不能发挥作用,经过排查发现给无线网卡指定的静态IP失败了,也就是说无线网卡没有IP导致DHCP无法工作,将/etc/default/ifplugd的内容修改配置如下:
INTERFACES=”eth0”
HOTPLUG_INTERFACES=”eth0”
ARGS=”-q -f -u0 -d10 -w -I”
SUSPEND_ACTION=”stop”
部署ATC
首先,必须安装Python2.7以上版本,然后安装pip:
sudo apt-get install python-pip python-dev build-essential)
sudo pip install --upgrade pip
然后,使用pip直接安装好ATC所有组件:
pip install atc_thrift atcd django-atc-api django-atc-demo-ui django-atc-profile-storage
这一步最好分开执行安装
接下来部署Django的web工程,提供手机访问并用来配置和切换网络用的。
1.使用django-admin生成一个新的django工程:
django-admin startproject atcui
cd atcui
2.修改atcui/settings.py,加入ATC相关的内容:
INSTALLED_APPS = (
…
# Django ATC API
‘rest_framework’,
‘atc_api’,
# Django ATC Demo UI
‘bootstrap_themes’,
‘django_static_jquery’,
‘atc_demo_ui’,
# Django ATC Profile Storage
‘atc_profile_storage’,
)
3.修改atcui/urls.py,urlpatterns 中加入atc的url页面:
…
…
from django.views.generic.base import RedirectView
urlpatterns = patterns(”,
…
# Django ATC API
url(r’^api/v1/’, include(‘atc_api.urls’)),
# Django ATC Demo UI
url(r’^atc_demo_ui/’, include(‘atc_demo_ui.urls’)),
# Django ATC profile storage
url(r’^api/v1/profiles/’, include(‘atc_profile_storage.urls’)),
url(r’^$’, RedirectView.as_view(url=’/atc_demo_ui/’, permanent=False)),
)
4.更新一下数据库:
python manage.py migrate
前面设置WiFI热点时,你还记得设置的无线网卡的名字吗?嗯,就是wlan0,这个很重要。接下来要启动网络控制的核心组件atcd,需要通过参数指定提供Wifi热点的内网用的网卡名字,外网访问的网卡名默认是eth0(如果不是也需要通过–atcd-wan指定)
sudo atcd --atcd-lan wlan0
然后,启动Django的工程:
sudo python manage.py runserver 0.0.0.0:8000
用手机连接上去试试吧。看看虚拟机的IP,比如我设置的是192.168.6.1,手机浏览器里访问:http://192.168.6.1:8000