Turn your laptop (running Linux) into a captive WI-FI hotspot




Introduction

This article shows how to turn your laptop (running Linux) into a captive WI-FI hotspot.

I assume that the WI-FI card shipped with your laptop can be used as an access point. If this is not the case, then you can use a cheap WI-FI USB adapter (for example: EDUP EP-N8531 WI-FI USB adapter).

Please note that if you choose to use a WI-FI USB adapter, then you may have to adapt the command line examples given in this article. Depending on your laptop’s specifications, you may need to replace « wlan0 » by whatever wireless interface identifier that is right for you (it could be « wlan1 », « wlan2 »…).

Please also note that I use Mint 17 as operating system. This OS is based on Ubuntu 14.04 (kernel 3.13). If you are using another version of Linux, some details of the procedure may be different. However, the overall explanation is similar.

Requirements

My laptop's OS:

$ lsb_release -da
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.2 Rafaela
Release:        17.2
Codename:       rafaela
Installation

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

If you use a WI-FI USB adapter, then plug the adapter into an USB slot. Then, make sure that your WI-FI adapter is correctly integrated to the system. The following output comes from the EDUP EP-N8531 WI-FI USB adapter:

$ 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.

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

Make a backup copy of the file /etc/dnsmasq.conf:

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.org

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

Note: you may have to change wlan0 by whatever identifier applies to your system.

With the configuration above, the DNS service (on your laptop) is based on your laptop'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 laptop (10.0.0.32). To do that, just add the following line into the file /etc/dnsmasq.conf.ap:

address=/#/10.0.0.32

Note: to get the list of all files associated to the package, you can use the following command: dpkg-query -L dnsmasq.

System

Configure your system so that wlan0 (or whatever identifier applies to your system) will be assigned a "well-known" static IP address.

Make a backup copy of the file /etc/network/interfaces:

sudo cp /etc/network/interfaces /etc/network/interfaces.org

Create the file /etc/network/wifi/interfaces.ap:

auto lo
iface lo inet loopback
ihwaddress ether cc:e7:df:03:89:11

auto wlan0
iface wlan0 inet static
  address 10.0.0.32
  netmask 255.255.255.0

Note: you may have to change wlan0 by whatever identifier applies to your system.

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

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

Notes:

OPTIONAL: you MAY need to disable ifplugd (especially if you use a WI-FI adapter) for wlan0 (or whatever wireless interface identifier that is right for you). To do that, edit the file /etc/default/ifplugd and replace:

INTERFACES="auto"
HOTPLUG_INTERFACES="all"

By:

INTERFACES="eth0"
HOTPLUG_INTERFACES="eth0"

Note:

Create the script start-ap.sh that will turn the WI-FI interface into an access point.

stop network-manager
cp /etc/network/wifi/interfaces.ap /etc/network/interfaces
ifdown wlan0
ifup wlan0
cp /etc/dnsmasq.conf.ap /etc/dnsmasq.conf
/etc/init.d/hostapd start
/etc/init.d/dnsmasq start

Note: you may have to change wlan0 by whatever identifier applies to your system.

Create the script stop-ap.sh that will stop the access point and restore nominal operation mode.

/etc/init.d/hostapd stop
/etc/init.d/dnsmasq stop
cp /etc/network/interfaces.org /etc/network/interfaces
cp /etc/dnsmasq.conf.org /etc/dnsmasq.conf
ifdown wlan0
ifup wlan0
start network-manager

Note: you may have to change wlan0 by whatever identifier applies to your system.

Test your configuration

Then restart your system: sudo shutdown -r now

Check that hostapd is NOT running: ps awx | grep hostapd

Check that dnsmasq is NOT running: ps awx | grep dnsmasq

Check the configuration of wlan0:

$ iwconfig wlan0
wlan0     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=20 dBm   
          Retry short limit:7   RTS thr=2347 B   Fragment thr:off
          Encryption key:off
          Power Management:off
$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 6c:71:d9:03:89:11  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Start the access point: sudo ./start-ap.sh.

Stop the access point: sudo ./stop-ap.sh.