In this guide, I will take you through on how to setup Raspberry Pi port forwarding on the router and also setting up Raspberry Pi Dynamic DNS. Both of these complement each other, but port forwarding is very important if you want web applications accessible over the internet.

I have prepared a video further down the page that takes you through everything you need to know about setting up both of these.

It’s important to remember that opening up ports will have an increased security risk. Tools such as Fail2ban will help protect your Raspberry Pi from intrusion. Also, make sure you pick strong, secure passwords.

If you enjoy this tutorial, then please subscribe so you can stay up to date on all the latest awesome Raspberry Pi projects, guides and much more.

Video

It’s important to know that the video goes through an older method than the text version provided below does.

Both methods will work just fine but if you’re looking for the latest, then be sure to check out the full-text version.

Setting Up Raspberry Pi Port Forwarding

Raspberry Pi port forwarding is a method where can allow external access to the Pi. To do this, we will need to change some settings on the router.

All routers are different but I will try and make this as generic as possible however there could still be a lot of differences between these instructions and your router. The router I am using for this tutorial is the TP-Link AC1750 wireless dual band gigabit router.

1. On a computer that is connected to the local network, connect to the router admin page via a web browser.

A router IP typically is 192.168.1.1 or 192.168.1.254

2. Enter the username and password for the router. By default, this is typically admin & admin.

3. In the router admin page head to forwarding->virtual server.

4. On this page enter the following

Service Port : This is the external port.

: This is the external port. IP Address : This is the IP of the Pi.

: This is the IP of the Pi. Internal Port : Set this to Pi’s application port. (A web server runs on port 80 for example)

: Set this to Pi’s application port. (A web server runs on port 80 for example) Protocol : Set this to ALL unless specified.

: Set this to ALL unless specified. Status: Set this to enabled.

5. These settings will route traffic destined for the port specified to the port on the Raspberry Pi.

6. You should now be able to connect to the application on the Raspberry Pi outside your network.

The best way to check if you have port forwarded correctly would be to either get a friend to connect or go somewhere outside your local network (A VPN could achieve this).

Below is an example of setting up access to a web server that is running on port 80 with the Raspberry Pi having an IP of 192.168.1.103 .

Troubleshooting

There are a few issues that might occur when attempting to set up your Pi for external access. Below are just a few issues you might come into when setting up Raspberry Pi port forwarding.

Double check your router settings and confirm they are correct.

Check that your external IP hasn’t changed. ISP’s will provide you with a dynamic IP rather than a static IP.

Restarting the router might clear problems.

Restarting the Raspberry Pi might also clear any problems.

Setting up Raspberry Pi Dynamic DNS

If your ISP supplies you with a dynamic IP (An IP that changes often), then it will probably be worth setting up the Raspberry Pi dynamic DNS (Sometimes Routers has this feature in them). This method means you will always be able to connect to the application on the Pi even if your external IP changes.

To get started we will need to install the DDclient, and it’s a pretty straightforward process that won’t take too long to get installed.

1. To begin, run the following lines to install the DDclient. Please note, just skip through the install, we will override these settings.

sudo apt-get update sudo apt-get install ddclient libjson-any-perl libdata-validate-ip-perl

2. After all of that, we will now need to replace that version of DDclient with a more recent one. This newer version will enable better support for services such as Cloudflare. To download the latest version enter the following commands.

wget https://files.pimylifeup.com/portforwarding/ddclient-3.9.0.tar.gz tar -zxvf ddclient-3.9.0.tar.gz

3. Once that has finished downloading and extracting, we will make use of the newer binary by copying it over the current one using the following command:

sudo cp -f ddclient-3.9.0/ddclient /usr/sbin/ddclient

4. Due to changes made in the most recent version of DDclient, the location of the configuration file has moved. We will use the following commands to reposition the one that came with the first install.

sudo mkdir /etc/ddclient sudo mv /etc/ddclient.conf /etc/ddclient

5. Once the ddclient has completed installing, we will now go and directly edit the configuration file to make a few crucial changes. Use the command below to launch an editor for the file.

sudo nano /etc/ddclient/ddclient.conf

6. We will need to add a few lines to this file, and we will use these same lines throughout every different configuration. They define whether to use SSL and how to obtain the external IP address of the Raspberry Pi.

7. Add the following to below the commented header in the file, also remove anything else in the file. We will add more to the file later on with our guides on using No-IP or Cloudflare.

use=web, web=checkip.dyndns.com/, web-skip='IP Address' ssl=yes

8. OK, now that this is done we can proceed to the next couple of steps on how to setup ddclient with a Dynamic DNS provider.

Just to quickly point out that the main difference between CloudFlare and No-IP is that you need to own a domain name to make use of CloudFlare. No-IP However offers free subdomains, meaning it’s a costless service to use.

It’s important to note that while Cloudflare isn’t truly a Dynamic DNS provider, we can use it the same way thanks to its API. Cloudflare also offers more protection such as free SSL, anonymized IP, DDOS protection and more. You can turn off the Cloudflare features and just use it as a DNS provider if you want. You may find that you need to do this anyway.

Dynamic DNS using Cloudflare

1. Before you start using Cloudflare as a Dynamic DNS provider, you will need to purchase a domain name. If you have a domain name already, you can skip ahead to the next step in this guide. Otherwise, you can purchase a domain name from somewhere such as NameCheap.

2. Now you will need to sign up to Cloudflare, don’t worry as it won’t cost you anything.

3. Sign up and follow the steps given for updating your domain name to point towards Cloudflare’s DNS (Domain Name Servers).

4. Next, you will need the Cloudflare API-key that you’re able to find on the account page. You want the global API key, copy and paste this somewhere safe for now.

5. Now we need to enter the Cloudflare details into the DDclient configuration file. Open it by running the following command.

sudo nano /etc/ddclient/ddclient.conf

6. Update or add the following lines to the bottom of the file, making sure to replace the username, api-key, zone and the hostname with the ones you used to setup your Cloudflare account.

The zone is the domain name itself and then underneath is the record/domain you wish to update. Either enter a subdomain or the primary domain itself. Make sure the record exists within Cloudflare on the DNS page, it will need to be an “a” record.

protocol=cloudflare server=api.cloudflare.com/client/v4 login=your_email password=your_api-key zone=your_domain.com anything.your_domain.com

7. Once you have updated the file, you can save and quit by using Ctrl-X.

8. Now, all we need to do is restart DDclient, as long as you have entered the correct information in step 3. everything should work correctly. Use the following command to restart the dynamic DNS client. (Sometimes you need to restart twice for the changes to take place)

sudo /etc/init.d/ddclient restart

9. The IP should now be updated within Cloudflare, and you should now be able to use your chosen domain name to connect to your Raspberry Pi. You may need to turn off the Cloudflare proxy for it to work, you can do this by going to the DNS page within Cloudflare and clicking the cloud next to the domain record, so it becomes grey.

You will now want to continue to the “Running ddclient as a Daemon” section of this guide so that you can have the IP automatically update otherwise you will need to restart the ddclient manually every time you want to update the IP.

Dynamic DNS using NOIP

1. To setup Raspberry Pi Dynamic DNS with NOIP, you will need to create a free account over at their website. Make sure you keep note of the username, password and the hostname that you pick as we will need them in the next step.

2. After you have finished creating your account, it’s time to enter these into the DDclient configuration file. You can open up the config file by running the following command.

sudo nano /etc/ddclient/ddclient.conf

3. Now, either update or add the following lines to the bottom of the file, making sure to replace the username, password and the hostname with the ones you used to create your No-IP account.

protocol=dyndns2 server=dynupdate.no-ip.com login=your_username password=your_password your_domain.com

4. Once you have updated this file, save it and quit by using ctrl-X.

5. Now, all we need to do is restart ddclient, as long as you have entered the correct information in step 3. everything should work just fine. Use the following command to restart the client.

sudo /etc/init.d/ddclient restart

6. Your IP address should now be updated, and you will be able to use your chosen domain name to connect to your Raspberry Pi given that the correct ports are open.

Continue the tutorial below to ensure that the ddclient automatically updates the IP.

Running ddclient as a Daemon

Since we don’t just want the IP address to update once, we still need to setup ddclient to run as a daemon so it can check for a change of IP address periodically and notify the chosen dynamic DNS provider if necessary.

1. To make DDclient runs as a Daemon, we need to make a change to a configuration file. Run the following command to begin editing the file.

sudo nano /etc/default/ddclient

2. In the file, you will need to change run_daemon so that it matches the line below. This change basically tells it to run ddclient in daemon mode.

run_daemon="true"

3. You will also need to check two other lines in this file to make sure that they are false. Otherwise, ddclient will not run in daemon mode correctly. Ensure the following two lines are matching, if they are set to true then change them to false.

run_dhclient="false" run_ipup="false"

4. Press Ctrl+X and then Y to save the changes to the configuration file.

5. Now enter the following command to startup ddclient as a service.

sudo service ddclient start

6. To ensure the service has properly started, you can use the following command to check its status.

sudo service ddclient status

7. Some dynamic DNS providers require you to update your IP address quite frequently. We can help ensure that you don’t get timed out by forcing ddclient to update your IP address once a week. We achieve this by first opening up the weekly crontab by using the following command.

sudo nano /etc/cron.weekly/ddclient

8. To that file, add the following lines.

#!/bin/sh /usr/sbin/ddclient -force

9. Save the changes by pressing Ctrl-X and then Y to accept the changes.

10. Lastly, we need to allow our new script to execute, and we can do this quickly by doing the following.

sudo chmod +x /etc/cron.weekly/ddclient

11. ddclient should now be successfully running as a daemon, use the following command to make sure that it is running correctly.

sudo service ddclient status

12. That should be all you need to do to have everything running smoothly.

If you find that something isn’t working correctly, then the following line is great for debugging. It will spit a ton of lines just look for anything that might look like an error or a hint at why it is not working.

sudo ddclient -daemon=0 -debug -verbose -noquiet

As long as you have Raspberry Pi port forwarding setup correctly, you should now be able to access your Pi outside your local network using a domain name. If the above instructions do not work for you, then you can always try out the old method which is detailed below.

Setting up Dynamic DNS (Old Method)

This next part is an old process that I replaced with the method above. It should still work, but the one above should be much easier to get up and running.

1. First, you will need to create an account over at No-IP (Make sure “create a hostname later” is unticked)

2. Now on the Raspberry Pi enter the following commands

sudo bash cd /usr/local/src/ wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz tar xf noip-duc-linux.tar.gz cd noip-2.1.9-1/ make install

You will be now presented with a few settings you will need to set.

Enter the email and password for the account you wish to connect the Pi to.

and for the account you wish to connect the Pi to. Next set the update interval with the default being 30 minutes. (This checks to see if the IP has changed)

You can now also set something to run whenever the IP updates.

3. Now add a new line into the rc.local file just before the last line exit 0 this ensures that no-ip will automatically start on reboot.To do this enter the following commands:

nano /etc/rc.local

4. Enter the following line right above the exit 0 line.

/usr/local/bin/noip2

5. Use CTRL+X to exit (Make sure you save)

6. You can start no-ip with the following command:

sudo /usr/local/bin/noip2

7. Check the status with the following command:

sudo /usr/local/bin/noip2 -S

8. You can close it using the following command. Replace pid with the pid from the -S command.

sudo /usr/local/bin/noip2 -K 'pid'

9. You’re able to recreate the default config file if required by entering the following command:

sudo /usr/local/bin/noip2 -C

Now you should have a Raspberry Pi dynamic DNS server setup and be able to connect with a domain name rather than an IP. This name will remain the same even if your IP address changes.

This setup means you will always be able to connect to the Pi or other home network devices without needing to find out your new external IP.

If you’re looking for a project that requires access to the internet, then you should check out either the Raspberry Pi Web Server, mumble server or even the webcam server.