Sharing Internet Connection in Ubuntu

Sponsored Link
Setting up a computer to share its internet connection should be easy.After all, you've successfully networked your computers together and even shared files with all your home computers, so why not the Internet? Well if you have a small home network of computers all connected and have tried to open up a browser, you've probably found out that things aren't quite as straight forward as connecting one machine with an Ethernet cable to the computer that has the DSL/cable modem.

The secret is that everything comes down to having a gateway.

A gateway is a computer or device than can route data between different networks. Put it this way, if your computers are on one network and every other computer connected to the Internet is on a different one, to pass information across network boundaries you need a device which is connected to both networks and equipped to decide whether information from one network should go out to the other network, and vice versa.

So, without a gateway there is no way for the other computers in your network to receive or pass information to the other networks, and consequently, they have no Internet access.

The computer in your small network which is directly connected to the cable or DSL modem is provided with a default gateway by the Internet service provider. That allows the computer to connect to the interenet, but unfortunately for us that gateway is not part of your home network -- rather it is a separate network formed between the computer connected to the cable/DSL modem and the ISP itself.

In order to enable Internet access on the rest of your small home network, the computer or device that connects with your ISP must become a gateway for the computers in your local home network. Once this has been configured, it will enable all of the other home network computers to access the Internet by sending and receiving information through that gateway.

The easiest way of acquiring a gateway for your home network is to buy a cable/DSL router. Given how cheap they currently are (about a quarter of the price they commanded when they first appeared in the marketplace), there really isn't much of an excuse not to buy one.

The major advantages of having a hardware router doing your internet sharing include minimal configuration, since they are set up to act as a gateway by default (it's pretty much all they do), increased reliability (they are not a PC… PCs crash.), and security, since most of these devices incorporate a firewall. Highly recommended!

You'll also need

a working dial-up Internet account with an Internet Service Provider (ISP) or

a working broadband (cable, DSL, satellite, etc.) Internet connection.

Share your Internet connection using the following Procedure

Note: Type all the following commands in a root terminal

Start by configuring the network card that interfaces to the other computers on you network

# ifconfig ethX ip

where ethX is the network card and ip is your desired server ip address (Usually is used)

Then configure the NAT as follows

# iptables -t nat -A POSTROUTING -o ethX -j MASQUERADE

where ethX is the network card that the Internet is coming from

# echo 1 > /proc/sys/net/ipv4/ip_forward

Install dnsmasq and ipmasq using the following command

# apt-get install dnsmasq ipmasq

Restart dnsmasq using the following command

# /etc/init.d/dnsmasq restart

Reconfigure ipmasq to start after networking has been started

# dpkg-reconfigure ipmasq

Start by configuring the network card that interfaces to the other computers on you network

# ifconfig ethX ip

where ethX is the network card and ip is your desired server ip address (Usually is used)

Then configure the NAT as follows

# iptables -t nat -A POSTROUTING -o ethX -j MASQUERADE

where ethX is the network card that the Internet is coming from

# echo 1 > /proc/sys/net/ipv4/ip_forward

Add the line "net.ipv4.ip_forward = 1" to /etc/sysctl.conf

# gedit /etc/sysctl.conf

Reboot your system is optional.


You may also like...

41 Responses

  1. Dim says:

    And why do not simply apt-get install ‘firestarter’ which is already doing all this staff for you just in few clicks?…


  2. sinoj says:

    I just tried those steps and now I can’t even browse from Ubuntu.
    any idea? I’m new to Ubuntu. please help.

  3. Allison says:


    I tried those steps above, but my second computer still doesn’t connect to the Internet.
    Ubuntu 7.04 is installed in both pcs.

    Another thing: firestarter can’t help here too.

  4. Esmeal says:

    it doesn’t work apt-get install it says “E: Coudn;t find package dnsmasq” any idea please

  5. a says:

    dnsmasq is in universe-package.

    sudo nano /etc/apt/sources.lst
    Remove # from line which contains universe.
    sudo apt-get update

  6. ptra8 says:

    can anyone tell me step-by-step to configure internet sharing on ubuntu..
    i cant understand.. i’m new use ubuntu.. plezzz

  7. Fabiano Duarte says:

    It doesnt work. I dont know if the problem is the version of Ubuntu i have or ???? …. Cause i didnt find the packages dnsmasq and ipmasq at Synaptic.

  8. karunadheera says:

    I have tried this. I also found i could not even browse internet after that!. Then rolled back several changes and got rid of that.

  9. Bob says:

    #7: See #5. You can also do this point-and-click by enabling universe in ‘software sources.’

  10. kaus says:

    I am new to ubuntu. Please help me with more details about connecting only two computers one having Ubuntu and another xp

  11. Motin says:

    As previously mentioned this method is far to complicated. Easier way:

    Install “Firestarter” using Applications -> Add/remove.
    Then run Applications -> Internet -> Firestarter and follow the configuration guide (about 3-4 questions about how you connect to the internet and LAN)

    If you do not enable the DHCP-option* (automatic network configuration) you will have to manually set the ips of the other machines so that they begin with the same three groups of numbers (eg 192.168.0.x) as the internet-connected machine has on the LAN. Then, set the gateway ip of the other machines to the ip of the internet-connected machine.

    * Do not do this if your network already has a DHCP server (ie via an existing router) – two DHCP servers on the same network that gives out different settings is a bad idea…

  12. bob says:

    Motin’s comment should be added to this post as a simpler alternative.

    Good job Motin

  13. feistytom says:

    WOW! this tutorial is the best!!!

    after a couple of tries, i have picture-perfect internet sharing,

    thanx a ton!

  14. gocho says:

    It works… Just needed to change DNS 🙂

  15. Tuoni says:

    You can’t use the firestarter stuff unless you’re working in a graphic environment, the tutorial only requires command line access.

    Very usefull stuff, but the comment about the universe packages should be added to the tutorial, to many people would miss that else (I did to at first)
    But when adding those packages, it worked like a charm, thanks a lot!

  16. simon says:

    So I have a linksys router right now but it is kind of a piece of garbage and I was wondering if I got a wireless card and put it in my computer and did this would it then work as a wireless router?
    Like the internet would come directly into my computer and then it would serve up to different laptops in my house wirelessly. Am I right? And would this be better than my linksys router?

  17. Arup Roy Chowdhury says:

    How about using the excellent Firestarter, it has a wonderful interface to setup the firewall and an easy way to enable ICS.

  18. Moustafa says:

    hi all im a new user for lunix systems i want to learn how to creat a palt form dialer for internet connexion on ubuntu

  19. Vance says:

    Hi, and thanks for the post. I like the way you’ve laid it out better than other places I’ve seen this. Question, I’ve successfully deployed this technique between my Ubuntu Box and my Mac, however, upon rebooting either machine, I lose connectivity. I’m pretty sure that the problem lies with my client/Mac, but re-performing these steps restores connectivity.

    I guess my question for you is: How do I make this a permanent set-up? Also, for the people asking about this up the thread, I found a more comprehensive step-by-step within the Ubuntu documentation. This is the resource I used.

    Ubuntu documentation on ICS (Internet Connection Sharing)

    Ubuntu forum thread on ICS (Internet Connection Sharing)

  20. Davion says:

    hi um this is kinda a weird question
    I’ve just installed hardy and i plugged my ethernet cable in and internet was instantaneously there no need to set it up or any thing does the ADSL router i use already contain the login info and IP address or am i some how unknowinly stealing it

  21. Fred says:

    I’m using XP SP2 dualboot with Intrepid Ibex. I can go online w/ my ubuntu but not on XP. Before I upgrade to Intrepid I had Hardy installed boot OS can go online. Is there any settings on my Intrepid that is blocking my XP not to access the internet? Please help.

  22. Panic says:

    A big thanks, that was helpful!!!
    It works now!!!

    I am also new to ubuntu but now I am sharing my internet from ubuntu to windows vista!
    After i have followed these steps i tried from Vista to get automatically ip (DHCP) but did not work so i set the ip manually, for instance It just took me a minute!!!

    Thanks again!!!

  23. Majed Jarrar says:

    erm… hi, I just wanna know how to set up my network, I have a USB Modem connected to my host, and the host is connected (with wire and sometimes i have to unplug and connect wirelessly) to a router that broadcasts the network.

    i have no problem setting up the network when the router is connected by lan cable [just set up the IPv4 numbers manually, choose my host IP to be same as gateway and same as DNS, while the USB modem has its own external DNS servers to get the internet], but i’m trying to find out how to do it if the host was connected wirelessly to the router, it doesn’t seem to work the normal way.


  24. winrid says:

    I have followed these instructions and the Ubuntu one’s and have tried firestarter. I am trying to do the network as follows:

    ISP >> router >>> ||| ETH0 >>> Ubuntu 9.04 >>> ETH1 ||| >>> Windows 2000 machine.

    The windows 2000 machine will not detect any internet connection. I have made my own crossover cable and I think it is working correctly because both network adapters’ lights are lit and blink occasionally. What could be wrong? I can’t find the tcp/ip settings in win2000. I don’t know much about Windows… It’s the most confusing operating system I’ve used!

  25. Ross Perkins says: say the following code twice:

    Start by configuring the network card that interfaces to the other computers on you network

    # ifconfig ethX ip

    where ethX is the network card and ip is your desired server ip address (Usually is used)

    Then configure the NAT as follows

    # iptables -t nat -A POSTROUTING -o ethX -j MASQUERADE

    where ethX is the network card that the Internet is coming from

    # echo 1 > /proc/sys/net/ipv4/ip_forward

  26. danny says:

    Hi, this is for setting up the gateway right. Can someone help in configuring the client?

  27. Poster says:

    I did this and now I can’t browse the internet on my linux computer. How do I undo this?

  28. Nico says:

    I have the same problem.
    Can’t browse anymore on my computer.
    Thanks for helping reverting all these changes.

  29. Erock says:

    I have installed Ubuntu before on this PC w/o even needing to configure my wireless connection but now I can’t get it to connect except for sometimes it will connect for 20 seconds or so. Any ideas what I need to change. I’ve tried configuring a static IP like I have to do on all my crummy windows machines but even that didn’t work. I can’t ping anything. What is the deal?

  30. Walter says:

    try to remove (completely) dnsmasq and ipmasq
    — that worked for me (debian etch)

Leave a Reply

Your email address will not be published.