脱机运行云课堂:利用树莓派架设局域网

为了在脱机环境下,可以利用树莓派架设一个云课堂,需要开启树莓派的无线热点,并且自动为访问设备分配IP

安装hostapd

sudo apt-get install hostapd
sudo systemctl stop hostapd
sudo nano /etc/hostapd/hostapd.conf
首先需要安装hostapd,并停止服务以调整设置:

ssid:wifi名
hw+mode:a:802.11a(5G),b:802.11b(2.4G),g:802.11g(2.4G),为保证兼容性,一般设置为2.4G
channel:信道编号
wpa_passphrase:wifi密码

接下来是启用新配置
sudo nano /etc/default/hostapd
将 DAEMON_CONF修改为/etc/hostapd/hostapd.conf

最后启动hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd
这个时候,就已经看得到wifi信号了

设置wlan的静态ip

树莓派作为网关,需要分配一个静态地址

首先更改dhcpch配置
sudo nano /etc/dhcpcd.conf
在末尾修改为静态的ip
interface wlan0
static ip_address=192.168.100.1/24
nohook wpa_supplicant

然后重启 dhcpcd 服务
sudo systemctl restart dhcpcd

安装dnsmasq服务

dnsmasq提供的dhcp服务可以为客户端分配IP

首先安装dnsmasq
sudo apt-get install dnsmasq
sudo systemctl stop dnsmasq

然后修改配置文件
sudo nano /etc/dnsmasq.conf

在最后加上两行:
interface=wlan0
dhcp-range=192.168.100.10,192.168.100.200,255.255.255.0,24h
这样,当客户端连接wifi时,就可以将192.168.100.10到192.168.100.200之间的ip分配给客户端了。

可以看见,给这部设备分配的ip是192.168.100.35

这个时候启动express,便可以使用局域网中的设备打开树莓派上运行的网站了