参考自://www.greatytc.com/p/1fca72a710d5,https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md
设置默认网关route add default gw 192.168.1.1
此WiFi热点可以转发来自有线的网络,让树莓派变成路由器方便调试
(2018新版raspbian测试失败,请注意)
一、安装dnsmasq hostapd
sudo apt-get install dnsmasq hostapd
二、接口配置
sudo nano /etc/dhcpcd.conf
在下面加入,按Ctrl+X 输入y 回车退出:
interface wlan0
static ip_address=192.168.0.1/24
三、 配置HOSTAPD
sudo nano /etc/hostapd/hostapd.conf
输入内容如下,可以修改ssid和wpa_passphrase为你自己设置的WiFi热点名字和密码,按Ctrl+X 输入y 回车退出:
interface=wlan0
driver=nl80211
ssid=RaspberryPi
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
接着设置开机自启动
sudo nano /etc/default/hostapd
修改文件中这一行,并取消注释,按Ctrl+X 输入y 回车退出:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
四、配置DNSMASQ
sudo mv /etc/dnsmasq.conf /etc/bak_dnsmasq.conf
sudo nano /etc/dnsmasq.conf
输入内容如下,按Ctrl+X 输入y 回车退出:
interface=wlan0
dhcp-range=192.168.0.2,192.168.0.20,255.255.255.0,24h
五、设置IPV4转发
sudo nano /etc/sysctl.conf
找到# Uncomment the next line to enable packet forwarding for IPv4这一行,把下一行取消注释,按Ctrl+X 输入y 回车退出:
# Uncomment the next line toenable packet forwarding for IPv4
net.ipv4.ip_forward=1
执行以下指令使其生效:
sudo sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward"
继续配置NAT转发,逐条执行以下命令:
sudo iptables -t nat -APOSTROUTING -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"
创建一个新文件:
sudo nano /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat
写入如下命令,按Ctrl+X 输入y 回车退出:
iptables-restore </etc/iptables.ipv4.nat
最后,重启树莓派,完成后即可搜到此前设置的WiFi热点:
sudo reboot