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 (http://www.kombitz.com/2009/02/09/windows-7-how-to-create-an-ad-hoc-network/) 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
address 192.168.0.1
netmask 255.255.255.0
broadcast 192.168.0.255
wireless-mode ad-hoc
wireless-essid YOUR-NETWORK-SSID-HERE
wireless-key YOUR-NETWORK-KEY

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

INTERFACES="wlan0"
(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 "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

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

and replace them with:

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

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

Then paste this at the end of the file:

subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.100 192.168.0.200;
option domain-name-servers 192.168.0.1;
# option domain-name "internal.example.org";
option routers 192.168.0.1;
option broadcast-address 192.168.0.255;
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.

References:




10 comments:

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

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

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

    ReplyDelete
  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

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

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

    ReplyDelete
  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.

    Regards,

    Gayan

    ReplyDelete
  7. I found a work around for my android device http://anxiousnut.wordpress.com/2011/09/10/a-weird-android-obtaining-ip-address-wifi-issue-workaround/

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete