Monday, February 25, 2013

Getting to your pi from the outside

So now that you're familiar with the Pi, and clearly love playing with the Pi, and are busy programming the pi loads.
Now comes the problem of what happens when you need to leave the house. but still want to get to some stuff, (say you store some files on the device and want to get at them using SCP), or you want to run programs via SSH.

Allowing Access
The first thing that you're going to do is called Network Address Translation, Otherwise known as port forwarding.

First connect to your router, To find your routers address open up the command prompt and type
ipconfig, you'll fine a line that says gateway address, this is your internet router.

(probably on address 192.168.0.1 or 192.168.1.1 or 192.168.1.254)

Connect to this address in a web browser, and enter the username and password that's listed on the sticker on the bottom of the router, or in the instruction manual that came with the router.

Look at the internet settings, for the firewall that runs on the router. you'll need to look for a section marked NAT or Port forwarding.

In here you'll find a section that says, Outside port, inside port, and local address.

You need to put the port that you want to connect to your SSH server in the outside port box, (the default port for SSH is port 22, but you might want to put say port 66, or port 93 something that you can remember, but is not standard (so ideally people won't try to connect to it).

For the inside port you need to write 22.

In the address box you need to write the address of your raspberry pi.

you find this out by typing

ifconfig in the command prompt


Getting there
There are two ways of getting to your Raspberry pi.

Either you visit www.whatismyip.com before you leave the house all the time. and connect via IP address.
Or you sign up to a dynamic DNS provider like no-ip.com

I've signed up with no IP.com so the rest of this article will be giving instructions on how to install the client.

On a windows machine you can install the client just by running an executable.
On a Linux machine however there is no pre-made executable.

So to get this working you need to do the following.

First go to www.no-ip.com and create an account. chose either free or paid for dynamic DNS services. (depends what you want).

Now come back to your Raspberry Pi.

You need to be at a command prompt, (either not using x at all, or using the console shell within x.)

First start in your home directory, (which should be /home/pi)

You can find out where you are in the directory structure by printing the working directory.
pwd

If the output is anything other than /home/pi
Then type cd /home/pi

Now you need to create a new directory to put your download into

mkdir noip

And go into that directory

cd noip

Now you need to download the linux client files from the no-ip website.

wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz

Then you need to extract and unzip the files from the archive that you downloaded.
To do that use the following command:

tar vzxf noip-duc-linux.tar.gz

Then you need to browse into the folder that was just created.
So type ls to see what it's called:

pi@raspberrypi ~/noip $ ls
noip-2.1.9-1  noip-duc-linux.tar.gz


Then use the change directory command to move into that directory.

cd noip-2.1.9-1

Now you need to make the software only superusers can make software so you'll need to use the sudo command.

sudo make

then you need to install so type
sudo make install


when this set up runs you'll be asked for the user name and password to login to the service.

The trouble is, at least when I ran this, the configuration wasn't saved.

so in order to generate a configuration file type:
sudo /usr/local/bin/noip2 -C

then enter the user name and password details again.

this creates a configuration file in the /usr/local/etc directory called no-ip2.conf

now you can run the client
sudo /usr/local/bin/noip2

you probably want the client to run each time the machine reboots (so that you always have an up to date assignment of address to your host-name)

so you should add the no-ip client to a start up script.

To do this:
type
sudo nano /etc/rc.local

then use the arrow keys to go to the bottom of this file

then go up a few lines so your cursor is just above the line that says exit 0
then type the path and executable name

/usr/local/bin/noip2

to exit the editor hold down control and press X it'll ask you if you want to save, so press Y, then press enter to confirm the file name

now reboot your Raspberry Pi with the command
sudo reboot



You can check that the client is running by typing
ps -ef | grep noip

No comments: