温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Raspberry pi做成无线热点

发布时间:2020-07-30 22:07:28 来源:网络 阅读:1616 作者:fanglinxun 栏目:开发技术

Raspberry pi作为卡片式微型电脑,本身没有自带显示器、鼠标、键盘等外设,如果想对Raspberry PI进行设定需要连接显示器,或者通过路由器ssh登陆才可以。心想Raspberry pi是可以运行Linux系统的卡片型电脑,为何不尝试把Raspberry Pi做成一个无线AP,想登陆Raspbery pi的时候就比较方便了。后续也可以加上USB存储外设之类的做个多媒体网关等等。


主要的实现思路是wlan0设定成固定IP,eth0动态获取IP。Raspberry Pi运行hostapd和udhcpd分别作为无线AP热点和DHCP服务器给终端分配IP地址。

目前我手上的wifi网卡是Realtek的8818芯片。如果直接按照网上的教程安装hostapd的话会出现错误。经过一番查找,Realtek公司专门有针对8818芯片的hostapd版本,网上牛人已经把相关代码放到github上,我们直接安装就可以了。

为了能正常为8818芯片的wifi网卡安装上hostapd,首先要卸载掉原先的hostapd

sudo apt-get autoremove hostapd

然后安装8818芯片版本的hostapd

wget https://github.com/jenssegers/RTL8188-hostapd/archive/v2.0.tar.gztar -zxvf v2.0.tar.gz

然后编译安装

cd RTL8188-hostapd-2.0/hostapd sudo make

sudo make install


$ sudo service hostapd restart

[ ok ] Stopping advanced IEEE 802.11 management: hostapd.

[ ok ] Starting advanced IEEE 802.11 management: hostapd.

hostapd安装完毕。

接下来安装udhcpd

sudo apt-get install udhcpd

安装完成后配置/etc/udhcpd.conf


start 192.168.20.20 # This is the range of IPs that the hostspot will give to client devices.
end 192.168.20.200
interface wlan0 # The device uDHCP listens on.
remaining yes
opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use.
opt subnet 255.255.255.0
opt router 192.168.20.1 # The Pi's IP address on wlan0 which we will set up shortly.
opt lease 864000 # 10 day DHCP lease time in seconds

接下来编辑/etc/default/udhcpd并且将下面这行注释掉,以使DHCP Server正常工作:

#DHCPD_ENABLED="no"

为了下次启动仍然有效,我们需要配置/etc/network/interfaces文件:

sudo nano /etc/network/interfaces

注释掉所有的关于无线网卡的部分,最后应该变成下面所示:

#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

注意保留allow-hotplug wlan0,英文原文是不保留的,但是我操作下来发现如果不保留这段的话,无线网卡有时无法正常配置IP,最后无线网卡IP的配置信息如下:

allow-hotplug wlan0
iface wlan0 inet static
  address 192.168.20.1
  netmask 255.255.255.0

编辑hostapd配置

sudo nano /etc/hostapd/hostapd.conf


启动IP转向功能以便于开通NAT

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

net.ipv4.ip_forward=1

配置iptables防火墙

我们可以做个脚本名为nat.sh,以便启动的时候自动运行nat相关配置

#!/bin/sh
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 chmod +x nat.sh

编辑/etc/rc.local文件,使之开机时运行nat相关iptabls配置和启动hostapd

sudo nano /etc/rc.local

在exit 0上面加上以下两行后保存退出

sudo service hostapd start

sh /home/pi/nat.sh


再让udhcpd也启动时一起运行。

sudo update-rc.d udhcpd enable

所有的步骤结束后重启Raspberry pi.

大功告成!


PS:这里碰到了几个小问题。

  1. 不知是问么原因,原本打算用sudo update-rc.d hostapd enable使hostapd开机启动的。但是会报错无法开机启动,后来决定在/etc/rc.local上加上sudo service hostapd start的方式解决。

  2. 关于iptables,原本打算用sudo sh -c "iptables-save > /etc/network/iptables",然后在/etc/network/interface上加上 up iptables-restore < /etc/network/iptables方式使nat相关配置生效的。但是不知什么原因重启Raspberry pi后一直不生效。也只能通过万能的rc.local上加上sh /home/pi/nat.sh的方式解决。

后期打算加上自动加载USB存储设备,通过媒体共享的方式给我的IPAD,iphone等共享文件,播放视频等等。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI