Tutorial: wlan-hotspot

Install required packages:

sudo apt-get install hostapd dnsmasq

Set static ip to your wlan interface:

nano /etc/network/interfaces

content: (check for a single wlan0 entry!)

iface wlan0 inet static
address 192.168.20.1
netmask 255.255.255.0

Add a configuration to hostap:

nano /etc/default/hostapd

content: (add config file line):

# 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"

If you are not sure what driver is needed check your usb devices (driver name will be disbplayed)

lsusb -t
lsusb -t
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
|__ Port 1: Dev 2, If 0, Class=vend., Driver=smsc95xx, 480M
|__ Port 5: Dev 3, If 0, Class=vend., Driver=rtl8192cu, 480M

Create hostapd.conf:

nano /etc/hostapd/hostapd.conf

content:

# interface for ap
interface=wlan0
# driver of wlan stick
driver=rtl871xdrv

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0

# wlan ap configuration
ssid=RaspberryPi
channel=1
hw_mode=g
ieee80211n=0

# wlan security
wpa=2
wpa_passphrase=super-secure-password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP

# country code
country_code=US

Add dnsmasq configuration:

nano /etc/dnsmasq.conf

Content: (two important lines)

# Configuration file for dnsmasq.

# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.
# Repeat the line for more than one interface.
interface=wlan0

# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
dhcp-range=192.168.20.100,192.168.20.200,255.255.255.0,12h
#set default gateway (if wanted)
#dhcp-option=3,192.168.20.1

Restart services:

sudo service hostapd restart
sudo service dnsmasq restart

Enable IP forwarding:

nano /etc/sysctl.conf

Uncomment:

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Activate settings:

sudo sysctl -p

Setup forwarding rules:

nano /etc/network/if-up.d/apforwarding && chmod +x /etc/network/if-up.d/apforwarding 

content:

# !/bin/sh
iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
iptables --append FORWARD --in-interface wlan0 -j ACCEPT

Reboot your Raspberry Pi.