In order to work as an access point, the Raspberry Pi will need to have access point software installed, along with DHCP server software to provide connecting devices with a network address. Ensure that your Raspberry Pi is using an up-to-date version of Raspbian (dated 2017 or later).
Install the required software (dnsmasq and hostapd) with this command:
sudo apt updatesudo apt upgradesudo apt install dnsmasq hostapd
To configure the static IP address, open the dhcpcd configuration file with the following command:
sudo nano /etc/dhcpcd.conf
Go to the end of the file and edit it so that it looks like the following:
interface wlan0static ip_address=192.168.4.1/24nohook wpa_supplicant
Now restart the dhcpcd
daemon:
sudo systemctl restart dhcpcd
The DHCP service is provided by dnsmasq. By default, the configuration file contains a lot of information that is not needed, and it is easier to start from scratch. Rename this configuration file, and edit a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
Type or copy the following information into the dnsmasq configuration file and save it:
interface=wlan0 # Use the require wireless interface - usually wlan0dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
Reload dnsmasq
to use the updated configuration:
sudo systemctl reload dnsmasq
sudo nano /etc/hostapd/hostapd.conf
add the following text into the file
interface=wlan0driver=nl80211ssid=pytobot:00:00:00hw_mode=gchannel=7wmm_enabled=0macaddr_acl=0auth_algs=1ignore_broadcast_ssid=0wpa=2wpa_passphrase=PASSWORDwpa_key_mgmt=WPA-PSKwpa_pairwise=TKIPrsn_pairwise=CCMP
We now need to tell the system where to find this configuration file.
sudo nano /etc/default/hostapd
Find the line with #DAEMON_CONF, and replace it with this:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Now enable and start hostapd
:
sudo systemctl unmask hostapdsudo systemctl enable hostapdsudo systemctl start hostapd
Edit /etc/sysctl.conf
sudo nano /etc/sysctl.conf
and uncomment this line:
net.ipv4.ip_forward=1
Add a masquerade for outbound traffic on eth0:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Save the iptables rule.
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Edit /etc/rc.local and add this just above "exit 0" to install these rules on boot.
sudo nano /etc/rc.local
Add the following line aboce Exit 0
iptables-restore < /etc/iptables.ipv4.nat
Using a wireless device, search for networks. The network SSID you specified in the hostapd configuration should now be present, and it should be accessible with the specified password.
If SSH is enabled on the Raspberry Pi access point, it should be possible to connect to it from another Linux box (or a system with SSH connectivity present) as follows, assuming the pi
account is present:
ssh pi@192.168.4.1
By this point, the Raspberry Pi is acting as an access point, and other devices can associate with it. Associated devices can access the Raspberry Pi access point via its IP address for operations such as rsync
, scp
, or ssh
.
The following script updates the SSID name to the last 8 digits of the mac addres. The PASSWD is updated to a random 8 digit number.
acc_point_update.sh#!/bin/bash​SSID=pytobot:$(ifconfig eth0 | grep -Eo ..\(\:..\){5} | tail -c 9)echo $SSID​PASSWD=$((10000000 + RANDOM % 9999999))echo $PASSWD​sudo sed -i "s/SSIDNAME/$SSID/g" /etc/hostapd/hostapd.confsudo sed -i "s/PASSWORD/$PASSWD/g" /etc/hostapd/hostapd.conf​sudo systemctl unmask hostapdsudo systemctl enable hostapdsudo systemctl start hostapd
TODO: At this moment the script will search for SSIDNAME and PASSWD in the config file. The first time on startup the SSIDNAME and PASSWD will be changed to the MACADDR and random PASSWD, But after that its not possible to change it anymore. That is not a problem as long the SD card stays in the same controller with the same MACADDR.
Local hostspot- Raspberry Pi[SOURCE]