Turn a RaspBerryPi 3 into a WiFi router-hotspot

Requirements

  • 1x RaspBerryPi model 3 (earlier versions will not work unless you have a WiFi dongle. In that case, just follow this Adafruit tutorial)
  • 1x Ethernet cable
  • Ethernet access

Context

Initial checks

ifconfig -a# You should see something like this:
#eth0 Link encap:Ethernet HWaddr b8:27:eb:77:8a:b2
# inet addr:192.168.2.3 Bcast:192.168.2.255 Mask:255.255.255.0
# inet6 addr: fe80::395:c29a:6e8:3756/64 Scope:Link
# UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
# RX packets:1257 errors:0 dropped:0 overruns:0 frame:0
# TX packets:932 errors:0 dropped:0 overruns:0 carrier:0
# collisions:0 txqueuelen:1000
# RX bytes:417879 (408.0 KiB) TX bytes:165808 (161.9 KiB)
#
#lo Link encap:Local Loopback
# inet addr:127.0.0.1 Mask:255.0.0.0
# inet6 addr: ::1/128 Scope:Host
# UP LOOPBACK RUNNING MTU:65536 Metric:1
# RX packets:325 errors:0 dropped:0 overruns:0 frame:0
# TX packets:325 errors:0 dropped:0 overruns:0 carrier:0
# collisions:0 txqueuelen:1
# RX bytes:26584 (25.9 KiB) TX bytes:26584 (25.9 KiB)
#
#wlan0 Link encap:Ethernet HWaddr b8:27:eb:22:df:e7
# inet addr:192.168.42.1 Bcast:192.168.42.255 Mask:255.255.255.0
# inet6 addr: fe80::6b78:7810:9062:550a/64 Scope:Link
# UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
# RX packets:24737 errors:0 dropped:25 overruns:0 frame:0
# TX packets:841 errors:0 dropped:0 overruns:0 carrier:0
# collisions:0 txqueuelen:1000
# RX bytes:4045148 (3.8 MiB) TX bytes:360850 (352.3 KiB)

Software setup

  • hostapd
  • isc-dhcp-server
sudo apt-get update
sudo apt-get install hostapd isc-dhcp-server

DHCP server

# Be wise and always make a backup of the default config
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.default
# Edit the defult config file
sudo nano /etc/dhcp/dhcpd.conf
# Comment the following lines...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
# ...to read:
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
# ...and un-comment this line
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# ... to read:
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
# ... scroll down at the bottom of the file (CTRL + V) and paste:
subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
sudo nano /etc/default/isc-dhcp-server# Edit this line:
INTERFACES=""
# ...to read
INTERFACES="wlan0"
# First, shut it down...
sudo ifdown wlan0
# ...keep it safe and make a backup file:
sudo cp /etc/network/interfaces /etc/network/interfaces.backup
# ...edit the network interfaces file:
sudo nano /etc/network/interfaces
# ... edit accordingly to read:
source-directory /etc/network/interfaces.d
auto loiface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
post-up iw dev $IFACE set power_save off
# ...close the file and assign a static IP now
sudo ifconfig wlan0 192.168.42.1

Hostapd

# Create a file and edit it:
sudo nano /etc/hostapd/hostapd.conf
# ...modify ssid with a name of your choice and wpa_passphrase to a WiFi authen
interface=wlan0
ssid=WiPi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=xyz
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
basename $( readlink /sys/class/net/wlan0/device/driver )
# Create a backup file
sudo cp /etc/sysctl.conf /etc/sysctl.conf.backup
# ...edit the config file
sudo nano /etc/sysctl.conf
# ...un-comment or add to the bottom:
net.ipv4.ip_forward=1
# ...and activate it immediately:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
# ...modify the iptables to create a network translation between eth0 and the wifi port wlan0
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
# ...make this happen on reboot by runnig
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
# ...and editing again
sudo nano /etc/network/interfaces
# ...appending at then end:
up iptables-restore < /etc/iptables.ipv4.nat
# Our /etc/network/interfaces file will now look like this:
source-directory /etc/network/interfaces.d
auto loiface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
source-directory /etc/network/interfaces.d
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf # Your hotspot is up and running: try to connect to it from a computer or a smartphone. When you do so, you should also see some log activity on your terminal. If you're satisfied, stop it with CTRL+D # Let's clean everything:
sudo service hostapd start
sudo service isc-dhcp-server start
# ...and make sure that we're up and running:
sudo service hostapd status
sudo service isc-dhcp-server status
# ...let's configure our daemons to start at boot time:
sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable
# ...reboot the pi.
sudo reboot
tail -f /var/log/syslog

--

--

“Cooper, this is no time for caution!” I work for @AWSCloud & my opinions are my own

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Edo Scalafiotti

Edo Scalafiotti

“Cooper, this is no time for caution!” I work for @AWSCloud & my opinions are my own