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
country_code=CH
driver=nl80211
interface=wlan1
bridge=br0
hw_mode=g
channel=5
ssid=sushi
wpa=3
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
wpa_passphrase=tempura
#ieee80211n=1
#ht_capab=[HT40+][SHORT-GI-20][SHORT-GI-40]

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 10.157.157.0/24 as quite unlikely to clash with other networks:

# Wifi AP pool
subnet 10.157.157.0 netmask 255.255.255.0 {
        option domain-name-servers 10.157.157.1;
        max-lease-time 7200;
        default-lease-time 600;
        range 10.157.157.50 10.157.157.60;
        option subnet-mask 255.255.255.0;
        option broadcast-address 10.157.157.255;
        option routers 10.157.157.1;
        }

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
    address 10.157.157.1
    network 10.157.157.0
    netmask 255.255.255.0
    broadcast 10.157.157.255
    bridge-ports wlan1

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

INTERFACES="br0"

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

net.ipv4.ip_forward=1

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
#!/bin/sh
/sbin/ifdown --force wlan1
/bin/sleep 3
/sbin/ifup --force wlan1
EOT

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

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

iptables -t nat -A POSTROUTING -s 10.157.157.0/24 -o eth0 -j MASQUERADE
iptables -A FORWARD -s 10.157.157.0/24 -o eth0 -j ACCEPT
iptables -A FORWARD -d 10.157.157.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i eth0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.157.157.0/24 -o wlan0 -j MASQUERADE
iptables -A FORWARD -s 10.157.157.0/24 -o wlan0 -j ACCEPT
iptables -A FORWARD -d 10.157.157.0/24 -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.

Advertisements

, , ,

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

    Hi,
    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
    Dexter

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: