Monday, September 26, 2011

Linux: Sharing Internet - The Easy Way

This tutorial will show you how do I share internet belong to a computer with another computer. The method will use no tools other than terminal and just two commands. Read more to learn how do I do it.




Take the diagram below as example.

  • Computer A has the internet connection via either wireless lan or wireless broadband(either wlan0 or ppp0)
  • Computer B and C will connect to computer A to use the internet connection(belong to computer A)
  • Computer A got 2 ethernet port named eth0 and eth1 both connected to computer B and C respectively
To do it, make sure you have connected to internet(PC A). Enter as root by running command "sudo -s". Run command "gedit /etc/resolv.conf" and take note the DNS server list
  1. Justify interface name that has internet connection at Computer A. Run command "ifconfig" (ignore the quote). In this case, I will assume interface name ppp0(mobile broadband) that has an access to the internet.
  2. Run the command "iptables -t nat -A POSTROUTING -o XXXX -j MASQUERADE" (replace xxxx with the interface name you obtained earlier..in this case ppp0)
  3. Next, run the the command "echo 1 > /proc/sys/net/ipv4/ip_forward"
  4. Connect PC A and B with ethernet cable. Open "edit connection". Set/edit wired connection like below. If none available to edit, create new.
    PC A
    IP Address: 192.168.1.100
    Netmask: 24
    Gateway: 0.0.0.0
    DNS: Enter the DNS server you obtained earlier.

    PC B
    IP Address: 192.168.1.101
    Netmask: 24
    Gateway: 192.168.1.100(point back to PC A)
    DNS: Enter the DNS server you obtained earlier.
    Edit the eth0 or Create new
  5. Click apply. Disconnect wired connection and and reconnect. Make sure both PC use the specified address. If not prevail, try again.
  6. Try access internet from both computer.
NOTE
  • Repeat step 4 to setup PC A and PC C.
  • You also can use subnetting. Just enter appropriate subnet mask and ip address
  • If you want to connect between PC with wireless also no problem. Just set proper value in "wireless" tab in "edit connection" setting..

No comments:

Post a Comment

Your comment is much appreciated. Thanks :)