If you follow my blog regularly, you would have known that my father has brought a new computer running Ubuntu, and that I have completed my BE degree course and have brought my computer home from college.

At home, we have a broadband Internet connection that we share between the two computers. Here’s how I setup my father’s computer as a gateway for my computer.

Both computers had an inbuilt ethernet port and we bought an extra ethernet card for my father’s computer and use it as an internet gateway. Sharing internet on two windows computers is a very straight forward procedure. You just have to connect the broadband modem to one of the ethernet ports and use additional ethernet ports to connect other computers to the gateway.

In Linux however, this doesn’t work automatically. Port forwarding is disabled by default in Ubuntu. This has to be enabled if the computer should forward packets it recieves to other computers. Here’s how this is done.

On the computer you want to use a gateway

  1. You need to connect the internet cable to eth1 and the client computer to eth0. Since the computer has two network cards, you have to figure out which one is connected to the internet and which one to the other computer. Here’s an easy way to do this
    1. Pull out the cable connecting the two computers
    2. Goto Applications -> Accessories -> Terminal and enter the following command
    3. $ sudo ifconfig

    4. You will be shown 2 or more sections with eth0 and eth1 being the first two. In the two sections, one of them will have a line similar to this:
    5. inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0

    6. The section that has this line is the one connected to the internet. If this is in eth0, remove the internet cable and plug it in the other ethernet port and the cable from the client to eth0. If it is in eth1, you can connect the client cable back to its original port
  2. Next you have to give a static ip address to eth0. In terminal, type
  3. $ sudo gedit /etc/network/interfaces

  4. And add the following lines to the file and save it
  5. auto eth0
    iface eth0 inet static
    address 10.8.16.1
    netmask 255.255.255.0
    broadcast 10.8.16.0
    network 10.8.16.0

  6. This will assign the static ip address 10.8.16.1 to eth0, but these changes take effect only after a reboot. To use these settings temporarily for the current session, use the command
  7. $ sudo ifconfig eth0 10.8.16.1

  8. Once your network card serving the client has a static ip address, the next thing to do would be to enable port forwarding:
    1. Goto terminal and open the local startup script as root. To do this, type
    2. $ sudo gedit /etc/rc.local

    3. Add the following lines before the exit 0 line in the file.
    4. sysctl -w net.ipv4.ip_forward=1
      /sbin/iptables -P FORWARD ACCEPT
      /sbin/iptables --table nat -A POSTROUTING -o eth1 -j MASQUERADE

    5. Before closing the script, make sure it is enabled. See the first line of the script. If it isn’t present already, just add a '-e' to the first line so that it would look like '#!/bin/sh -e'
  9. That’s all you have to do on the server. Just do a restart for the settings to take effect. If that’s not possible, you have to execute all the commands you have added to the rc.local script one by one for all of them to take effect

On the client

  1. Go to System -> Administration -> Network There should be a wired connection. Select it and click ‘properties’. Choose ‘Static IP Address’ in the Configuration list box.
  2. Use 10.8.16.2 as the ip address and set the gateway address to 10.8.16.1
  3. Network manager should give you a subnet mask automatically. You won’t have to change that.

This should be enough. This is what I did. If this doesn’t seem to work, just let me know and I’ll help you out.

P.S. You can use a Windows computer to connect to Internet through the linux gateway too. Nothing needs to be changed on the server. Just make 10.8.16.2 the IP address of the client in LAN Settings which can be found in Network Connections. Also, set 10.8.16.1 as the default network gateway address.

del.icio.us Digg DZone IndianPad Newsgator reddit Simpy SlashDot StumbleUpon Technorati


3 Comments to “Sharing an Internet Connection between two Ubuntu Computers”

  1. Colin | June 10th, 2008 at 4:43 am

    I have been trying to set up a gateway in Linspire 6.0. I tried to use the Firestarter wizard but it will not work. I thought your instructions would be the answer, but sudo gedit /etc/network/interfaces got me ‘command not found’. The mother board has an ethernet that is working fine. I am connected by a cable modem. If I connect to the Linksys network card I cannot connect to the internet.

  2. sahas | June 19th, 2008 at 1:08 pm

    This post was written primarily for ubuntu computers. gedit is available on the GNOME desktop environment. in your case, you should use kate in the place of gedit, which is the graphic text editor for KDE.

    vim can be used if you know how to use it.

  3. capi etheriel | July 20th, 2008 at 12:48 am

    clientside didn’t work. using gutsy (ubuntu 7.10) on the client, hardy (ubuntu 8.04 ) on the server.
    it just doesn’t connect to the server…

Leave a Comment