树莓派配置AP模式
所需硬件:树莓派、无线网卡
1.查看无线网卡是否被识别
pi@raspberrypi ~ $ sudo lsusb Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter pi@raspberrypi ~ $
WLAN Adapter代表的就是无线网卡被系统识别,芯片是RTL8188CUS
2.无线网卡配置静态IP地址
sudo vim /etc/network/interfaces #配置静态地址
pi@raspberrypi ~ $ sudo vim /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.8.1
netmask 255.255.255.0
保存后重启wlan0网卡
sudo ifdown wlan0 sudo ifup wlan0
3.安装DHCP服务软件
sudo apt-get install isc-dhcp-server
配置DHCP服务
sudo vim /etc/dhcp/dhcpd.conf #编辑配置文件 #option domain-name "example.org"; #注释 #option domain-name-servers ns1.example.org, ns2.example.org; #注释 authoritative; #去除注释 #在配置文件底部添加发下配置 subnet 192.168.8.0 netmask 255.255.255.0 { range 192.168.8.100 192.168.8.200; option broadcast-address 192.168.8.255; option routers 192.168.8.1; default-lease-time 600; max-lease-time 7200; option domain-name "local"; option domain-name-servers 8.8.8.8, 8.8.4.4; }
sudo vim /etc/default/isc-dhcp-server #编辑配置文件 INTERFACES="wlan0"
4.安装AP软件与配置
sudo apt-get install hostapd 配置Access Point sudo vim /etc/hostapd/hostapd.conf interface=wlan0 driver=rtl871xdrv#这个是树莓派无线网卡驱动,不要改 ssid=pi#无线信号名字,随意设 hw_mode=g channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=raspberry#密码,随意设 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
设置读取加载配置文件
sudo vim /etc/default/hostapd #找到#DAEMON_CONF=""修改成 DAEMON_CONF="/etc/hostapd/hostapd.conf"
默认安装的版本比较新,该驱动需要用V0.8的版本,否则运行hostapd会报错
wget http://www.adafruit.com/downloads/adafruit_hostapd.zip unzip adafruit_hostapd.zip sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG sudo mv hostapd /usr/sbin sudo chmod 755 /usr/sbin/hostapd
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
5.启动dhcp、hostapd服务
sudo service isc-dhcp-server start #启用DHPCServere服务 sudo service hostapd start #启用AP服务
设置开启自启动
sudo update-rc.d hostapd enable sudo update-rc.d isc-dhcp-server enable
6.开启转发
sudo vim /etc/sysctl.conf #开机启动设置ip转发 #底部加上一行 net.ipv4.ip_forward=1 或者直接: sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
7.添加防火墙规则
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
8.保存规则并开机自动生效
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" sudo vim /etc/network/interfaces #开机生效 up iptables-restore < /etc/iptables.ipv4.nat #底部增加一行
---------------------------------------------------------------------------------------------------
后记:如果有两个无线网卡,又想设置两个AP,那么可以修改/etc/network/interfaces和/etc/dhcp/dhcpd.conf以及添加一个/etc/hostapd/hostapd2.conf
如下:
pi@raspberrypi ~ $ sudo vim /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.8.1
netmask 255.255.255.0
#继续添加
auto wlan1
iface wlan0 inet static
address 192.168.9.1
netmask 255.255.255.0
network 192.168.9.0
broadcast 192.168.9.255
接着,修改/etc/default/isc-dhcp-server文件,添加wlan1
sudo vim /etc/default/isc-dhcp-server #编辑配置文件 INTERFACES="wlan0"
接着,修改/etc/dhcp/dhcpd.conf
sudo vim /etc/dhcp/dhcpd.conf #编辑配置文件 #option domain-name "example.org"; #注释 #option domain-name-servers ns1.example.org, ns2.example.org; #注释 authoritative; #去除注释 #在配置文件底部添加发下配置 subnet 192.168.8.0 netmask 255.255.255.0 { range 192.168.8.100 192.168.8.200; option broadcast-address 192.168.8.255; option routers 192.168.8.1; default-lease-time 600; max-lease-time 7200; option domain-name "local"; option domain-name-servers 8.8.8.8, 8.8.4.4; }
#继续添加
subnet 192.168.9.0 netmask 255.255.255.0 { range 192.168.9.100 192.168.9.200; option broadcast-address 192.168.9.255; option routers 192.168.9.1; default-lease-time 600; max-lease-time 7200; option domain-name "local"; option domain-name-servers 8.8.8.8, 8.8.4.4; }
最后,添加一个/etc/hostapd/hostapd2.conf,内容类似hostapd.conf,但是channel要不一样,否则出现干扰
interface=wlan1 driver=rtl871xdrv#这个是树莓派无线网卡驱动,不要改 ssid=pi2#无线信号名字,随意设 hw_mode=g channel=2 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=raspberry#密码,随意设 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
如此,启动
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf /etc/hostapd/hostapd2.conf
即可启动两个AP,如果加上-B,可运行于后台。
------------------------------------------------------------------------------------------
此外,如果想配置成wlan0 ap模式,wlan1 连接路由器模式,可以修改/etc/network/interfaces文件,添加wlan1的设置即可。
auto wlan1 allow-hotplug wlan1 iface wlan1 inet dhcp wpa-ssid "SDN2.4" wpa-psk "404404404"
重启wlan1(sudo ifdown wlan1 sudo ifup wlan1)即可。