If you ever happened to be in a place such as a hotel or a workplace where they only provided an Ethernet connection and you needed Wifi to get your device(s) connected, then taking a wifi router along might not be the best solution. Instead, you could take with you the popular Raspberry Pi computer and get yourself WiFi access in no time. But before getting that to work, you need to do some initial setup which we will mention here today.
What exactly happens, when Raspberry Pi is configured as a WiFi Hotspot?
It will allow you to connect to the internet over a WiFI using the Raspberry Pi as the bridge to the internet. Basically, it accepts the incoming connection using the WiFi which is built on Raspberry Pi (in case of the Pi 3 or USB WiFi dongle for the rest) and passes the requests to the Ethernet port. By this, you’ll be able to use this connection with any devices that you have with the information of whatever security has set up on it.
Things needed to get started:
- Raspberry Pi model 3 or earlier version with a WiFi dongle.
- Micro SD card (8GB or larger) with Raspbian on it.
- Monitor, keyboard, mouse (if you’re not using SSH or VNC) for initial setup.
- USB power supply with cable.
- Ethernet cable
If you have a Raspberry Pi 3, then just skip to the next heading but if you are using an older version of the Pi along with a USB WiFi dongle, you need to make sure that it supports WiFi Access point. To do this, open up the terminal with the dongle plugged in and type:
lsusb
We found out that this dongle doesn’t support AP mode by default. if you get a different result, search online for it and see if it is supported.
Setting up your Raspberry Pi as a wireless access point
Very basic knowledge of Linux and Networking would be useful (but not mandatory) since we’re going to get into the command line here. Plug everything in and get into the terminal.
1. Update and Upgrade
Before moving to installing and setting up the packages, you’ll have to update and perform an upgrade on pi by entering the following commands:
sudo apt-get update sudo apt-get upgrade
2. Install Hostapd and Dnsmasq
Now, let us install our two software packages i.e hostapd and dnsmasq :
sudo apt-get install hostapd sudo apt-get install dnsmasq
Stop these services temporarily for doing a couple of other changes by:
sudo systemctl stop dnsmasq sudo systemctl stop hostapd
Hostapd is the one which allows the user to use the WiFi as an access point or as a hotspot. Dnsmasq acts as both a DHCP and DNS server so that the user can assign IP addresses and it can process the requests of DNS through Raspberry pi itself, and also it becomes easy to configure.
3. Adding a static IP for the access point
Assuming that the standard 192.168.###.### IP is being used for networking we are going to set up the WiFi IP to 192.168.0.1 by editing the DHCP configuration file:
sudo nano /etc/dhcpcd.conf
then add these lines at the end of the file and save it-
interface wlan0 static ip_address=192.168.0.1/24
Now, restart the DHCP service by:
sudo service dhcpcd restart
4. Configuration of dnsmasq
Dnsmasq’s default configuration contains lots of information which is not being used here. Hence, we will rename the existing file and create a new one, so that dnsmasq will use this file:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig sudo nano /etc/dnsmasq.conf
Give the IP addresses range for the wlan0 interface in the renamed file:
interface=wlan0 dhcp-range=192.168.0.2,192.168.0.20,255.255.255.0,24h
5. Configuration of Hostapd
You need to configure the host software package, to do that:
sudo nano /etc/hostapd/hostapd.conf
A new file will be created. Type the following commands in the new file:
interface=wlan0 bridge=br0 hw_mode=g channel=7 mm_enabled=0 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP ssid=HOST_NAME wpa_passphrase=PASSWORD
ssid is the host name which will broadcast to the other devices and wpa_passphrase is the password.You need to change these to your preference.
Now we need to link this hostapd file as a default:
sudo nano /etc/default/hostapd
In this file go to the line #DAEMON_CONF=” ”, uncomment it by deleting the # before it and enter your path in between the quotes. Which looks similar to the following line:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
6. Enable IP forwarding.
Forwarding is necessary if any devices connected to your access point needs to connect to the internet. That can be done by:
sudo nano /etc/sysctl.conf
search for the following line and uncomment it:
#net.ipv4.ip_forward=1
So, it looks like:
net.ipv4.ip_forward=1
7. Start the services
Start the hostapd and dnsmasq services by:
sudo service hostapd start sudo service dnsmasq start
8. Configuring NAT(Network Address Translation)
NAT(Network Address Translation) will forward all the traffic from your access point over to your ethernet connection. This can be done by:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Save it:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
To make your Pi run as an access point by default after every restart, we need to edit the rc.local file:
sudo nano /etc/rc.local
Add these commands just above “exit 0”:
iptables-restore < /etc/iptables.ipv4.nat
8. Bridge Traffic from AP to Ethernet
We are done setting the Pi as a WiFi access point and you can reboot the Pi and connect to its WiFi from another device. But you still won’t be able to connect to the internet as we haven’t yet bridged the incoming traffic from the WiFi AP to the Ethernet’s internet connection. To do that:
sudo apt-get install hostapd bridge-utils
Again, stop hostapd for configuration by:
sudo systemctl stop hostapd
Now, we add a new bridge:
sudo brctl addbr br0
Connect Ethernet to the bridge by:
sudo brctl addif br0 eth0
9. Add the new bridge interface
The interface needs to be edited to make the connection between the WiFi access point and ethernet work. This is done by:
sudo nano /etc/network/interfaces
Add the bridging rule:
# Bridge setup auto br0 iface br0 inet manual bridge_ports eth0 wlan0
Edit the hostapd file again as mentioned in step 5 and add the following line just below
interface=wlan0
and uncomment the driver line:
interface=wlan0 bridge=br0 driver=nl80211
If your WiFi dongle uses a different driver, you need to mention that here.
And now you’re done setting up your Raspberry Pi as a WiFi Hotspot or Access Point or Router or whatever you want to call it. Do let us know if this worked for you in the commetns section below 🙂