Ditch Your Cable Modem/ISP With a Tethered iPhone and Raspberry Pi

Last month, I wrote a blog post about tethering your iPhone to your Raspberry Pi. I got a lot of emails regarding this post, and one in particular caught my eye. It was from a fellow tech-enthusiast/cord cutter who wanted to save the ~60$ a month that he was paying for his cable modem, and instead tether his iPhone which already has a data plan.

Before getting started I realize this setup is not for power users, but instead for people who may

  • -travel often for work
  • -own a second residence and don’t want to pay two internet bills
  • -have tethering paid for by their employer
  • -have light internet usage or unlimited data plans
  • -cord cutters on a budget

 

Intrigued by this idea, I started testing it myself and got it working fairly easily. I already covered the full operating system setup in the Raspberry Pi/iPhone Tethering post, so I will just start with the tethering portion for this tutorial.

Prerequisites

Make sure your iPhone is disconnected and proceed to install the following iPhone and file system utilities on the Pi. Open up a terminal or SSH into your Pi and run the following commands:

sudo apt-get update
sudo apt-get install gvfs ipheth-utils
sudo apt-get install libimobiledevice-utils ifuse 
sudo apt-get install gvfs-backends gvfs-bin gvfs-fuse

After these are installed, and with your iPhone still not plugged in, lets take a look at our available interfaces. Run the following commands:

ifconfig -s

If you have a standard installation, you should see two interfaces, eth0 and lo.

tether iphone 5 raspberry pi

The reason we did this step was to make sure we correctly identified the name of the iPhone’s network interface. Now that you have this list you can turn on your iPhone’s Personal Hotspot and then plug it into your Raspberry Pi’s USB port, and issue the same command.

ifconfig -s

iphone raspberry pi

Your Pi should now recognize your iPhone is plugged in and assign its network interface a name, usually eth1.

Hot Swapping

So now we have our Raspberry Pi Recognizing our iPhone, but in the scenario we are trying to account for – we need to be able to hotswap our phone on and off our networks for it to be truly useful. To do so, unplug your iPhone and then open up your network interfaces file.

sudo nano /etc/network/interfaces

and add the following lines, where eth1 is the name identified above.

allow-hotplug eth1
iface eth1 inet dhcp

Save and close the interfaces file.

In my testing, tethering with only worked when the iPhone is mounted. To do this, we need to implement the ifuse package we downloaded earlier. First, create a mount directory for the iPhone

sudo mkdir /media/iPhone

Now we can disconnect the ethernet of the Pi, turn on the iPhone’s Hotspot , and plug in then mount the iPhone.

sudo ifuse /media/iPhone

The Internet should now be tethered from your iPhone. Remember, at this point the iPhone must manually be mounted each time it is plugged using the ifuse command.

Finishing the iPhone Portion

Having the Raspberry Pi connected to the internet via the iPhone is an important step, but it is a hassle to have to mount the iPhone everytime.

To automate the process, we need to create the following script named iphoneconnect in /lib/udev/:

sudo nano /lib/udev/iphoneconnect

#!/bin/bash
umount /media/iPhone #when the iPhone is unplugged, it is not automatically unmounted.
ifuse /media/iPhone
/etc/init.d/networking restart #this may not be necessary, but just a safe guard to rebridge the connection
ipheth_pair

Remember to make the script executable

sudo chmod 755 /lib/udev/iphoneconnect

The final step is to edit the file ’90-iphone-tether.rules’ in the ‘/lib/udev/rules.d’ directory.

sudo nano /lib/udev/rules.d/90-iphone-tether.rules

This is the script that runs automatically when ever we plug in the iPhone, and we want to edit it to call the script we created (/lib/udev/iphoneconnect) instead of what it currently calls (/lib/udev/ipheth_pair). To do this, we just edit the 5th line of the file replacing ipheth_pair with iphoneconnect.

The file should look like this


# udev rules for setting correct configuration and pairing on tethered iPhones
ATTR{idVendor}!="05ac", GOTO="ipheth_rules_end"

# Execute pairing program when appropriate
ACTION==”add”, SUBSYSTEM==”net”, ENV{ID_USB_DRIVER}==”ipheth”, SYMLINK+=”iphone”, RUN+=”iphoneconnect”

LABEL=”ipheth_rules_end”

 

Success!

iPhone Tethered Raspberry Pi

Replacing your Cable Modem with the Raspberry Pi

This is the part of this tutorial I found the most fascinating. I’m sure most of my readers already have their Wireless Router and Firewall configured exactly how they want, and don’t want to have to change any of those settings. The goal here is to just take the uplink ethernet cable from the router out of the cable modem and into the Raspberry Pi and then practice business as usual.

Bridging the Gap

In order to do this, we just need to bridge the connection from the iPhone interface (eth1) with the built in ethernet interface (eth0). To do so, we first need to install the bridge utilities,

sudo apt-get install bridge-utils

and then edit our network interfaces file again

sudo nano /etc/network/interfaces

and add the following lines

auto br0
iface br0 inet dhcp
bridge_ports eth1 eth0

Save and close the interfaces file, and reboot the rPi

sudo reboot

At this point, I logged into my Wireless Router and gave it a static IP, and used google’s public DNS. I am sure this step could be skipped if we setup a simple dhcp server on the rPi. When I have that working I will update this post with the optional step.

router ss

Results

Here is just a quick picture of my demo environment.
iphone-router-setup
I also did some speed tests and got some remarkable results. 10mb/s over Verizon’s 4G LTE. I am willing to bet that is faster than many home WiFi networks. Upload speeds were a less impressive, but still useful .25 mb/s.

downloadspeed

Troubleshooting

I noticed that because of the boot order of the network interfaces, and mounting of the iPhone, it worked best if I did a quick network reset after the device had fully booted. To do so, edit the rc.local file

sudo nano /etc/rc.local

and add the following line before the exit 0

/etc/init.d/networking restart

This will reestablish the bridge if the iPhone wasn’t tethered in time on boot.

If you have any other questions, just comment on this post.

Read More

How to Tether Your Raspberry Pi with your iPhone 5

If you have been reading my blog lately you know that I have done a lot of projects involving Raspberry Pi‘s, the $35 dollar credit card sized Linux computer. Because of its small size, I recently started of thinking of projects that would allow me to be mobile with the Pi.

That led me to this post.

There are plenty of resources online about tethering the iPhone 5 with the graphical interface, but I wanted to be able ‘hotswap’ my Raspberry Pi’s Internet connection without command line, and without the graphical interface. Plug and play, essentially.

I was able to get this working last night using Raspian (a free operating system based on Debian optimized for the Raspberry Pi hardware) and a few scripts.

Prerequisites

Make sure your iPhone is disconnected and proceed to install the following iPhone and file system utilities. Open up a terminal or SSH into your Pi and run the following commands:

sudo apt-get install gvfs ipheth-utils
sudo apt-get install libimobiledevice-utils gvfs-backends gvfs-bin gvfs-fuse

After these are installed, and with your iPhone still not plugged in, lets take a look at our available interfaces. Run the following commands:

ifconfig -s

If you have a standard installation, you should see two interfaces, eth0 and lo.

tether iphone 5 raspberry pi

The reason we did this step was to make sure we correctly identified the name of the iPhone’s network interface. Now that you have this list you can turn on your iPhone’s Personal Hotspot and then plug it into your Raspberry Pi’s USB port, and issue the same command.

ifconfig -s

iphone raspberry pi

Your Pi should now recognize your iPhone is plugged in and assign its network interface a name, usually eth1. Now we need to edit our interfaces file to allow for hotswapping and dchp.

Unplug your iPhone and then open up your network interfaces file

sudo nano /etc/network/interfaces

and add the following lines, where eth1 is the name identified above.

allow-hotplug eth1
iface eth1 inet dhcp

Save and close the interfaces file.

Graphical Interface

If you are using the graphical interface, you are now done and the above commands were all that were necessary to use your iPhone as an Internet connection!

You can test this by starting your desktop environment

startx

Then unplug your Ethernet connection, start your Personal Hotspot, and plug in your iPhone. A popup message saying a “Removable medium is inserted” , that is safe to close. Then open a terminal, and run the following command:

ping google.com

You should be connected to the Internet!

Command Line

In my testing, tethering with command line only will only work when the iPhone is mounted. To do this, we need the ifuse package:

sudo aptitude install ifuse

and create a mount directory for the iPhone

sudo mkdir /media/iPhone

Now we can disconnect the ethernet of the Pi, turn on the iPhone’s Hotspot , and plug in then mount the iPhone.

sudo ifuse /media/iPhone

The Internet should now be tethered from your iPhone. Remember, while using command prompt only, the iPhone must manually be mounted each time it is plugged in using the ifuse command.

Putting It All Together

Now that we know how to tether the phone in the graphical interface, and the command line interface, we can add a script(thanks to users djpod and benebeck on the raspberrypi.org forums) that will automatically mount the iPhone it is plugged in.

To automate the process, I put the following script named iphoneconnect in /lib/udev/:

sudo nano /lib/udev/iphoneconnect

#!/bin/bash
umount /media/iPhone #when the iPhone is unplugged, it is not automatically unmounted.
ifuse /media/iPhone
ipheth_pair

Remember to make the script executable

sudo chmod 755 /lib/udev/iphoneconnect

The final step is to edit the file ’90-iphone-tether.rules’ in the ‘/lib/udev/rules.d’ directory.

sudo nano /lib/udev/rules.d/90-iphone-tether.rules

This is the script that runs automatically when ever we plug in the iPhone, and we want to edit it to call the script we created (/lib/udev/iphoneconnect) instead of what it currently calls (/lib/udev/ipheth_pair). To do this, we just edit the 5th line of the file replacing ipheth_pair with iphoneconnect.

The file should look like this


# udev rules for setting correct configuration and pairing on tethered iPhones
ATTR{idVendor}!="05ac", GOTO="ipheth_rules_end"

# Execute pairing program when appropriate
ACTION=="add", SUBSYSTEM=="net", ENV{ID_USB_DRIVER}=="ipheth", SYMLINK+="iphone", RUN+="iphoneconnect"

LABEL="ipheth_rules_end"

Thats it! Your Raspberry Pi will now automatically Tether your iPhone without Graphical Interface and without Command line!

 

 

 

iPhone Tethered Raspberry Pi

 

My command line speed tests averaged at about 1.5mb/s download over Verizon LTE neat Boston, MA.

wget --output-document=/dev/null http://speedtest.wdc01.softlayer.com/downloads/test500.zip
Read More