Wednesday, April 29, 2009

HOWTO: Share internet connection (painlessly) from your Ubuntu machine over Wireless

Internet connection sharing (ICS) had been one of the insane things that I've always made use of, be it in Windoze or otherwise. And now with us using mobile broadband, me having my Acer Aspire One notebook installed with Ubuntu on it, it's time to take up the challenge again to get this up and running! No need for Wireless@SG anymore, I can BECOME Wireless@SG! :D

Problem was, after reading many Ubuntu forums and HOWTOs, trying out Firestarter many times (and I'm currently too lazy to learn to configure iptables myself manually), I was almost ready to give up, until I read up on ipmasq out of curiousity... You would need to use the terminal, so if you have an allergy to the terminal it'd be good to learn and overcome that first :)

If this was helpful to you, or if you have any questions, do leave a comment!

This HOWTO was done on the following:
  • Ubuntu Jaunty 9.0.4 UNR (Ubuntu Netbook Release)
  • Huawei E180 HSPA USB Modem
  • Acer Aspire One A150

Here's what you do:
1) Install dnsmasq and ipmasq
Run the following command in terminal:
sudo apt-get install dnsmasq ipmasq -y

2) Disable dnsmasq from autostarting
In your terminal, type:
sudo gedit /etc/default/dnsmasq

Look for the line that states "ENABLED=1" and change it to "ENABLED=0"

3) Kill off dnsmasq daemon
We don't want dnsmasq to be running as a daemon here, since NetworkManager tries to start it for shared connections.
sudo killall dnsmasq

4) Enable wireless
You should know how to do this if it's not already on...

5) Configure wireless
  1. Left click network manager applet in the top panel.
  2. Select "Create new wireless network".
  3. Type in the network name and change your security settings as needed.
This would tell NetworkManager to create an ad-hoc wireless network.

6) Check wireless config
  1. Right click network manager applet.
  2. Select "Edit Connections...".
  3. Go to the "Wireless" tab.
  4. Select your ad-hoc network and click on "Edit".
  5. Go to "IPv4 Settings" tab
  6. The method should be "Shared to other computers"
This is so that dnsmasq will run whenever this network is connected to, providing us a DHCP and DNS server for that ad-hoc network.

7) Configure ipmasq
In terminal, type:
sudo dpkg-reconfigure ipmasq
  1. Should PPP connections recompute the firewall? Yes
  2. Just press Ok
  3. When should ipmasq be started? After network interfaces are brought up

8) Connect internet facing modem connection if not already done so
For me I just select my service provider and Ubuntu "dials up" to connect

9) Run ipmasq to configure iptables
iptables configs does the actual routing, ipmasq helps configure iptables PAINLESSLY :)
From terminal, type:
sudo ipmasq

10) Get ipmasq to run whenever a network interface goes up or down
iptables configurations are not persistent, so I use ipmasq to autoconfig for me everytime a network interface goes up. Note that you can also use other methods to persist iptables. NetworkManager fires off scripts in /etc/network/if-up.d whenever an interface goes up, so let's tell it to run ipmasq too.
In terminal, type these commands:
cd /etc/network/if-up.d
sudo gedit config-iptables
Copy and paste the following into the editor, save and exit.
#! /bin/sh

/usr/sbin/ipmasq
Then in terminal again, make the script executable:
sudo chmod +x config-iptables
You will want ipmasq to autoconfig again when a network interface goes down, so run the following in terminal:
sudo cp config-iptables /etc/network/if-post-down.d

11) Connect client, start surfing :)
You're done! Now just get another wifi client to join in the ad-hoc network, and after it gets an IP automatically we should be good to go!

8 comments:

Anonymous said...

Thanks for your howto, saved me a lot of trouble with Jaunty NBR. Having played around a bit more I found that if you add the iptables package rather than ipmasq the network sharing feature works as it should without any further configuration (it seems like iptables was stripped out of the NBR version, dnsmasq-base should be installed by default with Jaunty). Hope this helps!

Grant said...

This article was very helpful and worked perfectly when I needed to share my Internet connection... But now I have a problem:
This totally screws up my local network so I can't access servers and printers on the LAN. How do I fix that?

GunBlad3 said...

Hi Grant, did you perform step 10? It should get ipmasq to config iptables correctly for your LAN connection too, provided that the ipmasq script is in the correct location.

Anonymous said...

GunBlade3,

Did you ever get working with WPA on jaunty..though works well with unencrypted.

been trying everything under the sun..
cannot connect 2 Jaunty 2.6.28.14 running m/cs ..

1. The n/w created is hidden.
2. Keeps asking p/w again and again.

ANY tips greatly appreciated

Sandhu

GunBlad3 said...

Hi Sandhu,

I'm afraid I haven't been able to get the settings to work reliably with WPA before either. I used that to setup temporary networks when outdoors, but never used it on a permanent basis.

ReyCazador said...

you are the BEST! Thanks to you I can use an ad hoc connection for my iPod touch. Thank you very much

GunBlad3 said...

You're welcome :)

ReyCazador said...

Hmm... for some reason it doesn't work anymore. I followed the instructions then connected using the Connect to the Hidden Network option. It tries then fails