1. 安装hotspot(hostapd)
sudo apt-get install bridge-utils hostapd
hostapd实现的是无线共享,但需要注意的是官方提供的程序不兼容RTL8188CUS芯片的无线网卡,不过Edimax团队为我们专门编译了兼容的版本,下面的操作需要替换hostapd为兼容版本。
替换的命令如下:
wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
sudo mv hostapd /usr/sbin/hostapd.edimax
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd
sudo chown root.root /usr/sbin/hostapd
sudo chmod 755 /usr/sbin/hostapd
建立AP有两种方案,一种是桥接方式设置热点,有点类似交换机工作原理,将网线口的信号转换为无线,另外一种则是路由方式实现无线上网,和大部分路由器工作方式一样。
注:由于我的树莓派网口是直接连接笔记本网口,树莓派通过我的有线网卡上网,我直接ssh控制树莓派,在使用桥接方式后,我发现putty连接不上树莓派,远程桌面也一样,检查树莓派ip是对的就是连接不上
由于项目要求,故我使用第二种方式:路由的方式设置WiFi热点
2. 路由的方式设置WiFi热点
2.1安装udhcpd
sudo apt-get install udhcpd
udhcpd主要为连接到WiFi的设备自动分配IP地址的
2.2配置udhcpd,编辑/etc/udhcpd.conf,主要内容如下
# Sample udhcpd configuration file (/etc/udhcpd.conf)
# The start and end of the IP lease block
start 192.168.0.20 #default: 192.168.0.20
end 192.168.0.254 #default: 192.168.0.254
# The interface that udhcpd will use
interface wlan0 #default: eth0
remaining yes #default: yes
opt dns 114.114.114.114 180.76.76.76
option subnet 255.255.255.0
opt router 192.168.0.1
opt wins 192.168.0.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
配置完成之后树莓派AP模式的IP地址为192.168.0.1
3.使能DHCP
配置 /etc/default/udhcpd
sudo nano /etc/default/udhcpd
使用#符号注释DHCPD_ENABLED=”no”
DHCPD_ENABLED="no"
改为
#DHCPD_ENABLED="no"
4.把wlan0配置为静态IP地址
通过下面的命令给无线网卡设置一个IP地址
sudo ifconfig wlan0 192.168.0.1
当然为了下次启动仍然有效,我们需要配置/etc/network/interfaces文件:
sudo nano /etc/network/interfaces
注释原来一些设置,改为:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0 #有些教程把他也注释掉了,经测试注释后其他设备连接ap后是无法获取到ip的!!
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
5.配置AP参数
5.1 创建配置文件
创建文件 /etc/hostapd/hostapd.conf,配置树莓派AP热点名称和热点密码。
interface=wlan0
driver=rtl871xdrv
ssid=xs
hw_mode=g
channel=10
macaddr_acl=0
auth_algs=3
ignore_broadcast_ssid=0
wpa=1
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
注:网上很多教程写的是wpa_pairwise=TKIP,经测试,也会出现无线网卡无法设置静态IP问题,我改为CCMP就可以,不知是不是我的问题
配置说明:
interface: 那个接口作为接入点
driver: 使用无线驱动,树莓派是rtl871xdrv,注意是187L(小写的L)
ssid: 共享网络的SSID
hwmode: 指定802.11协议,包括 a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g
channel: 指定无线信道
macaddracl: 指定MAC地址过滤规则,0表示除非在禁止列表否则允许,1表示除非在允许列表否则禁止,2表示使用外部RADIUS服务器;
acceptmacfile:指定允许MAC列表文件所在;
denymacfile:指定禁止MAC列表文件所在;
authalgs: 指定采用哪种认证算法 1表示开放系统认证(Open System Authentication, OSA),2表示共享密钥认证(Shared Key Authentication, SKA,3则两种认证方式都支持
ignorebroadcastssid: 是否禁止广播SSID
wpa=2: 使用WPA2加密方式,WEP非常容易被破解不推介使用
wpapassphrase:WPA/WPA2加密需要指定密钥,这个选项就是配置WPA/WPA2的密钥。注意wpapassphrase要求8~63个字符。另外还可以通过配置wpapsk来制定密钥,不过要设置一个256位的16进制密钥
wpakeymgmt: 设置客户端密钥算法验证方式。认证方式为WPA-PSK 加密方式为CCMP
wpapairwise/rsnpairwise: 如果启用了WPA,需要指定wpapairwise;如果启用了WPA2,需要指定rsnpairwise,或者采用wpa_pairwise的设定
5.2 指定配置文件位置
修改配置文件/etc/default/hostapd
sudo nano /etc/default/hostapd
改
#DAEMON_CONF=""
为
DAEMON_CONF="/etc/hostapd/hostapd.conf"
这时,执行
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
你就可以搜到你创建的ap热点啦,各个设备连接这个热点就可以组成一个小型局域网了,不过这是这个局域网是不能访问外网的,还要设置ip转发才可以
6配置NAT(IP转发)
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
nano /etc/sysctl.conf
修改参数为
net.ipv4.ip_forward=1
在以太网端与无线网端建立转发
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
查看iptables是否已经成功设置
sudo iptables -t nat -S
sudo iptables -S
成功的话会看到上面设置的内容。
确保开机自动生效 保存iptables规则
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
sudo nano /etc/network/interfaces
在底部新增一行
up iptables-restore < /etc/iptables.ipv4.nat
保存即可
这时执行
iptables-restore < /etc/iptables.ipv4.nat
你会发现连接直接建立的ap的设备已经能访问外网了
7 启动相应软件,并加入启动项
sudo service hostapd start
sudo service udhcpd start
sudo update-rc.d hostapd enable
sudo update-rc.d udhcpd enable
这时重启树莓派就行了,不过我遇到一个问题,就是重启后虽然能连上刚建立的ap,但是访问不了外网,执行
sudo iptables -t nat -S
sudo iptables -S
发现原来建立的ip转发规则没了,证明,之前在/etc/network/interfaces添加的
up iptables-restore < /etc/iptables.ipv4.nat
没有执行!!!!up意思是在接口启用时执行后面的代码,查了网上资料也没有找到其不能运行的具体原因,看来只能自己另想办法调用。Linux下有自启动配置文件,树莓派也不例外,所以只需要将
iptables-restore < /etc/iptables.ipv4.nat
这个命令放入自启动配置文件就行
sudo nano /etc/rc.local
在exit 0的上一行
加入
iptables-restore < /etc/iptables.ipv4.nat
保存即可。重启树莓派后完美解决问题