En a previous article talk about DietPi a system for our Raspberry Pi that in addition to having a large community has various systems and projects.
Among which you can turn your Raspberry Pi into a multimedia center, a retro game console, a NAS server among other things.
Now this time we will see how to use our Raspberry Pi as a wireless access point.
Table of Contents
Turning Raspberry Pi into a wireless access point
In order to achieve this, It is necessary to have an operating system on our Raspberry, so the official system is perfect for it.
The first thing we must do is update our system, for this in a terminal we are going to type the following commands:
sudo apt-get update
sudo apt-get upgrade
Once this is done, we will restart our device, so that any changes made are saved.
Back in the system, let's proceed to install hostapd, dnsmasq and bridge-utils, These are the two programs that we will use to turn our Raspberry Pi into a wireless access point.
Therefore, to install it, just open a terminal and in it we are going to type the following commands.
sudo apt-get install hostapd
sudo apt-get install dnsmasq
sudo apt-get install bridge-utils
Once the installation of these has been done, we are going to edit the configuration files of the programs so we must stop their services with:
sudo systemctl stop hostapd
sudo systemctl stop dnsmasq
Now we proceed to edit the following file:
sudo nano /etc/dhcpcd.conf
Now that it's in the file, add the following lines at the end:
static ip_address = 192.168.0.10/24
Here in this that we are placing, we are assuming we have local IP addressesIn addition, our network interface has common names and no others.
After that, press Ctrl + O, then Ctrl + X to save the file and exit the editor.
Configure DHCP server (dnsmasq)
We are going to use dnsmasq as our DHCP server. The idea of a DHCP server is to dynamically distribute network configuration parameters, such as IP addresses, to interfaces and services.
Let's rename the default configuration file and write a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.back
sudo nano /etc/dnsmasq.conf
In the new file we are going to write these configuration lines:
The lines we add mean that we are going to provide IP addresses between 192.168.0.11 and 192.168.0.30 for the wlan0 interface. We close and save the file.
Y now we are going to create another new configuration file, we do this by typing the following command:
sudo nano /etc/hostapd/hostapd.conf
Inside it we will place the following:
Where they will only edit the last two lines, because here they will place the name that they will give to their access point as well as their password, without the "".
Once edited we will save and close the changes. And now let's open the following file:
sudo nano /etc/default/hostapd
In this file, look for the line that says # DAEMON_CONF = ”” - remove that # and put the path to our configuration file in the quotes, so it looks like this:
DAEMON_CONF = "/etc/hostapd/hostapd.conf"
Configure traffic forwarding
The idea here is that when they connect, the traffic is forwarded through their Ethernet cable. Therefore, we will have to send WLAN through an Ethernet cable to your modem. This involves editing another configuration file:
sudo nano /etc/sysctl.conf
Now find this line:
# net.ipv4.ip_forward = 1
And remove the "#", save and close to file.
The next step is to add a new iptables rule, then with:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
iptables-restore < /etc/iptables.ipv4.nat
Enable internet connection
Now the Raspberry Pi is acting as an access point that other devices can connect to. However, those devices still cannot use the Pi to access the internet. To make it possible, we need to build a bridge that passes all traffic between the wlan0 and eth0 interfaces.
We are ready to add a new bridge (called br0):
sudo brctl addbr br0
Next, we will connect the eth0 interface to our bridge:
sudo brctl addif br0 eth0
Finally, let's edit the interfaces file and add the following lines to the end of the file:
sudo nano /etc/network/interfaces
iface br0 inet manual
bridge_ports eth0 wlan0
We reboot our Raspberry Pi and that's it.