Michaela Merz's personal blog site

Quickfix: Use Linux and Android as temporary router

We all have our networks and usually, everything works just fine. But: What if your cable or DSL goes down? You could simply continue to use the net on your mobile device, or connect your mobile device to one of your computers. But – if you have a Linux laptop, you can also use your Android smart phone to provide connectivity to all of your systems.

Here’s the situation: We need the Internet not just to surf the net, but also for work. We need to access our clouds, check for software updates and so on. Whenever we lose connectivity, work stops. But there’s a simple fix.

What you need:

You need a Linux box (I am using my laptop) and an Android phone.

Here’s what you do:

Connect your Android device to your laptop. I am using USB because I want to continue to provide access through WLAN.  Find the route your USB device is using (you’ll need the gateway address for the USB port). Use netstat to get the data you need. You will see something like this:

#netstat -nr

blognet

This pretty much tells you, that all the traffic flows to the gateway at 192.168.1.1 using eth0 which is your default (normal) interface . That would be ok – but our default gateway is down, so we have to do a few changes.

First you need to stop the network manager as it will interfere with the settings we are now changing. Don’t worry, nothing is permanent and will be reset to your defaults after reboot. On Fedora, it goes like this:

#systemctl stop NetworkManager.service

Now find the ip-address of your linux box by quering for the default interface:

#ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 40:16:7E:B1:68:4B  
          inet addr:192.168.1.50  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::4216:7eff:feb1:684b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:128732093 errors:0 dropped:0 overruns:0 frame:0
          TX packets:74880594 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:161442436147 (150.3 GiB)  TX bytes:6842292828 (6.3 GiB)

Look for the ‘inet addr’. This will become the address all the other systems will have to connect to.

Now set IP-forwarding allowing your temporary router to forward data between its network interfaces.

#echo '1' > /proc/sys/net/ipv4/ip_forward

Almost done. We now have to tell the Linux box to use a different default route. To do this, we first delete the old route and add the usb  interface connecting your smart phone as the new default route.

#route delete default
#route add default gw 192.168.42.129

So .. we are now using the new default gateway that directs all network traffic not belonging to our own network to flow via USB to the Android device. Since we enabled IP forwarding, data may flow freely between the eth0 and the usb0 interface. There’s one last step we need to take care of. Since all your computers are using this gateway now, we have to tell the Linux box to remember which computer connected where. This is called ‘masquerading’ and can be enabled with two simple steps:

Flush your firewall, we need to forward all traffic (you may want to set a few specific rules later)

#iptables -F
#iptables --table nat --append POSTROUTING --out-interface usb0 -j MASQUERADE
#iptables --append FORWARD -in-interface eth0 -j ACCEPT

That’s pretty much it. Go to the systems you want to use with your new environment and change the default route to the address of your temporary Linux box router. Don’t forget to check the name server setting as well.

You may also use this as a boiler plate for a more permanent solution. I just postet this, because my default net went down and I implemented this quick fix for the work to continue in the office.

Have fun.

Leave a Reply

Your email address will not be published. Required fields are marked *