Saturday, April 17, 2010

Turn your laptop into a wireless router in Ubuntu

It is easy to setup an ad-hoc wireless network on Windows ( to share your internet connection with another pc over a wireless connection but doing the same on Ubuntu is complicated. No wizards here, just plain old terminal commands.

Converting your laptop into a wireless router is an easy way to share your internet connection (broadband) with another laptop without requiring a multi port router or a wirless router or any cable connections. See diagram below..

Here's how I did it on Ubuntu 9.04. Took a lot of experimenting to find the right method but the following steps finally got it working.

-First, install the packages dnsmasq, ipmasq, and dhcp3-server

sudo apt-get install dnsmasq
sudo apt-get install ipmasq
sudo apt-get install dhcp3-server

-Assign a static IP address for the wireless card on the desktop machine by editing the file /etc/network/interfaces. Add these lines to the end of the file (if you already see wlan0 somewhere else, delete that first):

auto wlan0
iface wlan0 inet static
wireless-mode ad-hoc
wireless-key YOUR-NETWORK-KEY

-Next edit the file /etc/default/dhcp3-server by finding the line with INTERFACES="" and replacing it with

(This tells the DHCP server to listen on the local network for connections)

-Now open the file /etc/dhcp3/dhcpd.conf. Find the following lines:

option domain-name "";
option domain-name-servers,;

default-lease-time 600;
max-lease-time 7200;

and replace them with:

#option domain-name "";
#option domain-name-servers,;

#default-lease-time 600;
#max-lease-time 7200;

Then paste this at the end of the file:

subnet netmask {
option domain-name-servers;
# option domain-name "";
option routers;
option broadcast-address;
default-lease-time 600;
max-lease-time 7200;

-Set up IP masquerading and forwarding:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo dpkg-reconfigure ipmasq

The default answers should be fine.

-Set up DNS masquerading:

sudo dpkg-reconfigure dnsmasq

-Now start everything up!

sudo ifdown wlan0
sudo ifup wlan0
sudo /etc/init.d/dnsmasq restart
sudo /etc/init.d/dhcp3-server restart

Now if everything went well you would be able to see the newly setup netowrk in the list of wireless networks of your other pc and you can connect to it:)

However when running sudo ifdown wlan0 I got the error..

Error for wireless request "Set Mode" (8B06) : SET failed on device wlan0 ; Invalid argument.Error for wireless request "Set ESSID" (8B1A) : SET failed on device wlan0 ; Invalid argument.
sudo modprobe ndiswrapper

I think this is because my wireless network card did not have a native Linux driver. I fixed this by running..

sudo modprobe ndiswrapper

Ndiswrapper is a Linux module which allows Ubuntu to use the Windows driver for wireless cards (in most cases).

Everything is working fine now and I'm sharing my broadband internet connection with another laptop via a wireless connection now.



  1. Thanks for this excellent advice. It will really help a lot of people. I shared it with my friends on Facebook and Twitter.

  2. Nice post. I shall try this myself. Thanks for sharing this really useful information.

  3. I had some problems, I couldnt edit the interfaces file. any idea why?

  4. @dhisey check the file permissions. it should be set to root so you will need to open it using sudo command for editing.

    sudo gedit filepathname

  5. sudo vim /path/to/file/being/edited

    Of course, you can substitute "vim" for "nano" or your favorite editor.....

  6. Hi,

    Does this work with mobiles ? I get the accesspoint created although i cant connect my mobile to the network.
    It fails assigning a ip address.



  7. I found a work around for my android device

  8. Nice document.

    Do we need to put these commands below in a file (which file?) for them to run at start-up(boot) or are they run only once and we forget about them?

    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
    sudo dpkg-reconfigure ipmasq

  9. While much has been written on this topic, your article expresses both the positive and negative aspects of this important topic, without taking a boring stance on either side of the issue. Harini Sirisena, I must Thank you for your thorough research and clear writing.
    buy paper writer online

  10. I always enjoy your article, Harini Sirisena. You have a gift for discussing such inspirational topics in truthful yet amusing ways. Your articles help us realize that our problems are typical, and we can solve them in constructive ways. Thank you and keep these good articles coming.
    homework writing service