Thursday, January 3, 2013

Hostapd - The Linux Way to create Virtual Wifi Access Point

NOTE: Although this guide should work in most cases, it is not flawless and still requires few minor modifications to make the process bug-free. Please do point out corrections and changes.
(After you are done with this post, please do checkout my Python Hostapd Client)
I was recently looking into ways to use my laptop’s wifi adapter as a wireless access point to enable my phone (Nokia E63) and playstation portable to connect to the internet through it. Ad-hoc feature may be used to share internet through wifi, but it doesn’t work with many phones and my PSP. I found connectify and virtual router for Windows which served this purpose, unsatisfactorily. Other than the reasons like Virtual Router not detecting my 3g modem and Connectify (free version) not allowing me to set desired ssid for my virtual access point, the biggest issue with these two was the limited modes available for the access point. Both the programs offered only WPA2-PSK encryption for infrastructure mode and WEP and open encryption for ad-hoc modes. Many devices connect only through infrastructure mode and support for WPA2-PSK is absent in few devices (including the PSP). Also, since I am a Linux user, I needed something else.
This is where hostapd kicks in.


“hostapd is a user space daemon for access point and authentication servers. “
In simple words, hostapd allows you to create software wifi access points allowing decent amount of configuration options. In rest of this post, I will show how to create a software access point in Linux using hostapd and share your internet to the devices through it. I have used my Lenovo Z560 with ath9k wifi driver under Arch Linux and have also tested it under Ubuntu 11.10. But the method is also applicable for other Linux distros and supported hardware.
If the method works/doesn’t work for a non-Atheros wifi card, please do comment.


  • Supported Wireless Card (ie. supports master mode)
  • An internet connection you want to share. (not strictly a neccessity)
  • A linux distro


First of all, you will need to find if your wireless card is supported by hostapd.
To check what kernel driver is in use for your wireless card, type the follwing in the terminal
1lspci -k | grep -A 3 -i "network"
Look for the section in the output which corresponds to your Wireless controller. In my case, it is
06:00.0 Network controller: Atheros Communications Inc. AR9285 Wireless Network Adapter (PCI-Express) (rev 01)
Subsystem: Lenovo Device 30a1
Kernel driver in use: ath9k
The Bold part is my kernel driver in use. It will vary depending on your wifi card and driver you are using.
Now get the interface details of your wireless driver by
1modinfo ath9k | grep 'depend'
replace ath9k by your wifi kernel driver you determined in the last step. In my case, the output was
depends: ath9k_hw,mac80211,ath9k_common,ath,cfg80211
modinfo says my Kernel driver supports mac80211 interface which is supported by hostapd which implies that my wifi card is compatible with hostapd.

Supported wireless cards/drivers


Install Hostapd from your distro’s repo
sudo  apt-get update && sudo apt-get installhostapd
Or Download Hostapd here and compile it.


The /etc/hostapd/hostapd.conf is the main configuration which you need to deal with in order to set up a SoftAP.
This is the minimal configuration setting which will let you test if hostapd is working. Create a file ~/hostapd-test.confwith the following content:
start hostapd by
1sudo hostapd ~/hostapd-test.conf
Use a wifi device to check if the access point is being detected. You won’t be able to connect to it at present.
Once hostapd is working fine, its time to configure hostapd with more options.
Here is a brief overview of some of its options:
So, here is my complete /etc/hostapd/hostapd.conf with WPA authentication options.


Now that hostapd is running fine, you need to setup a DHCP server to run along with hostapd in order to assign ip address to the devices connecting to the access point. Setting up a dhcp server is quite straightforward.
Install dhcp server from your distro’s repo.
sudo  apt-get update && sudo apt-get installdhcp3-server
edit /etc/dhcpd.conf (for arch linux) or /etc/dhcp/dhcpd.conf (for Ubuntu) to
01ddns-update-style none;
04option local-wpad code 252 = text;
0710.0.0.0 netmask {
15option broadcast-address
18option domain-name-servers
23default-lease-time 1209600;
24max-lease-time 1814400;
options are easy to understand and you may change it according to your needs (if required).


The final steps involves enabling NAT to share internet in one network interface  with the clients connected through hostapd.
I have included all the steps to configure wlan interface, enable NAT, start DHCP server and hostapd in the BASH script below
Let the name of this file be initSoftAP.Copy the BASH file below to the file initSoftAP.(and make changes to file according to your system)
03ifconfig $1 up netmask
08if "$(ps -e | grep dhcpd)" == "" ]; then
14iptables --table nat --flush
15iptables --delete-chain
16iptables --table nat --delete-chain
17iptables --table nat --append POSTROUTING --out-interface $2 -j MASQUERADE
18iptables --append FORWARD --in-interface $1 -j ACCEPT
24sysctl -w net.ipv4.ip_forward=1
26hostapd /etc/hostapd/hostapd.conf 1>/dev/null
Script Changes (12/9/12) : Added check for already running dhcpd process (Thanks to Panji), Added an optional line to fix issues related to PPPoE connection sharing (See lorenzo’s comment)
It might be more convenient to use hostapd -B /etc/hostapd/hostapd.conf which runs hostapd in background. (Thanks to Enda for pointing out)
Make this file executable, and run it. The syntax for executing it is
./initSoftAP wifi_card_interface interface_with_internet
The “wifi_card_interface” will be wlan0 most of the cases. For “interface_with_internet“, since I want to share internet from my ethernet network interface, I used eth0. If I ever want to share internet from my 3g modem, I use ppo0. (These values need not be same for everyone)
You may see available network interfaces by
Note: If dhcpd is failing to start and throwing errors like No subnet declaration for wlan0, take a look at these comments by Maheshand Charlie.

Source :


Post a Comment