Wednesday, 5 June 2013

Connecting to the Raspberry PI from the outside world

Posted by Gary Newell  |  at  00:08 6 comments

Introduction

Yesterday I wrote an article showing how to set up the Raspberry PI as a personal web server

The concept of setting up the personal web server on the Raspberry PI really was for a hobby project and shouldn't really be used for hosting an external website. There are various reasons for this such as security, the power of the Raspberry PI, bandwidth and the fact that your external IP address will change at regular intervals.

This article goes some way to addressing some of the things you will need to do to connect to the website on your Raspberry PI from the outside world.

This is a proof of concept and just ideas on paper (or screen) and should not be seen as a gospel guide.

I will warn you now that I have never been that good with the networking side of things so I will try and explain as best I can. I may or may not be wrong in places and I am relying on people to correct me in the comments section.

Get your External IP address

There are basically 2 IP addresses that are important. There is the internal IP address which you can get by running IFCONFIG in a terminal.

When you connect to the internet you will actually be using an external IP address. 

I followed this guide (http://www.if-not-true-then-false.com/2010/linux-get-ip-address/) to get an external IP address.

I installed lynx first by running the following in the command line:
sudo apt-get install lynx
Then I ran the following command to get the external IP address.
lynx --dump http://ipecho.net/plain
Another way to get your external IP address is to log onto http://www.whatismyip.com/ but I will explain later why I bothered to install lynx.



Port Forwarding

Now this is the part that I can't really explain in much detail because it depends on the ISP and the router that you are using as to how it is done.

I am using 3 Mobile Broadband and to forward ports using this service you have to open a web browser and navigate to http://192.168.1.1.

This takes you to the home page of the router. There is a login section where you can then access more intricate settings for the router.

On 3 Mobile Broadband there is a settings tab and then under the security heading there is a section called virtual servers.

This is where the port forwarding takes place.

All I had to do was add a new row into the grid and enter the following details:

  • name
  • wan port
  • lan ip address
  • lan port
  • protocol
  • status
The name is just a description so that I can remember what the purpose of the entry is in the list. The wan port is the port from the outside world and so for the web server I entered 80 and for ssh I set this to 22
The lan ip address is the address to the Raspberry PI (run IFCONFIG to get this).
The lan port is the port to forward to on the Raspberry PI. For a web page I set this to 80. For ssh I set this to 22.
The protocol is either TCP, UDP or ALL.
The status can be set to on or off and obviously I set this to on.

Pressing Apply applied the changes.

Testing it out



















It is not possible to test this on the same network that the Raspberry PI is on. So if you are using the same broadband connection as the PI then you can't test connecting from the internet to the Raspberry PI as you would essentially be accessing the Raspberry PI from inside the network.

You will therefore need to test it from something not connected using the same network (broadband connection). If you have a 3G smart phone then you can use the browser on the phone to try to connect to your external ip address, alternatively pop round to the local library or pop to your nearest electronics store and buy a cheap 3G dongle. 

If it worked correctly then you will see the web page appear.

Troubleshooting

What happens if you cannot connect to the Raspberry PI?

First of all it is a good idea to see which ports are available from the outside world.

There are a number of sites that provide such a service. One of these services is available at http://www.yougetsignal.com/tools/open-ports/.
























You can enter the external IP address and ports to check and you will be told whether they are visible from the outside world. 

If the port is not open or visible then you have set up the port forwarding incorrectly.

Static IP address

To prevent everyone setting themselves up as web hosts, ISPs give each of their customers a dynamic IP address.

This means that the external IP address you will use today to connect to your site will be different tomorrow or even in just a few hours time.

If you are determined to use your Raspberry PI as a web server (not recommended) then there is a service at http://dyn.com/dns/ which enables you to set up an address that will remain static so you can always access the websites and ssh on your Raspberry PI.

Remote access to the Raspberry PI

If however you just want to be able to access your Raspberry PI from say your work then you won't want to pay money to access your Raspberry PI.

I use the following method to continuously be able to access my Raspberry PI when I need to.

First of all I installed SSMTP by following this guide. (http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/).

To install SSMTP I ran the following command:
sudo apt-get install ssmtp
Next I edited the ssmtp.conf file by running the following command:
sudo nano /etc/ssmtp/ssmtp.conf
I then added the following to the end of the file:

root=postmaster 
mailhub=smtp.gmail.com:587
hostname=raspberrypiAuthUser=myemailaddressAuthPass=myemailpasswordUseSTARTTLS=YES
I pressed CTRL O to save the file and CTRL X to exit nano.

Next  I edited the revaliases file by typing the following command.
sudo nano /etc/ssmtp/revaliases
Within the file I added the following line

pi:pi@everydaylinuxuser.com:smtp.gmail.com:587

I then saved the file by pressing CTRL O and exited the file by pressing CTRL and X

Finally I set the permissions to 774 by running the following command:
sudo chmod 774 /etc/ssmtp/ssmtp.conf
All of this basically enables me to send an email.

To send an email all I have to do is run a command and output it to the mail command. For example:
lynx --dump http://ipecho.net/plain | mail -s "Your PI IP" me@myemailaddress.com
This emails my IP address to my email address

Obviously this only does it once and so it would be better if it sent me an email at regular intervals so that I can see if there has been a change.

To do this I added the above command to a script called sendmemyIP.sh. I opened the script by typing the following:
sudo nano sendmemyIP.sh
I then copied the lynx command into the script and pressed CTRL O to save and CTRL X to exit nano.

To get the script to run regularly I added it as a cron job by copying sendmemyIP.sh to /etc/cron.hourly.
sudo cp sendmemyIP.sh /etc/cron.hourly/sendmemyIP.sh
Now when my Raspberry PI is turned on it will send the IP address to my email inbox once an hour.

I can then use this IP address within putty or ssh to access my Raspberry PI from anywhere.

Summary

This was all done as a proof of concept and I'm looking to the community to tell me if there are any glaring no-nos with the approach I have taken.

It certainly works for me and hopefully it will help some other people out there looking to try the same thing.

Thankyou for reading.


About the Author

Gary Newell started the Everyday Linux User blog in 2010 and has written reviews on dozens of different Linux based operating systems. He has also written a number of tutorials.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

Related posts

6 comments:

  1. Thanks for the information, I found this very useful!

    ReplyDelete
  2. Thank you so much for this guide. I have been trying to get remote SSH access to my pi from anywhere. Could I link this page to my Google plus site? I am going to set up my pi access soon. :)

    ReplyDelete
    Replies
    1. Thanks for the response. Yes feel free to link from Google+

      Delete
  3. Thank u .....very much.

    An awesome article.....helped me a lot.

    ReplyDelete
  4. All worked perfectly, had an issue with the gmail password and mail wouldn't send, simple password change fixed that, mailx on the pi is separated now into 3 diff apps now..

    mailutils 1:2.99.97-3
    heirloom-mailx 12.5-2
    bsd-mailx 8.1.2-0.20111106cvs-1

    I used mailutils and was great!

    Still working out the cron job now... not a big cron fan so will get googling ;)

    ReplyDelete

Feel free to comment on any of the blog posts. Please try to be constructive.

Offensive messages will be removed as will blatant adverts for misleading products and sites.

Thanks for visiting my blog

    Popular Posts

    Total Pageviews

    Subscribe

    Enter your email address:

    Delivered by FeedBurner

    Popular This Month

    What are other people buying?

    Zorin OS Buy
    PCLinuxOS Buy
    Lubuntu Buy
    Linux Mint Buy
    Simplicity Linux Buy
    Kubuntu Buy
    Kali Buy

    Followers

    Feedburner Followers

Blogger templates. Proudly Powered by Blogger.
back to top Google