Use your laptop as a wifi router under Linux

Introduction

It is easy to setup ad-hoc wifi router in Linux. The problem is, many devices e.g Android (currently) can not connect to it. Network Manager 0.9.8 brings AP-mode Hotspot. However, since it is not integrated into nm-applet or other easy tools, we will still have to wait some time for a seemless experience. Until then, we will have to rely on hostapd software.

Is your hardware supported ?

According to linuxwireless.org

As far as Linux is concerned, out of the old drivers you can only use HostAP, madwifi, prism54 drivers with hostapd. All new mac80211 based drivers that implement AP functionality are supported with hostapd’s nl80211 driver. Userspace programs like hostapd now use netlink (the nl80211 driver) to create a master mode interface for your traffic and a monitor mode interface for receiving and transmitting management frames.

Enough theory. You can find out if your card falls under one of these categories using following command.

$ lspci -k | grep -A 3 -i "network"
02:00.0 Network controller: Intel Corporation Centrino Wireless-N 1030 [Rainbow Peak] (rev 34)
 Subsystem: Intel Corporation Centrino Wireless-N 1030 BGN
 Kernel driver in use: iwlwifi

As you can see, the driver used in this case is iwlwifi.

Let us find out more information about this driver’s module.

$ modinfo iwlwifi | grep 'depend'
depends: cfg80211

To find out if this driver is supported by hostapd, I just do is search the term “cfg80211 hostapd” on google. In this limited post I cannot list all supported drivers.

Installation

You will need to install two software (hostapd and dhcp3-server). On ubuntu installing these software would require following command.

$ sudo apt-get install hostapd dhcp3-server

Hostapd is used to configure our wireless driver to be used as a router, and dhcp server provides ip addresses to connecting devices.

Configuration

This part might look like a lot of work, but it is the most easiest part of the whole tutorial. You just need to copy and paste following texts to necessary files.

a. Create /etc/hostapd/hostapd.conf file and paste the following text.

interface=wlan0
driver=nl80211
ssid=testhostapd
channel=1
hw_mode=g
auth_algs=1
wpa=3
wpa_passphrase=mypassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

Note: Please change some of these values to match your setting. In a laptop most often the wireless interface is wlan0, but to be sure run ifconfig command and find out.

$ ifconfig
wlan0 Link encap:Ethernet ...

You might also want to change the driver (as discussed above), router name (ssid), wireless password (wpa_passphrase) etc.

b. Edit /etc/dhcp/dhcpd.conf file to look like this

ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
# option definitions common to all supported networks…
# option domain-name “example.org”;
# option domain-name-servers ns1.example.org, ns2.example.org;
option domain-name-servers 208.67.220.220,208.67.222.222 ;
subnet 10.42.43.0 netmask 255.255.255.0 { 
 range 10.42.43.50 10.42.43.70; 
 option subnet-mask 255.255.255.0; 
 option broadcast-address 10.42.43.255; 
 option routers 10.42.43.1; 
}

What we did above is, create a dhcp server configuration, commented option lines and left other lines untouched. Lastly added dhcp ip range we want our devices to use.

c. Finally create a file (any name you like) in your home directory. In this example I will create a file called connecthostapd in bin directory of home (~/bin/connecthostapd) with following text.

#!/bin/bash
rmmod iwlwifi
modprobe iwlwifi
iwconfig wlan0 mode Master freq 2.42G essid testhostapd
sleep 2
ifconfig wlan0 10.42.43.1/24
iptables -t nat -A POSTROUTING -s 10.42.43.0/24 -o ppp0 -j MASQUERADE
iptables -A FORWARD -s 10.42.43.0/24 -o ppp0 -j ACCEPT
iptables -A FORWARD -d 10.42.43.0/24 -m state --state ESTABLISHED,RELATED -i ppp0 -j ACCEPT
echo 1 >/proc/sys/net/ipv4/conf/all/forwarding
echo 'INTERFACES=wlan0' >/etc/default/dhcp
dhcpd wlan0
hostapd -d /etc/hostapd/hostapd.conf

In this particular example, I used DSL internet available through ppp0 as the source. If you have some other devices or interface that you want to use (e.g eth0 in case of direct connection) as the internet source, change ppp0 to appropriate device interface in all three places in above file. Make sure the essid matches the name that we used in /etc/hostapd/hostapd.conf, and finally the kernel module we discovered above for wifi (iwlwifi in this case) .

Note: Don’t forget to make this file executable using “chmod +x command”

$ chmod +x ~/bin/connecthostapd

For the sake of completion (dhcp), I suggest you restart your computer this one time.

From now on, you just have to remember only the last file. If you have been browsing the internet using wifi, and decide to use it as a router, you just need to disconnect wifi (don’t turn off), and run above file as root. Which would be something like this.

$ sudo ~/bin/connecthostapd

You can now connect to this device as a normal wifi. To disconnect, simply type “Ctrl + c ” in the terminal where connecthostapd is currently running.

Note: Due to its limitations (in my case) on failing to keep the connection for long time, this is not going to be a replacement for wifi router, in some of your cases. However in situations where wifi does not work, and you want to update some internet related services on devices that depend on wifi (e.g Android) you can do it easily, with above setup.

Conclusion

Sharing internet through wifi has recently got much attention. As a result, most newer hardware support this feature. Hope it works for you. One problem I have to note though is, the performance is rather limited than a dedicated wifi router.

References

1. http://projects.gnome.org/NetworkManager/
2. http://www.linux.org.ru/forum/general/9047756
3. http://www.phoronix.com/scan.php?page=news_item&px=MTMwNzY
4. http://linuxwireless.org/en/users/Documentation/hostapd

14 Comments

Filed under Uncategorized

14 responses to “Use your laptop as a wifi router under Linux

  1. Got everything working with a few modifications however my device says there is no full internet connection so I’m guessing the DNS or something is not working – any idea?

  2. Pingback: Links 8/5/2013: Linux in Space, Android’s Growing Tablet Domination | Techrights

  3. wojtasskorcz

    Hello xpressrazor!
    I’ve got some problems with hostapd, could you take a look?

    wojtek@msi ~ $ lspci -k | grep -A 3 -i “network”
    06:00.0 Network controller: Intel Corporation Centrino Wireless-N 1000
    Subsystem: Intel Corporation Centrino Wireless-N 1000 BGN
    Kernel driver in use: iwlwifi
    Kernel modules: iwlwifi

    wojtek@msi ~ $ modinfo iwlwifi | grep ‘depend’
    depends: mac80211,cfg80211

    So according to your post it should work. On the other hand though:

    wojtek@msi ~ $ sudo iwconfig wlan0 mode master
    Error for wireless request “Set Mode” (8B06) :
    SET failed on device wlan0 ; Invalid argument.

    Which would mean, that my card doesn’t support an AP mode. Also, following your post I have this error when trying to run hostapd:

    wojtek@msi ~ $ sudo hostapd ~/hostapd-test.conf
    Configuration file: /home/wojtek/hostapd-test.conf
    Failed to update rate sets in kernel module
    Could not connect to kernel driver.
    Using interface wlan0 with hwaddr 00:1e:64:55:76:84 and ssid ‘testhostapd’
    random: Only 18/20 bytes of strong random data available from /dev/random
    random: Not enough entropy pool available for secure operations
    WPA: Not enough entropy in random pool for secure operations – update keys later when the first station connects
    Failed to set beacon parameters
    Could not connect to kernel driver.

    Do you have a clue what I’m doing wrong? I tried on Windows and my card can serve as AP there (using Connectify).

    • I don’t have a definite answer, but could you set different modes in the iwconfig command in the script. Could you try using “iwconfig wlan0 mode ad-hoc” from Morten Slott Hansen’s answer. Though, the whole point of this article was to avoid using ad-hoc (but since it works for Morten, may be it will work for you too). Looking at link 1 and link 2, I think it is mode problem. To find out different modes run “man iwconfig” and see the mode section.

      I hope it works for you.

      • wojtasskorcz

        Yes, apparently master mode is not supported. I can’t use ad-hoc mode (although it works), because my android device doesn’t show ad-hoc networks. Well, guess I’ll have to stick to windows then for these rare times I need wifi.

      • I had same problem (ad-hoc not supported in android). It looks like android 4.2 has ad-hoc support (http://www.networkworld.com/community/blog/why-android-422-important-update). Looking at this and this it looks like you need to have both master mode and ap for hostapd. This is the place to download the driver and copy it to /lib/firmware (if something has changed). Since you said it works in windows, you might have to investigate how it works in windows (what modes are used). E.g Do you need to specify specific frequency as Morten Slott Hansen did? Other than that, I don’t think I have any good answer.

  4. Hello, I have a problem with this thing. When I run connecthostapd:

    rmmod: ERROR: Module iwlwifi is in use by: iwldvm
    Error for wireless request “Set Mode” (8B06) :
    SET failed on device wlan0 ; Invalid argument.
    SIOCSIFFLAGS: Operacja niemożliwa ze względu na RF-kill
    Bad argument `ESTABLISHED,RELATED’
    Try `iptables -h’ or ‘iptables –help’ for more information.
    Internet Systems Consortium DHCP Server 4.2.4
    Copyright 2004-2012 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/
    Wrote 0 leases to leases file.

  5. Thanks for the post.

    Almost there, I am able to authenticate device, my case my phone. But looks like dhcpd does not obtain ip address for my phone or tablet. Any ideas ? – don’t see any thing wrong in the log dump, where should I look ?

    • well, I got this error
      Error for wireless request “Set Mode” (8B06) :
      SET failed on device wlan1 ; Invalid argument.

      • Got it.

        Instead configuring dhcp, I use dnsmasq.

        bring the interface up
        ifconfig wlan1 10.42.0.1/24

        and then

        /usr/sbin/dnsmasq –conf-file –no-hosts –keep-in-foreground –bind-interfaces –except-interface=lo –clear-on-reload –strict-order –listen-address=10.42.0.1 –dhcp-range=10.42.0.10,10.42.0.100,60m –dhcp-option=option:router,10.42.0.1 –dhcp-lease-max=50

        also I start hostpod from unit.d
        /etc/init.d/hostapd start

        iptables are needed to forward the internet.

        Skip following commands if you end up with similar issues
        # rmmod iwlwifi
        # modprobe iwlwifi
        # iwconfig wlan0 mode Master freq 2.42G essid testhostapd

        Cheers!