Wednesday, 11 September 2013

how-to-share-internet-connection-through-a-cable

Now we need to configure one of the machines as a router, the one with the 3g usb dongle.

- edit /etc/sysctl.conf, set 'net.ipv4.ip_forward = 1'
- enable ip forwarding, 'sysctl -p'
- enable NAT via iptables, 'iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE'


(I'm guessing that your Internet connection is ppp0, replace if incorrect)
- save iptables settings, 'service iptables save'
- in Network Manager ensure the Internet connection is set to connect automatically and is "Available to all users" (this means you won't have to log on to this machine to make the connection active)
- Make a note of the dns settings your router pc picks up when it's connected to the Internet, try looking in /etc/resolv.conf.

On the second (client) machine:
- set the default gateway to be the machine above
- set the dns settings in /etc/resolv.conf to those you noted down above.

Setup SSH to run on startup

        Secured Shell Daemon            
by default SSH is not configured to run on startup. This means you will have to activate the service every time before being able to use SSH on the Fedora/CentOS/RHEL server.

To enable this to come back up after reboot run the following command.
                $chkconfig sshd on                   
This can be applied to any service you want to run on boot. Once this is done restart the server and SSH will be started

Internet Connection Sharing via Router

If you want to share this internet connection to the rest of the family since you have a huge quota every month (my case I am having 5GB), the rest of the family is connecting their network thru a wireless router, so following the steps below:

Follow these steps carefully:
  1. Make sure your 3G connection is connected and running well with your PC.
  2. Make sure your PC either connected to home router via network cable or wireless, in my case I prefer the later because some reason network cable will set as default resulted the internet connection won't work in local surfing.
  3. Make sure your router wireless and admin access are protected with at least WPA2 AES password to prevent unauthorized access especially your neighbor.
  4. Now, to setup your local PC, you need to have dnsmasq package, if not already installed as mentioned in my previous article, do so now by $ sudo yum install dnsmasq
  5. Find out the network interface that your 3G connection and wired/wireless connection is using, for my case 3G is using usb0 and wireless connection is wlan0. You can check this by right click on the Network Manager icon from the system tray and select Connection Information.
  6. Now, you need to configure Fedora to enable both IP masquerading and IP forwarding. To enable, run the following command in the terminal as root:

    # iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE   

    # echo 1 > /proc/sys/net/ipv4/ip_forward                                         
  7. Start up your firewall editor, in my case i am using system-config-firewall, configure usb0 to allow Masquerading interface.
    Add usb0 interface and check it. Apply the rules.
  8. Now, using another PC and connecting to the router. You need to change the gateway/router connection IP address to the IP address shared above.
  9. Also change the DNS address to either a public DNS like Google DNS or others.
  10. Launch a browser and it should works.
If you can't get it working, comment below.