I’ve been using Ubuntu’s built in functions to access the Internet from my netbook via my iPhone for quite a while now, but I have never been completely satisfied with the reliability, when connecting/disconnecting to the iPhone.
I take the train to work just about every day, and usually I would have to do some tinkering with both my iPhone and my netbook, every time I wanted to connect to the Internet.
Firstly, I would disable bluetooth on the Iphone, then enable it again.
Then, from the iPhone, establish a connection to my laptop.
If at this point, my laptop was still unable to connect through my iPhone, using Ubuntu’s built in bluetooth-manager, I would have to either pair the two devices once again or even have to restart my iPhone and my netbook, to get things working properly again.
After some time of noticing odd behaviour of Ubuntu’s built in bluetooth-manger, I decided to look into some of the underlying commands in Ubuntu that handes bluetooth connections, to fashion a more robust procedure to make an Internet Tethering connection to my iPhone.
How to use iPhone Tethering in Ubuntu from the Command Line
I’ve made a script that I now use to connect to the Internet through my iPhone. I will walk you through the script, to explain what you definitely need, and what you might want to leave out, depending on your system set up.
You can download a copy of the script here: iPhone_Tethering.sh.zip
The script supports 3 different arguments, when executed. They are “start”, “stop” and “install”.
The “install” routine, simply makes sure that the packages “bluez” and “bluez-utils” are installed. I won’t go through this part of the script, as it is relatively self-explanatory.
Lets start with what you need to do, before you execute the actual script.
Before you can use the script, there’s a few things that must be done manually.
The very first thing you must do, is to pair your iPhone with your Ubuntu installation. This part should be straight forward, and you can do it using your iPhone or with Ubuntu’s graphical bluetooth-manager. Note that I made the initial pairing from my iPhone to my netbook.
Make sure that the iPhone can connect to your Ubuntu installation, by going to “Settings” -> “Genereal” -> “Bluetooth”, and tap the name of your computer. If you are able to connect, you should be well on your way.
You also have to ensure that “Internet Tethering” is turned on, on your iPhone. This is done by going to “Settings” -> “General” -> “Network” – > “Internet Tethering”. If you can’t enable Internet Tethering, it might be because your iPhone is not set up to support Internet Tethering. In this case, you should contact your provider to find out how to enable it.
You will also need super-user privileges for a lot of the commands executed in the script, so make sure you have them.
Starting Internet Tethering
Lets begin with what happens when you execute the script, with either no argument, or if you choose “start” as the first argument.
The first thing it does, is stopping Ubuntu’s “network-manager” daemon. I do this, simply because I want to make sure that my netbook only makes use of my iPhone Tethering for Internet connections, and that the network-manager doesn’t tinker with my DNS-settings in “/etc/resolv.conf”. Please be aware that you will most likely disable your other network connections when you execute this command. If you want to use iPhone Tethering along side any other network connections on your computer, you might want to comment out this line.
sudo service network-manager stop
Now for the par that really matters. This next command will try to make an active bluetooth connection to your iPhone. Remember that you need to have your iPhone paired with Ubuntu. You also need to replace the capital letters, with the actual MAC-address of your iPhone’s bluetooth-device. This address can easily be found by going to “Settings” -> “General” -> “About” on your iPhone. Scroll down to find the section named “Bluetooth”.
hidd –connect AA:BB:CC:DD:EE:FF
Next up is the actual Internet connection, through your iPhone. This command creates a virtual network interface, which in my case is called “bnep0”. Again, remember to replace the capital letters with your iPhones bluetooth MAC-address.
sudo pand –connect AA:BB:CC:DD:EE:FF -n
Once the virtual network interface “bnep0” is created, we still need an actual IP-address on the interface, to be able to access the Internet. This is done, simply by asking for a DHCP IP-address on the “bnep0” interface.
sudo dhclient bnep0
Lastly, I use OpenDNS as my DNS-server of choice, to be able to resolve Internet addresses. You should note that these two commands will overwrite any lines you might have manually written inside “/etc/resolv.conf”. I found that this was no problem for me, since Ubuntu’s network-manager automatically updates this file, whenever I reconnect to a network that delivers an IP-address with DHCP.
sudo sh -c “echo ‘nameserver 220.127.116.11’ > /etc/resolv.conf”
sudo sh -c “echo ‘nameserver 18.104.22.168’ >> /etc/resolv.conf”
You should now be able to access the Internet through your iPhone.
If you experience connection problems, you should check to make sure that the DNS-settings in “/etc/resolv.conf” are correct, and that the virtual interface “bnep0” actually has an IP-address. In my case, the iPhone gives me the IP-address “192.168.20.2”.
Stopping Internet Tethering
Using the “stop” argument for the script, naturally stops Internet Tethering and re-enables the network-manger.
The first line in the “stop” routine, breaks the Tethering connection to the iPhone and also drops the initial bluetooth connection, made by the “hidd” command used in the “start” routine.
sudo pand -k AA:BB:CC:DD:EE:FF
The last line, re-enables Ubuntu’s network-manager.
sudo service network-manager start
You should now be able to connect to your WiFi hotspots and Ethernet networks again.