I would like to be able to ssh to my raspberry pi over the internet. So far I have done the following:

  • Enabled the ssh server in raspi-confg
  • Set a static internal IP for the raspberry pi
  • Tried forwarding various router ports to port 22 on the raspberry pi
  • Identified my external IP from a website

After this I either get Name or service not found or Connection refused. I have not yet messed with dynamic DNS, but my external IP seemed to be fixed at least during the hour I was working on this. From everything else I've seen these are the necessary steps. Is there something else I'm missing?

Edits: I am running the default Raspbian from NOOBS on Raspberry Pi 3. I am able to ssh to the pi from inside the network. I have not configured any firewall rules on the pi. This is my first attempt at port forwarding, so I have not done it successfully for any other machine.

I don't yet have a good way to get the results of ssh -vvv. To be outside of my network at home I use my phone, but the app doesn't provide a -vvv option. I tried just now from work, but I'm not sure the results are useful if the firewall is blocking the connection. From work here's what I got:

OpenSSH_6.7p1 Ubuntu-5ubuntu1.4, OpenSSL 1.0.1f 6 Jan 2014 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug2: ssh_connect: needpriv 0 debug1: Connecting to <IP> port 22. debug1: connect to address <IP> port 22: Connection refused ssh: connect to host <IP> port 22: Connection refused

  • You have not specified which distribution you are running, whether you (or something) have installed firewall rules on the Pi, whether the Pi has a default route to the world, or whether you have verified that sshd is indeed running on the Pi. Can you connect to it from other machines on your network? Have you successfully port-forwarded stuff to other machines? How have you tested your setup to make sure it's the Pi's fault and not a problem with something else in your setup?
    – JayEye
    Commented May 17, 2016 at 4:14
  • In third step you should just set port 22 to your Raspberry Pi static address not various ports. You can look more about it here: howtogeek.com/66214/how-to-forward-ports-on-your-router
    – Huczu
    Commented May 17, 2016 at 7:13
  • Sorry for the confusion. I mean that I originally tried forwarding port 22 from the router. When that didn't work, I saw another question that said 22 can sometimes be treated differently by your ISP, and switching to another router port will sometimes fix the problem. I always only tried forwarding one port at a time, I've just made several attempts.
    – chris
    Commented May 17, 2016 at 10:36
  • 1
    If you could call your isp and ask them if the port has been blocked, that would help rule that out, also certain ISPs won't allow you to use an external IP from the same network as the Pi is on so be sure to be testing from another network such as your mobile data, and lastly be sure that you have properly forwarded the correct port Commented May 17, 2016 at 16:23
  • I find the output of ssh -vvv [email protected] to be useful to debug this sort of problem. How about posting the output minus stuff like your IP address? Commented May 17, 2016 at 17:25

1 Answer 1


Your problem likely relies on the connection being dropped. Now you want to login to your router settings often by logging into and forwarding port 22 to the raspberry Pi's local ip. If you still can not connect to the Pi remotely, you can try changing the port you are using. As some ISPs will block all outgoing traffic on port 22, to prevent you from inadvertently making your ssh server public. To change your ssh port edit the file /etc/ssh/ssh_config by running the following command:

   sudo nano /etc/ssh/sshd_config

And by changing the line that says

   port 22

To a different one such as 2122 by changing the line above to the following port 2122

After that reboot your Pi and ensure that it works locally, then forward the port that you have just selected such as 2122 to your Pi on your routers config page then try to access it from another network connection and see if that works. If it does not then your isp is probably blocking all inbound ports on your network and their won't be much I can do, often a lot of isps will unblock ports for a small processing fee. For me personally it's $7 a month for a static IP with no blocked ports.

Note: you should defiantly change the ssh login credentials on your Pi from the default, to prevent someone or a bot from guessing it and taking control of your Pi

  • Thanks, @Mohammad. Is it necessary to change the port on the Pi itself? I assumed you could test for this by forwarding the router's port 2122 to the Pi's 22, for example, but you're saying that the ISP may be blocking port 22 at the machine level?
    – chris
    Commented May 18, 2016 at 17:19
  • It's unlikely that the router is blocking the port at the router level, but some routers do not allow for different ports to be forwarded and changing the port at the Pi level lofted removes confusion as you would have to use a different port locally. Commented May 18, 2016 at 17:21
  • I called my ISP and they said they don't block any ports. I tried changing the ssh port on the Pi anyway but it still only accepted connections on port 22.
    – chris
    Commented May 19, 2016 at 11:46
  • Did you reboot your Pi after changing the ssh port? Commented May 19, 2016 at 16:24
  • @chris After editing sshd_config, restart the sshd service with sudo service sshd restart.
    – Matteo
    Commented May 19, 2016 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.