Connecting to the Raspberry PI from the outside world


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 ( 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

Another way to get your external IP address is to log onto 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
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.


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
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 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. (
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:



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

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 | mail -s “Your PI IP”

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 I opened the script by typing the following:

sudo nano

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 to /etc/cron.hourly.

sudo cp /etc/cron.hourly/

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.


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.


  1. 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 😉

  2. Hi there, this is fantastic. I am using this to access my 3D printer from across the web.

    I got this working yesterday (I got a friend to test it for me too and he could connect) I could also connect from my laptop and phone to the external IP of my Raspberry Pi, but today when I tried connecting, both on my phone and laptop, I cannot reach the server..

    instead of using I am using 'curl' to email myself my external IP address (And this was also working) and it is still reporting the same IP address as yesterday but now it doesn't let me connect.

    Do you have any idea why this might be?

    Kindest regards,


    • Good question.

      It is possible with the use of a script.

      Store the value of the current IP address somewhere and in the script check the value of the stored IP address against the current IP address. If the stored value is different then the value has changed so send the email, then store the current IP address again

    • ok, i added a new script, "" which contains..

      lynx –dump >/home/pi/currentIP.txt

      Then i went into "sudo crontab -e" and added the following line to the bottom…

      */10 * * * * sh /home/pi/

      This updates my current IP to the file "/home/pi/currentIP.txt" every 10 minutes.

      Do you have any idea as to how to update the "" script to check that file every hour, and only send the email if the value has changed.


  3. Thanks Gary, I was able to get this working last night. It seems to be working well, but let me know if you think their is a more efficient way. I used this link as a guide, but ended up changing the code a bit so have left my steps below as well.
    To get this working, i have a script (/home/pi/scripts/ which contains..


    read ip1 < /tmp/iptest.txt
    ip2=$(wget -qO-

    if [ "$ip1" = "$ip2" ]
    echo "$ip2" > /tmp/iptest.txt
    echo "$ip2" | mail -s "Your IP has changed"

    Then, I go into..
    sudo crontab -e
    and add a line to the bottom..
    58 * * * * sh /home/pi/scripts/

    Now my ip is checked every hour, and if their is a change, the new ip is emailed to me!

  4. Hi Chakka,

    What kind of fan is it? Does it connect via USB, does it have bluetooth or wireless controls?

    Can you give me a link to a web page that shows the make and model of fan and I will see what I can come up with? Shouldn't be too difficult.

  5. Thank you so much for a such a nice description

    I am facing one problem
    when i am trying to remotely access my pi with same user login and password then it is showing access denied (It is showing login window properly)..while with that same user and password is working fine in LAN
    can anybody tell how can i fix it that i can login ?

  6. i purchased my new raspberry pi2. i installed on it (raspbian wheezy).
    now after connecting my raspberry to laptop through LAN wire. now i write command (apt -a) on cmd its show dynamic IP address. i copied that IP address and write on (putty) and then small screen open and asking login and password.
    i entered "pi" as login it accepted but password time its says " access denied".
    now what i do?
    please help me friends.

Leave a Reply