GUIDE: Connect to internet through Ubuntu 12.04


3 posts
by RobJN » Wed May 30, 2012 5:29 pm
I spent quite a bit of time attempting this so now that I have it working, I thought I would share for anyone else.

1. The Setup
My router is hidden away and not near a screen. Connecting my RPi direct to the router is not convenient. I would still like network (and internet access occasionally) so have set my laptop up to share it's internet connection via the LAN network (A standard CAT5 cable, no need for crossover cable as the Pi can deal with this):

Ubuntu 12.04 laptop <==== Network cable =====> RPi

2. Setup the Raspberry Pi
The first stage is to give the RPi a static IP address. Do this by following the steps on the video to change your interfaces file:

http://www.youtube.com/watch?v=kcqQwBpB3Rw&feature=plcp
Code: Select all
cd /etc/network
sudo nano interfaces
Edit the file by replacing "dhcp" with "static" and adding the following lines to the end. Then Save and quit with 'Ctrl+x', then 'y'.
Code: Select all
       address 10.50.0.10
       netmask 255.255.255.0
       network 10.50.0.0
       broadcast 10.50.0.255
       gateway 10.50.0.1
We finally set up DNS nameservers. Here I am using OpenDNS's servers:
Code: Select all
sudo nano /etc/resolv.conf
Change the file to include the 2 nameservers (save and quit - Ctrl+x,y):
Code: Select all
nameserver 208.67.222.222
nameserver 208.67.220.220
3. Setup the Ubuntu 12.04 laptop
From the drop down network menu next to the clock, click "Edit Connections..." and edit the details on the "IPv4 Settings" tab of your RPi wired network to:

* Method = Manual
* Address = 10.50.0.1
* Netmask = 255.255.255.0
* Gateway = 0.0.0.0
* DNS servers = 208.67.222.222, 208.67.220.220

Click "Routes..." and tick the box "Use this connection only for resources on its network".

Finally open a terminal window in Ubuntu and type the following 3 lines*:
Code: Select all
sudo iptables --table nat --append POSTROUTING --out-interface wlan0 -j MASQUERADE
sudo iptables --append FORWARD --in-interface eth0 -j ACCEPT
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Where wlan0 is Ubuntu's connection to the internet, and eth0 is Ubuntu's to the RPi. Replace as appropriate with the correct network (hint: use the command "ifconfig" on the Ubuntu machine).

* : I believe you can get away with just the 1st and 3rd but hopefully someone will confirm.
Note: These last 3 lines will need executing after each reboot of your Ubuntu machine. To avoid this hassle you can add the lines to you /etc/rc.d/rc.local file (see http://www.linuxsolved.com/forums/index.php?topic=115.0)

Cheers,
Rob

p.s. you may need to disable the firewall in Ubuntu to allow this (or set up forwarding) - unverified.
Posts: 2
Joined: Mon May 21, 2012 11:45 am
by markb » Fri Jun 08, 2012 8:57 am
Thanks.
User avatar
Posts: 124
Joined: Thu Nov 17, 2011 8:09 am
by nacnud » Thu Sep 13, 2012 5:43 pm
Right,

It turned out like this in the end. An end I should say, that I'm not 100% satisfied with; given that Ubuntu has been tinkering under the hood and out of sight. Maybe there is a way to drill into what it got up to; but I don't know it.

Anyway, for those not totally bored yet:

Interfaces File ( Ubuntu Laptop )
Code: Select all
auto lo
iface lo inet loopback
#The WireLess Connection
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.254


Interfaces File ( Pi )
Code: Select all
auto lo
iface lo inet loopback
iface eth0 inet static
address 10.42.0.2
netmask 255.255.255.0
gateway 10.42.0.1



You'll notice the weird IP addresses on the Ethernet port on the lappy and the Ethernet port on the Pi.

The reason for these values, is because that's what Ubuntu said they should be. I went into
Edit Connections->Wired->Select Wired Connection->Edit->ipV4 Settings-> and make sure the following is set
METHOD : Shared to Other Computers.


Everything else is cleared or not checked.


Next, configure IPtables rules to allow for MASQUERADING and FORWARDING. Out on Wireless ( eth0 ) and in on eth2 ( from Pi )


Code: Select all
iptables --table nat --append POSTROUTING --out-interface eth1 -j MASQUERADE
iptables --append FORWARD --in-interface eth2 -j ACCEPT
# Just turn on the fact that we are doing IPV4 forwarding.
echo 1 > /proc/sys/net/ipv4/ip_forward


Note, it's best to execute these from a "sudo -s" session rather than sudo before each command. I've no idea why; must be something to do with privileges / shell variables. Anyway, the advice on the net is do it that way.

And that's how I got this pass through kind of thing working. I have *no* idea what Ubuntu did when I selected "Shared To Other Computers" but with a wee bit of hunting it is possible to see a difference in the IP command output. Thus:

ip route
default via 192.168.1.254 dev eth1 proto static
10.42.0.0/24 dev eth2 proto kernel scope link src 10.42.0.1 metric 1
169.254.0.0/16 dev eth1 scope link metric 1000
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.10 metric 2

Now, there is a whole in my understanding about this /16 /24 business. I will have to go away and read up about it. I've never had to mess about with it before.

The only issue I can see arising out of this set up is that my PI will not be able to be an equal member of the 192.168.0.254 network and hence file sharing could be a bit tough; though I am only guessing. I'd have liked to have been able to run Samba from the lappy and stored files. We'll have to see how that one pans out.

Just thought I'd bore you all to tears:) Now, does anyone know where I can post this so that it'll serve others?

Cheers,
Dunk

PS. If anyone can tell me what Ubuntu did when I choose that setting; I'd be most obliged :)
Posts: 2
Joined: Thu Sep 13, 2012 5:39 pm