2022年2月10日 星期四

設定 Raspberry pi zero Wifi AP

#安裝相關軟體
apt install -y iptables dnsmasq hostapd

export uap0=192.168.2.1/24
export dhcpr=192.168.2.2,192.168.88.50,12h

#設定網路界面
cat >/etc/network/interfaces.d/wlan0 <<EOF
auto wlan0
allow-hotplug wlan0

iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
EOF

cat >/etc/network/interfaces.d/uap0 <<EOF
iface uap0 inet static
  address 192.168.2.1
  netmask 255.255.255.0
  network 192.168.2.0
  broadcast 192.168.2.255
  gateway 192.168.2.1
EOF

#設定 dhcpd uap0 界面
echo >>dhcpcd.conf <<EOF
interface uap0
static ip_address=${uap0}
nohook wpa_supplicant
EOF

#設定 dnsmasq 服務
cat >>/etc/dnsmasq.conf <<EOF
interface=uap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=${dhcpr}
EOF

#設定 hostapd 服務
cat >/etc/hostapd/hostapd.conf <<EOF
interface=uap0
ssid=piap
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF

echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"'  >>/etc/default/hostapd
systemctl unmask hostapd

#產生執行 script
cat >/root/startap.sh <<EOF
#!/bin/bash
# Create the virtual device
/sbin/iw dev wlan0 interface add uap0 type __ap
ifup uap0

# Route all the packets coming on this interface, this will enable #us to access internet through wlan0

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 ! -d 192.168.2.0/24 -j MASQUERADE

systemctl start dnsmasq
systemctl start hostapd
EOF

沒有留言: