Wednesday, January 21, 2009

Internet connection sharing on fedora core 10

Many times it happens that you are happily enjoying a broadband internet connection on your laptop and then suddenly your room-mate decides to bring a new machine for him also. So, now instead of buying one more internet connection, you can actually share the same connection by making one of your machine as a router.

I assume the machine which is connected to the internet has at least two network interfaces (e.g eth0, wlan0 etc). If the machine A has internet connection on eth0 then it can use wlan0 or may be another Ethernet interface ath0 to set up a network with machine B on the corresponding interface.

So, the steps for internet connection sharing are as follows:

Step 1: Set up a network between machine A and machine B.
Step 2: Flush the iptables rules in machine A.
Step 3: Set up IP forwarding in machine A.
Step 4: Set up the new nameserver in /etc/resolve.conf for machine B.
Step 5: Happily enjoy the connection on both machines.

This is how you can execute the above steps:

Step 1: Set up network between machine A and machine B.
I assume machine A is connected to the internet on Ethernet interface eth0 and the two machines are connected to each other through wireless interface wlan0.

So the network settings for the two connections would be something like this:

eth0:
Machine A:
IP: 192.168.1.2 Subnetmask: 255.255.255.0 Gateway: 192.168.1.1
This can be different depending upon your connection. They will be set automatically if your service provider uses DHCP. Its just the gateway which is important for us.

Now, for network on wlan0 use the following settings on two machines:

wlan0:
Machine A:
IP: 192.168.0.1 Subnetmask: 255.255.255.0 Gateway: 192.168.1.1

Machine B:
IP: 192.168.0.2 Subnetmask: 255.255.255.0 Gateway: 192.168.0.1

Please keep in mind that, the default gateway for wlan0 on machine A is the default gateway on eth0 for machine A and default gateway for wlan0 on machine B is IP of machine A on wlan0.


Step 2: Flush the old iptable rules on machine A:
Type the following commands on your terminal to flush the existing firewall rules:
iptables -F
iptables -t nat -F
iptables -t mangle -F

Now to delete the chains containing above rules type following commands in terminal:
iptables -X
iptables -t nat -X
iptables -t mangle -X

Save and restart the iptables with the following commands:
service iptables save
service iptables restart


Step 3: Set up IP forwarding in machine A.
Set value of net.ipv4.ip_forward = 1 in /etc/sysctl.conf and save the file.

Set new forwarding rule in iptables by typing following command in terminal:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Again save and restart the iptables by typing following command:
service iptables save
service iptables restart


Step 4: Setup the new nameserver in /etc/resolve.conf for machine B.

Open the /etc/resolve.conf in machine B and add following line:
nameserver 192.168.1.1

Mind it, that the nameserver added is the default gateway for eth0 connection on machine A.

Step 5: Well, If all the above steps have executed successfully for you, then don't waste your time on step 5 :).

1 comment: