Before we get started installing and setting up our packages, we will first run an update on the Raspberry Pi by running the following two commands.
$ sudo apt-get upgrade
$ sudo apt-get update
With that done we can now install our two packages, run the following to commands to install hostapd and dnsmasq.
$ sudo apt-get install hostapd
$ sudo apt-get install dnsmasq
With the packages now installed we will want to ensure that we are actually given a static IP address for our Raspberry Pi, one of the ways of doing this is by making some modifications to the dhcpcd configuration, in here we can specify a specific IP Address we want the Raspberry Pi to sit on as well as IP we want our routers to operate on.
To do this lets begin modifying the dhpcd.conf
file by running the following command on our Raspberry Pi.
$ sudo nano /etc/dhcpcd.conf
Within this file we need to add the following line to the bottom, this will setup our wlan0 interface to the way we want it for our tutorial.
interface wlan0
static ip_address=192.168.2.1/24
static routers=192.168.2.0
Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.
Now we need to restart our dhcpd service so it will load in all our configuration changes, we also need to reload our wlan0 interface to make sure it’s loaded in our interface changes. Run the following command to reload the dhcpd service:
$ sudo service dhcpcd restart
Next, we need to adjust our hostapd configuration, to do this we need to begin editing the config file with the following command:
$ sudo nano /etc/hostapd/hostapd.conf
In this file we need to write out the following lines, these basically set up how we want to interact with the wlan device. The only real lines you should worry about in this file is the ssid= line and the wpa_passphrase= line.
interface=wlan0
driver=nl80211
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
ignore_broadcast_ssid=0
# Use WPA2
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
# This is the name of the network
ssid=Pi3-AP
# The network passphrase
wpa_passphrase=raspberry
Remember to change wpa_passphrase to your own password, make sure you set it to something secure so random people can’t just connect into your Wi-Fi access point.
Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.
With that done we should now have our hostapd configuration, but before it can be used we need to edit two files. These files are what hostapd will read to find our new configuration file.
To begin editing the first of these two files run the following command:
$ sudo nano /etc/default/hostapd
In this file, we need to find the following line and replace it:
Find:
#DAEMON_CONF=""
Replace with:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.
Now we need to edit the second configuration file, this file is located within the init.d folder. We can edit the file with the following command:
$ sudo nano /etc/init.d/hostapd
In this file, we need to find the following line and replace it:
Find:
DAEMON_CONF=
Replace with:
DAEMON_CONF=/etc/hostapd/hostapd.conf
Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.
With hostapd now set up we need to move onto setting up dnsmasq, before we begin editing its configuration we will move the default one to a new location. We can do this with the following command:
$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
Now that the original configuration file is moved out of the way we can begin by creating our own new configuration file. We will create and edit the new file with the following command:
$ sudo nano /etc/dnsmasq.conf
To this file add the following lines, these lines basically tell the dnsmasq service how to handle all the connections coming through.
interface=wlan0 # Use interface wlan0
listen-address=192.168.2.1 # Specify the address to listen on
bind-interfaces # Bind to the interface
server=8.8.8.8 # Use Google DNS or an other one
domain-needed # Don't forward short names
bogus-priv # Drop the non-routed address spaces.
dhcp-range=192.168.2.50,192.168.2.150,12h # IP range and lease time
Set the dnsmasq to start. Edit the file:
$ sudo nano /lib/systemd/system/dnsmasq.service
Add the following line before “ExecStartPre=/sur/sbin/dnsmasq –test”:
ExecStartPre=/bin/sleep 30
Now we can save and quit out of the file by pressing Ctrl +X then pressing Y and then Enter.
Enable automatic startup with the following command:
$ sudo systemctl enable dnsmasq.service
Now you should finally have a fully operational Raspberry Pi wireless access point, you can ensure this is working by using any of your wireless devices and connecting to your new access point using the SSID and WPA Passphrase that was set earlier on in the tutorial.
To ensure everything will run smoothly it’s best to try rebooting now. This will ensure that everything will successfully re-enable when the Raspberry Pi is started back up. Run the following command to reboot the Raspberry Pi:
$ sudo reboot