树莓派做无线路由器ap

发布时间:2015-12-21 13:40:04 阅读:1400次

转:http://www.embbnux.com/2015/02/08/setup_raspberry_to_wifi_access_point_with_rtl8188/

http://shumeipai.nxez.com/2014/04/01/raspberry-pi-wireless-router.html

寒假放假回家,忘记带无线路由器了,家里只有一根网线没有wifi,幸亏带了树莓派还有一个usb无线网卡,于是考虑用树莓派来做无线热点,利用树莓派来共享无线网络。

一 需要的材料

一根已经能够上网的网线,接入树莓派的网口,保证树莓派能够上网

一个usb无线网卡,我的型号的RTL8188CUS

树莓派版本我是B,应该其他版本都是一样的

二 配置无线网卡驱动

把usb网卡接到树莓派上,ssh登陆到树莓派,或者利用显示器直接打开树莓派的终端

1
2
3
4
5
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

测试:iwconfig #如果有看到wlan0就表示网卡成功驱动

三 配置无线热点

apt-get install isc-dhcp-server
apt-get install hostapd

配置dhcp,编辑配置文件cat /etc/dhcp/dhcpd.conf

subnet 10.77.50.0 netmask 255.255.255.0 {
        range 10.77.50.2 10.77.50.200;
        option subnet-mask 255.255.255.0;
        option broadcast-address 10.77.50.255;
        option routers 10.77.50.1;
}

编辑/etc/network/interfaces

root@raspberrypi:/etc/network# cat interfaces
auto lo
iface lo inet loopback
iface eth0 inet static
        address 192.168.0.112
        netmask 255.255.255.0
        gateway 192.168.0.1
allow-hotplug wlan0
iface wlan0 inet static
        address 10.77.50.1
        netmask 255.255.255.0

这样即使重启也会自动配置静态ip

配置hostapd,由于我用的rtl8818cu并不被官方安装的hostapd支持,所以需要额外安装新的hostapd:

1
2
3
4
5
sudo apt-get remove hostapd
git clone git@github.com:jenssegers/RTL8188-hostapd.git
cd hostapd
make
make install

修改hostpad配置/etc/hostapd/hostapd.conf

root@raspberrypi:/etc/hostapd# cat /etc/hostapd/hostapd.conf
# Basic configuration
interface=wlan0
ssid=MyRaspberryPi
channel=1
#bridge=br0
# WPA and WPA2 configuration
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=1234567890
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

编辑/etc/default/hostapd,添加下面的话:

1
DAEMON_CONF="/etc/hostapd/hostapd.conf"

配置NAT:

1
sudo sh -c"echo 1 > /proc/sys/net/ipv4/ip_forward"

编辑/etc/sysctl.conf,取消注释,保证重启自动配置

1
net.ipv4.ip_forward=1

启用NAT:

1
2
3
4
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
sudo sh -c"iptables-save > /etc/iptables.ipv4.nat"

编辑/etc/network/interfaces,在最后加入下面的话:

1
up iptables-restore </etc/iptables.ipv4.nat

四 启用无线热点服务

1
2
3
4
sudo service hostapd start
sudo service isc-dhcp-server restart
sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable

没什么错误的话,这时候用笔记本或者手机就可以搜索到了刚刚新建的wifi热点了.

笔记本连MyRaspberryPi

cmd查看ip,如果ip不对,那么

C:\Documents and Settings\Administrator>ipconfig /renew

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:树莓派做无线路由器ap 出自老鄢博客 | 欢迎分享