Here are the steps I used (on debian) which you might find useful.
1.Setting Static IP for the Pi.
a.Open the interfaces file using the command
sudo nano /etc/network/interfaces
b.Change the interfaces file to the following (This works for me I am not sure if a few of the lines can be excluded).
auto lo
iface lo inet loopback
iface eth0 inet static
address 192.168.1.2 #static ip you want for the pi
netmask 255.255.255.0
gateway 192.168.1.1 # the ip address of the router
c.Check if the Ip is changed using the ifconfig command
2.Setting up a dynamic DNS
Most probably the IP address assigned by your ISP is dynamic and so we need a way to resolve the changes in IP address.I use dnsdynamic and ddclient software to achieve this.Both are free to use.
a.Sign up for a account with dnsdynamic
b.Add a new domain and select a name and the domain from the list.You can choose any of the available domains with your choice of subdomain.Click on check availabilty and then add
3.Setting ddclient on Pi
a.Install ddclient using the command
sudo apt-get install ddclient
b.Open the configuration file for ddclient using the following command and add the following lines.
sudo nano /etc/ddclient.conf
and change the file to the following.
# /etc/ddclient.conf
daemon=600
timeout=10
ssl=yes
protocol=dyndns2
use=web, web=myip.dnsdynamic.com
server=www.dnsdynamic.org
[email protected] #userid for dnsdynamic.com
password='xxxx' #password for the same put inside quotes
x.dnsdynamic.com #the domain name you created
and save the file.
d.We want the ddclient service to start automatically on reboots.So do the following.
sudo nano /etc/rc.local
Add the following command before exit 0(which is the last line)
sudo /usr/sbin/ddclient -daemon 600 -syslog
Save the file.To start ddclient either reboot your pi or execute the above command on the prompt.It will keep your domain name updated with the current ip address changing when necessary.
4.Port Forwarding
a.Find the model of your router.
b.Go to portforward.com
c.Enter your router brand and model to find the detailed guide on forwarding port.
d.After selecting the router brand and model, Click on the Default guide from the redirected page.
e.In the Static Ip Text Box, Enter the Static IP of the Pi you gave in Step 1.
f.Follow the steps and forward port 22.The source port needs to be 22 and the destination port can be anything.Please remember to use the Destination Port when sshing into pi.To avoid confusion,I tend to use the same Source and Destination port.
If you were following the tutorial,
Login to your pi from anywhere by using replacing the ip address with the domain name.
eg- I use [email protected]:22 to ssh into my pi.