EBC Exercise 02 Internet Connection via the Host

From eLinux.org
Revision as of 15:16, 19 August 2013 by Yoder (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

One of the slickest features of the Bone is it's ability to access the Internet through the USB connection.

On your Linux host computer run:

host$ ifconfig
eth0     Link encap:Ethernet  HWaddr 00:18:8b:72:b8:c2  
         inet addr:137.112.41.109  Bcast:137.112.41.255  Mask:255.255.255.0
         inet6 addr: fe80::218:8bff:fe72:b8c2/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:8481193 errors:0 dropped:0 overruns:0 frame:0
         TX packets:1871287 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:3172154531 (3.1 GB)  TX bytes:203188180 (203.1 MB)
         Interrupt:19 

eth4     Link encap:Ethernet  HWaddr d4:94:a1:39:ff:ff  
         inet addr:192.168.7.1  Bcast:192.168.7.3  Mask:255.255.255.252
         inet6 addr: fe80::d694:a1ff:fe39:ffff/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:8 errors:0 dropped:0 overruns:0 frame:0
         TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:2775 (2.7 KB)  TX bytes:1234 (1.2 KB)

lo       Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING  MTU:16436  Metric:1
         RX packets:37315 errors:0 dropped:0 overruns:0 frame:0
         TX packets:37315 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:3665320 (3.6 MB)  TX bytes:3665320 (3.6 MB)

You'll see a new network has appeared, eth4 in my case. The IP address is 192.168.7.1. There's a good chance the Bone is at 192.168.7.2. Try connecting to it.

host$ ssh -X root@192.168.7.2
beagle$ 

You are now logged into the Bone through the network. This is much faster than the serial port (.115M vs. 100M) and supports many interesting network things. The only problem is, the Beagle doesn't know how to access the Internet through the host. Get back to the host computer by entering RETURN ~ ^Z. That is, hit RETURN, then ~ (it's up there near the ESC key) and then Ctrl-Z. This gets you back to your host, but leaves the ssh connection running.

Copy the following lines into a file call host.ipForward.sh

#!/bin/bash
# These are the commands to run on the host to setup IP masquerading so the Beagle
#  can access the Internet through the USB connection.
# Inspired by http://thoughtshubham.blogspot.com/2010/03/internet-over-usb-otg-on-beagleboard.html

if [ $# -eq 0 ] ; then
echo "Usage: $0 interface (such as eth0 or wlan0)"
exit 1
fi

interface=$1
hostAddr=192.168.7.1
beagleAddr=192.168.7.2
ip_forward=/proc/sys/net/ipv4/ip_forward

if [ `cat $ip_forward` == 0 ]
  then
    echo "You need to set IP forwarding. Edit /etc/sysctl.conf using:"
    echo "$ sudo gedit /etc/sysctl.conf"
    echo "and uncomment the line   \"net.ipv4.ip_forward=1\""
    echo "to enable forwarding of packets. Then run the following:"
    echo "$ sudo sysctl -p"
    exit 1
  else
    echo "IP forwarding is set on host."
fi
# Setup  IP masquerading on the host
sudo iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o $interface -j MASQUERADE

# Check to see what nameservers the host is using and copy these to the same
#  file on the Beagle
# This makes it so you can connect to the Beagle without using your password.
ssh-copy-id root@$beagleAddr
# Save the /etc/resolv.conf on the Beagle in case we mess things up.
ssh root@$beagleAddr "mv -n /etc/resolv.conf /etc/resolv.conf.orig"
# Copy the resolv.conf file to the Beagle.  Now the Beagle will use the
# same name servers as the host.
cat /etc/resolv.conf - << EOF > /tmp/resolv.conf
nameserver 137.112.18.59
nameserver 137.112.5.28
nameserver 137.112.4.196
search rose-hulman.edu
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
scp /tmp/resolv.conf root@$beagleAddr:/etc
# Tell the beagle to use the host as the gateway.
ssh root@$beagleAddr "/sbin/route add default gw $hostAddr"

Now run:

host$ chmod +x host.ipForward.sh
host$ ./host.ipForward.sh eth0

This will give you instructions on how to set up your host and will remotely set up your Beagle. Note: If you are using a wireless connection you should use host$ ./host.ipForward.sh wlan0

Once ./host.ipForward.sh has been run you can:

host$ fg
ssh -X root@192.168.7.2
(Hit RETURN)
beagle$ ping google.com

You should see Google responding. Hit Ctrl-C to stop.

Note: If you are using Ubuntu 12.04 and having an problem connecting to the internet, you may need to add a line to the /etc/resolv.conf file on the Bone. If ping google.com returns an "unknown host" error, this means that the Bone cannot resolve the url google.com to an IP address. Users with an Ubuntu 12.04 host may see this issue because the resolv.conf that Ubuntu uses (and is copied to the Bone in the host.ipForward.sh script) is not compatible with the Bone. The solution is to run the host.ipForward script as normal, then add the line "nameserver 8.8.8.8" above the existing entry in /etc/resolv.conf (run "nano /etc/resolv.conf" on the Bone to edit the file). This configures the Bone to use Google's Public DNS Server.

Congratulations! Your Beagle is now on the network through your host computer.