WiFi hotspot with an Ubuntu netbook

My task was to set up my Ubuntu netbook (Acer Aspire One D255E-13DQws) so that it acts as a wi-fi access point whenever I insert a USB WiFi adapter, and shares its existing connection, be it a wired RJ45 or WiFi connection on the built-in wi-fi card.

Among other use scenarios, it may be useful in hotels where only one WiFi client device is allowed in a room.

The below scenario is tested with Ubuntu 11.10, and it should work with older versions too. The TP-Link TL-WN821N is used by default, and also a Ralink3070 based adapter was working the same way.

Install additional packages:

apt-get install bridge-utils isc-dhcp-server bind9 iw hostapd iptables-persistent

Insert your Wi-fi USB adapter and check if AP mode is supported:

iw list

On my netbook, the USB adapter got the name wlan1. Create the hostapd configuration as shown below. You need to adapt it to your country code, channel number, SSID, and the password. Also for some unknown reason, 802.11N mode resulted in maximum 15Mbps connection, while the plain 802.11g was working fine at 54Mbps.

cat >/etc/hostapd/hostapd-wlan1.conf <<EOT
wpa_pairwise=TKIP CCMP

Disable the automatic startup of DHCP and DNS services, as we only need them in Access Point mode:

update-rc.d bind9 disable
update-rc.d isc-dhcp-server disable
/etc/init.d/bind9 stop
/etc/init.d/isc-dhcp-server stop

Configure the IP pool in /etc/dhcp/dhcpd.conf as follows. I took the network as quite unlikely to clash with other networks:

# Wifi AP pool
subnet netmask {
        option domain-name-servers;
        max-lease-time 7200;
        default-lease-time 600;
        option subnet-mask;
        option broadcast-address;
        option routers;

Add the wlan1 configuration to /etc/network/interfaces:

# Do not add "auto wlan1" here. With it, the netbook will look for this 
# interface on boot-up, and it will take about two extra minutes if the USB card 
# is not plugged in 
iface wlan1 inet manual
    hostapd /etc/hostapd/hostapd-wlan1.conf
    post-up /etc/init.d/isc-dhcp-server start
    post-up /etc/init.d/bind9 start
    pre-down /etc/init.d/isc-dhcp-server stop
    pre-down /etc/init.d/bind9 stop

#Bridge interface
auto br0
iface br0 inet static
    bridge-ports wlan1

In /etc/default/isc-dhcp-server , configure DHCP service to listen on br0 interface only:


In /etc/sysctl.conf , allow IP forwarding:


Reboot the machine.

Configure udev to bring up the wlan1 interface when the USB card is inserted:

mkdir /etc/txlab
cat >/etc/txlab/wifi_hotspot_init <<EOT
/sbin/ifdown --force wlan1
/bin/sleep 3
/sbin/ifup --force wlan1

chmod a+x /etc/txlab/wifi_hotspot_init

cat >/etc/udev/rules.d/90-wifi-hotspot.rules <<EOT
# Bring up the Wifi access point
SUBSYSTEM=="net", ACTION=="add", NAME=="wlan1", RUN="/etc/txlab/wifi_hotspot_init"

Configure iptables to work as NAT gateway between eth0 and wlan1 and br0:

iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE
iptables -A FORWARD -s -o eth0 -j ACCEPT
iptables -A FORWARD -d -m conntrack --ctstate ESTABLISHED,RELATED -i eth0 -j ACCEPT
iptables -t nat -A POSTROUTING -s -o wlan0 -j MASQUERADE
iptables -A FORWARD -s -o wlan0 -j ACCEPT
iptables -A FORWARD -d -m conntrack --ctstate ESTABLISHED,RELATED -i wlan0 -j ACCEPT
# save the rules for persistent use
iptables-save >/etc/iptables/rules.v4

Now insert the Wifi card, and it should immediately start as access point. Use the command “iwconfig” to see the actual status of wifi adapters.


, , ,

  1. #1 by Dexter on March 27, 2013 - 2:45 pm

    I followed all your instructions and could get my usb wifi hotspot running on linux, my android device could detect and connect to the hotspot and also obtain a dynamic ip, but I cannot browse the internet from android.

    Please help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: