
Hi all,
Back with another Blog, and a solution every Linux newbie faces, yes how to share Internet connection through ubuntu or any linux machine (if you are using your machine as a gateway for rest of the machines)
Scenario for this is, you are having a Linux machine (here its Ubuntu as I am posting this blog under Ubuntu section) with two Ethernet cards (or you can say LAN-cards or NIC's), eth0 is from where internet connection is coming from and eth1 is connected to your LAN or to another PC at your Home-Network. Simple scenario isn't it?
Ok lets start with the solution, here trick is simple, you just supposed to make "nat" (hope so you are aware of natting, if not don't worry I will publish another post describing NAT)
Firstly configure your eth0 (means eth with internet connection)
IP add : 192.168.1.x ('x' means any IP address you want to use for your machine)
Subnet Mask : 255.255.255.0 (as its a class-C IP address)
Default Gateway : 192.168.1.1 (normally it is but if you are using any different then put that in place of and remember its your routers IP which you are using for internet connection)
Primary DNS : xxx.xxx.xxx.xxx (Put DNS provided by your ISP or you can use any open DNS like 4.2.2.2 or 4.2.2.1 and there are lots of)
now check whether you are able to ping to your default gateway and your DNS by opening your terminal from Application --> Accessories --> Terminal and by typing
$ ping 192.168.1.1
$ ping 4.2.2.2
Now its time to configure the connection between your machine and the another machine with which you are willing to share your internet connection
for eth1(on your machine only):
IP address : 192.168.2.1 (or any IP you want to use instead)
Subnet Mask : 255.255.255.0
Default gateway : (leave this blank as there is no need to assign it)
Primary DNS : (put DNS provided by ISP)
configure the 2nd machine accordingly and put Default gateway as IP of your 1st machine (means your own machine through which you want to share the internet connection), for example -
other machine:
IP address : 192.168.2.2
Subnet Mask : 255.255.255.0
Default gateway : 192.168.2.1
IP address : 192.168.2.2
Subnet Mask : 255.255.255.0
Default gateway : 192.168.2.1
Primary DNS : (DNS provided by ISP)
this is all done with configuration, now the last step is required to complete the magic..
on 1st machine (your own machine which is going to act like a server now) add iptables rule as
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
and thats it..
now you can check on 2nd computer that internet is working.
But one thing is to remember here that, the changes made in iptables till this point are temporary only, once you reboot the system all the changes you have made to iptables will be lost, next time again you have to make changes to the iptables by firing above mentioned command of NAT.
To solve this issue do the following so that changes to iptables remains unchanged after reboot
$ sudo iptables-save > /etc/iptables.up.rules
then edit /etc/network/interfaces by using your favorite editor
$ sudo vim /etc/network/interfaces
and add the line highlighted in red color, add this line just below the loopback entry
# The loopback network interface
auto lo
iface lo inet loopback
pre-up iptables-restore < /etc/iptables.up.rules
# The primary network interface
auto eth0
#iface eth0 inet dhcp
And its all done.. now the changes remains as it is after rebooting of machine..
now share and enjoy internet connection over your LAN or over your home-network.
Hope this post will help you.. if any problem persist let me know.
Thanks..!!!
Hi great work
ReplyDelete