Turn a Raspberry Pi into a captive Wi-Fi access point




Introduction

I give IT courses and I wanted to create a server accessible through WI-FI. The server should be light and small. I did not want to carry a laptop around...

I chose to use a Raspberry Pi 2 model B with a WIFI adapter.

By the time this article was written, the Raspberry 3, which include an integrated WI-FI adapter was available. However, some reports indicated that the board heats a lot. See this article: http://makezine.com/2016/03/02/raspberry-pi-3-not-halt-catch-fire/

Therefore, I decided to use the Raspberry Pi 2 model B I already used to run some development tools (an Apache server, an SVN server, a GIT server, a Satis server and a « private-bower » server).

Requirements

Check your current OS with the command lsb_release -da. Mine is:

$ lsb_release -da
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 8.0 (jessie)
Release:        8.0
Codename:       jessie
Installation

You may need to upgrade your system: sudo apt-get update; sudo apt-get upgrade

Plug the WI-FI adapter into the USB slot. Then, make sure that your WI-FI adapter is correctly integrated to the system:

$ lsusb
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Install hostapd and dnsmasq: sudo apt-get install hostapd dnsmasq

Note that, in case you want to remove it later: sudo apt-get --purge remove hostapd dnsmasq (don't do that now !)

.
Configuration
Hostapd

Now, you must create the file /etc/hostapd/hostapd.conf.

If you want to create a WPA2 secured access point, then put the following configuration into your file:
interface=wlan0
driver=nl80211
ssid=denis-hotspot
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Your_password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
If you want to create an open (no authentication) access point, then put the following configuration into your file:
interface=wlan0
driver=nl80211
ssid=denis-hotspot
channel=6

Notes:

Then, edit the file /etc/default/hostapd. You must tell hostapd that it must load a configuration file.

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#   -d   show more debug messages (-dd for even more)
#   -K   include key data in debug messages
#   -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
dnsmasq

Edit the file /etc/dnsmasq.conf and set the following variables:

With the configuration above, the DNS service (on your raspberry) is based on the Raspberry's "/etc/hosts" file.

Using dnsmasq you can build a "catch-all DNS" : all DNS queries will return the same IP address. This can be useful. Let's say that we want all DNS queries to return the IP address of the raspberry pi. To do that, just add the following line into the file /etc/dnsmasq.conf:

address=/#/10.0.0.32

Note:

System

Configure your system so that wlan0 will be assigned a "well-known" static IP address. To do that, edit the file /etc/network/interfaces:

$ cat /etc/network/interfaces
# Please note that this file is written to be used with dhcpcd.
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'.

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

auto wlan0
iface wlan0 inet static
  address 10.0.0.32
  netmask 255.255.255.0

Configure your system so that hostapd is started automatically after boot: sudo update-rc.d hostapd enable

Configure your system so that dnsmasq is started automatically after boot: sudo update-rc.d dnsmasq enable

Notes:

Disable ifplugd for wlan0. To do that, edit the file /etc/default/ifplugd and replace:

INTERFACES="auto"
HOTPLUG_INTERFACES="all"

By:

INTERFACES="eth0"
HOTPLUG_INTERFACES="eth0"

Note: see http://sirlagz.net/2013/02/10/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-3b/

Test your configuration

Then restart your system: sudo shutdown -r now

Check that hostapd is running: ps awx | grep hostapd

Check that dnsmasq is running: ps awx | grep dnsmasq

Check the configuration for wlan0: iwconfig wlan0 and ifconfig wlan0

$ iwconfig wlan0
wlan0     IEEE 802.11bgn  Mode:Master  Tx-Power=20 dBm   
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:off

Make sure that you get: Mode:Master.

$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr e8:4e:06:35:c5:0e  
          inet addr:10.0.0.32  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::28fa:ac57:e41d:3b51/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:91 errors:0 dropped:1 overruns:0 frame:0
          TX packets:149 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13110 (12.8 KiB)  TX bytes:30262 (29.5 KiB)

Make sure that you get: inet addr:10.0.0.32.