Accessing Raspberry Pi via the Internet
Join the DZone community and get the full member experience.Join For Free
In this post I will explain how to access Raspberry pi via Internet. I am trying out a home automation system using Pi and I wanted to access a node.js service running in my Raspberry pi. Also I may wanted to SSH to the pi to restart or configure it while I am away from home.
Configuring the Router
In my home Raspberry Pi is connected to a Netgear R6300 router. The router is then connected to a DSL modem. In these kind of setup we might be dealing with four different IP’s
- External IP assigned to you by your ISP.
- IP Address of the modem
- IP Address of the Router assigned by the modem
- IP Address of the Raspberry Pi assigned by Router.
- Credentials to login to the Modem configuration.
- Credentials to login to the router configuration.
External IP: you can get your external ip by visiting whatismyip.com
IP Address of the Modem and Router: As I am using Netgear modem, I login into Netgear genie app. And from the Internet section I could see the IP Address assigned to my router as shown below.
As you could see the IP address assigned to my router is 192.168.1.2. Also you can see the Gateway IP address 192.168.1.1, that IP will be the IP address of your modem.
IP address of the Raspberry pi: SSH to Raspberry PI and issue ‘ifconfig’ command and you can see some details and check wlan0 as shown below. Line in bold shows the ip address of the Pi.
wlan0 Link encap:Ethernet HWaddr 04:a1:51:6a:fb:8d
inet addr:10.0.0.10 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5543 errors:0 dropped:11 overruns:0 frame:0
TX packets:1799 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:1200798 (1.1 MiB) TX bytes:192891 (188.3 KiB)
In my scenario I assigned a static IP to this Pi, other wise router’s DHCP server will assign ip and might change from time to time. So most router have configuration, to set static IP to the devices and will never change. At my house most of the devices like TV, Tabs and Raspberry PIs have static IP’s and the guest devices uses DHCP assigned IP’s.
Let’s configure SSH to the Pi from internet.
Router Port Forwarding
Excerpt from Genie help
Port forwarding is designed for FTP, Web server, or other server-based services. Once port forwarding is set up, requests from the Internet are forwarded to the proper server.
In the Genie app you can find Port forwarding in Advanced setup->Port forwarding/Port triggering, you might be able to find some thing similar in other modem’s configuration.
I clicked ‘Add Custom Service’ button and it will show the below screen to setup Port forwarding configuration. Here I am configuring SSH.
Here 22 is the port where SSH server in my PI is listening. Internal IP address is the IP address of the Raspberry Pi. Click apply and the changes will get saved.
We setup the port forwarding in the router. We need to do the similar configuration in Modem.
Modem Port Forwarding
Enter the IP address of the modem in the Chrome/IE, enter the user name and password if prompted.
Note: My DSL modem is pretty old one and may not be match with the interface of your modem configuration screen.
The port forwarding configuration in my Modem is located at Advanced Setup/NAT/Virtual servers. See the configuration below.
Here I setup the external port number as well as internal port number. You can have different external port number if you want, say for e.g. external port number 8090 can be mapped to internal port number 22. The server IP address should be the IP address of the router.
Check whether the port is opened by going to yougetsignal.com, if the port is still closed then you might need to do Port triggering in Modem as shown below.
We successfully configured SSH to Raspberry Pi from internet, you can check using a machine connected to a different network and connect to Pi using any SSH client like Putty. While connecting the IP address should be the external IP address and the port should be the one configured in Modem, here it’s 22.
The above approach will work, in case you want to open any port, I have a node.js service listening to 8090 in the same Raspberry pi. I did the above approach to access it via internet.
Leave your comments if you like this post or in case of any questions.
Published at DZone with permission of Sony Arouje, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.