Sharing an Internet Connection between two Ubuntu Computers
Internet, My activities, Networking, Ubuntu May 19th, 2008
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
- 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
- Pull out the cable connecting the two computers
- Goto Applications -> Accessories -> Terminal and enter the following command
- 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:
- 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
$ sudo ifconfiginet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0 - Next you have to give a static ip address to eth0. In terminal, type
- And add the following lines to the file and save it
- 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
- Once your network card serving the client has a static ip address, the next thing to do would be to enable port forwarding:
- Goto terminal and open the local startup script as root. To do this, type
- Add the following lines before the
exit 0line in the file. - 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'
$ sudo gedit /etc/rc.local
sysctl -w net.ipv4.ip_forward=1
/sbin/iptables -P FORWARD ACCEPT
/sbin/iptables --table nat -A POSTROUTING -o eth1 -j MASQUERADE - 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
$ sudo gedit /etc/network/interfaces
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
$ sudo ifconfig eth0 10.8.16.1
On the client
- 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.
- Use 10.8.16.2 as the ip address and set the gateway address to 10.8.16.1
- 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.
















