Sharing internet connection from a linux machine over Ethernet

Tarun Chinmai
2 min readDec 25, 2016

--

I have a PC at home, which connects to the internet using ethernet. However, due to a storm, my ISP was down and I wasn’t getting broadband. However, I still had access to the internet through mobile data, and was using a hotspot to access internet from my laptops.

The PC doesn’t have a WiFi card, and hence wasn’t able to connect to the internet. So, I decided to share my laptop’s internet with the PC. The laptop runs linux, and the PC runs Windows.

The setup looks like this :

On the laptop, I did the following.

  1. Enable IP Forwarding sysctl -w net.ipv4.ip_forward=1. This will enable the kernel to forward packets, which are arriving to this machine.
  2. Assign a static IP to the ethernet interface, in my case eth1. sudo ifconfig eth1 192.168.122.10 netmask 255.255.255.0 up Just make sure that no other device on your network uses this ip. On the safer side, assign the ip in a totally different subnet.
  3. Enable masquerading on the interface which is connected to the internet. In my case, its my WiFi interface, eth0. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE. This will masquerade (replace the src ip on the packet with the eth0 ip) all traffic arriving from other interfaces, to the eth0 interface.
  4. Add iptable rules to ACCEPT and FORWARD traffic from the subnet
sudo iptables -I FORWARD -o eth0 -s 192.168.0.0/16 -j ACCEPT
sudo iptables -I INPUT -s 192.168.0.0/16 -j ACCEPT

These rules will redirect the kernel to accept packets coming from the 192.168.0.0 subnet, and forward them onto the eth0 interface.

On the windows machine, do the following

  1. Goto Network and sharing center → Ethernet → Properties → ipv4 Properties
  2. Assign an ip in the same subnet as the ethernet interface of the laptop
  3. Set the laptop’s ip as the default gateway
  4. Assign DNS servers (8.8.8.8 , 8.8.4.4 for Google DNS)

Now the PC has internet access, through the laptop.

--

--