Sharing computer internet with HTC phones in Ubuntu

Few months ago I was looking for a way to share computer internet connection with my HTC Desire C phone on Ubuntu. I have searched a lot for this matter and tested different ways which led me to no way. By contrast of internet connection sharing from computer to phone which did not work for me easily, the opposite way which was sharing internet from phone to computer (Tethering) worked easily but in my case it was kind of useless.

Finally, what I did was, installing windows on Virtualbox and then installing HTC Sync application and share internet to my cellphone via that since HTC Sync is not available for Linux platform. It is easy way but it bugged me a lot since every time that I wanted to share internet on my phone, I had to run Virtualbox.

Now after few months again I searched the net and found the native solution for the issue which I will demonstrate in this post. All credits of below section and scripts are dedicated to Oliverthered user of Ubuntuforums.org.

For sharing computer internet to your phone, firstly you need to install Bind9 package with below command,

$ sudo apt-get install bind9

After that, you should configure it via following script,

#!/bin/bash
iptables -A POSTROUTING -t nat -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

The script is available in GitHub and you can access to it from this link.

The next step is to run the script in the command line after connecting your phone to the computer. In order to run the script, you need give needed permission to the script file and then run it like below.

$ chmod +x setup_bind9_nat.sh
$ ./setup_bind9_nat.sh

After running bind9 script, the computer internet is shared with your phone and you can surf the net. But the only problem is that, the computer does not send any acknowledgement to the phone regarding successful connection and thus, HTC does not realize that the internet is shared. As a result, the phone attempts to connect to HTC sync again and again and will give up in less five minutes which finally terminates connection. On such condition, you will loss internet connection after maximum five minutes. The solution for this issue is easy (Thanks Oliverthered) and it could be solved with emulating HTC Sync and send acknowledgement to the phone regarding successful internet sharing. For doing this you should use the following script.

#!/bin/bash
phone_usb_device="usb"
get_ip ()
{
 arp -n | grep $phone_usb_device | awk '{print $1}'
}
#TODO: This needs a timeout and loop needs cleaning up, but works fine and borrowed from another post.
echo "waiting for IP on computer usb"
while [[ `get_ip` < 192 ]];do sleep 2; done
phoneip=`get_ip`
echo "IP adress is $phoneip "
echo -n -e "\x00\x02\x00\x00" | nc -q 2 $phoneip 6000 > /dev/null

For downloading the script from GitHub please click here.

Bear in mind that you should run both scripts to have successful and stable internet sharing and in addition for that firstly, you have to run bind9 script and then signal sender script.

Finally, if you have more than one USB network card, you should change the second line of the above script base on your need.
For more information please refer to following links.
Main topic of internet sharing on Ubuntuforums.org
http://ubuntuforums.org/showthread.php?t=1620008&page=3
Bind9 man page
http://www.bind9.net/manuals

send your idea and information to kasra.madadipouya@geeksweb.eu.org

Leave a Reply