1

I can't seem to find a straight answer anywhere. So I'm just going to ask SO instead.

For arguments sake, lets assume these are my details

public IP       ~ 11.111.11.11
Pi's private IP ~ 192.168.0.6
Port forwarded  ~ 21 both TCP and UDP
Pi's name       ~ pi@mikespi 

I have my pi turned on, sitting at home. Now I'm at work, and I want to SSH onto my pi. I am running linux mint so I want to use the terminal (bash) to SSH. What command do I write?

even ssh -h hasn't helped me work out how to do this remotely.

Note: I have managed to SSH onto the pi within my own network easily.

F1Linux
  • 1,589
  • 1
  • 9
  • 28
Meeky333
  • 51
  • 6
  • 2
    Does this answer your question? [How to use SSH out of home network](https://raspberrypi.stackexchange.com/questions/6757/how-to-use-ssh-out-of-home-network) – Mark Feb 07 '20 at 13:01
  • 2
    "*Port forwarded ~ 21 both TCP and UDP*", why you forwarded to port 21? The default SSH port is 22. Did you change it? – M. Rostami Feb 07 '20 at 13:30
  • I have since read that 22 is the default port. but I just chose 21 at random to be honest, I didn't think it mattered – Meeky333 Feb 07 '20 at 13:39
  • 1
    @Meeky333 If you want to use a different ssh port is fine, but you'll need to make sure that the ssh daemon on the Pi is set to listen on the port 21! – Charemer Feb 07 '20 at 16:24
  • @Meeky333 I just Added a working MikroTik config you can use as a template to configure your own DNAT. – F1Linux Feb 07 '20 at 19:09
  • Don't choose a port number less that 1023, they are mostly reserved. Pick a port like 2022, 3022 (if you insist on not opening 22). Leave the ssh daemon listening on 0.0.0.0:22 (INADDR_ANY port 22). – Dougie Feb 07 '20 at 20:52
  • 1
    Quoted from Wikipedia about [Well-known ports](https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports): >The port numbers in the range from 0 to 1023 (0 to 2^10 − 1) are the well-known ports or system ports. They are used by system processes that provide widely used types of network services. On Unix-like operating systems, a process must execute with superuser privileges to be able to bind a network socket to an IP address using one of the well-known ports. You should only use but never redefine them. You are forwarding to or from (you don't specify it) port 21 for ss – Ingo Feb 07 '20 at 19:17
  • 2
    The only hassle that will accrue from using a non-standard port are the style of connection errors you will get -- which anything that requires authorisation will see a regular trickle of such anyway. Of course, if you are going to use a non-standard port it makes sense to use a more obscure one. Doing this w/ ssh is very common since if you use 22 you will get more than a trickle of erroneous connection attempts. – goldilocks Feb 07 '20 at 20:59

5 Answers5

4

This is a combination of M. Rostami's answer and my reflections on his answer, which I found cumbersome to add as comments.

NB: Your question shows you know how to find your public IP address and the internal address of your Pi. I've included directions here in the hope that they will help others.

On your Pi, set up SSH on the default port number, port 22. You will also need the internal IP address of your Pi, which you can get with ip a typed into a terminal window. Look for the eth0 address for wired connections or the wlan0 address for wireless. The address may look like 192.168.x.x or 10.x.x.x.

When you have SSH running, set up SSH cryptographic key security. That allows you to log in with a cryptographic key rather than a password. For so long as you keep the key secure, your Pi is protected. There are instructions for setting up cryptographic authentication here: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 and probably plenty of other places, including a brief discussion of the value of such keys in this Stack Exchange Community.

  • Do protect the private key with a password. You'll have to type it every time you log in, but you've at least somewhat protected the private key in case your remote system is somehow compromised.
  • Do disable root logins using passwords after you've tested that you can log in using SSH protected by cryptographic keys.
  • Consider disabling all logins with passwords. (Do this unless there's a good reason why someone could not use a key to log in.)

Note: Your Pi is not accessible from outside yet, and that's good. Set up security first, then open up access.

You will need to pick an external port number to access your server from outside. The bad guys on the Internet "rattle doorknobs" looking for open ports, then try to get in through those ports. You can't stop them, but you can discourage them. One way of doing that is to use a slightly obscure external port. Pick a port number that is greater than 1023 and does not end in 22. Perhaps something like 7137. Your SSH will still be detected by full port scanning, but it's a lot more work than just rattling those doorknobs that end in 22.

Now set up your router to forward incoming traffic on the port you picked to the internal IP of your Pi on port 22. How to do that will depend on the router you have.

You are now set to SSH to your Pi from outside. You can find out the public (outside) address of your router with https://www.whatismyip.com/

Assuming a public address of 11.111.11.11 and a port of 7137, you'd SSH to 11.111.11.11:7137. Done!

Although your ISP can change that outside address, called a dynamic IP address, my own experience is that mine doesn't change often. If you find, using WhatIsMyIP, that yours changes frequently, there are dynamic DNS services that will let you assign a name and keep up with the changing IP address. I use a paid service, but there are free services. Something within your home will have to run the dynamic DNS client. You can do that on your Pi.

Bob Brown
  • 1,015
  • 7
  • 12
3

Port forwarded ~ 21 both TCP and UDP

It's actually forward packets to port number 21 which is the default port of FTP. You must change it to port number 22 on your router/switch.

If you are stubborn about changing the default port of SSH which is 22 to another port number, take note that you can set the SSH default port to 21 but the FTP client would be disabled. Therefore, Well Known Port is term of telling don't use port numbers in the range of 0 through 1023. If you do, you will face a problem with other protocols. So, you should set a port number more than 1023 for the SSH daemon.

On the other hand, according to your router/switch/firewall on your network, you can set a port forwarding rule or PAT. As an example forward all external port number 9022 to 22 on the raspberry pi IP address/hostname. In this case, you should connect to your raspberry pi over the internet by x.x.x.x:9022 because the router/switch/firewall will forward to 22.

In addition, it's so easy to find which opened port number of your raspberry pi has been used for SSH. You can do it by nmap command.


How to change the default SSH port number on Raspberry PI?

Open the SSH server config file:

sudo nano /etc/ssh/sshd_config  

Then, (for example) add this line to set the port number to `2222:

port 2222  

Reboot the device or restart the SSH server's service:

sudo service ssh restart
M. Rostami
  • 4,235
  • 1
  • 15
  • 34
  • 1
    Thanks for considering Mr. @BobBrown – The number `2222` was an example. As I said, it's so easy to figure out which port has configured for a Linux host, the `22`, `2222` or the `7137`. – M. Rostami Feb 08 '20 at 21:13
2

First the ssh port is usually port 22 but it can be changed and secondly you only need tcp not udp. Another issue that can crop up is ensuring the pi will accept connections on port 22 from outside your LAN which can be achieved using UFW https://wiki.ubuntu.com/UncomplicatedFirewall

Make sure you set a strong password or better still use SSH keys as you WILL get hackers trying to force their way in, some guides to best security practises here https://blog.devolutions.net/2017/4/10-steps-to-secure-open-ssh https://www.howtogeek.com/443156/the-best-ways-to-secure-your-ssh-server/

Bra1n
  • 1,181
  • 6
  • 7
  • I've been setting up what I can and only trying to SSH into it at work. But I since wandered, would I be able to SSH into it from home while pretending to be external? – Meeky333 Feb 07 '20 at 13:50
  • 1
    I hadn't looked into the Pi's firewall yet. I am becoming aware how dangerous it can be to leave a port open for a Pi defended by a weak password. I just want to try it out at the moment, but this weekend I will be doing everything I can to protect my network. – Meeky333 Feb 07 '20 at 13:52
  • Can you SSH from home, pretending to be external? If you have a shell account on an external computer that you can reach from home, you can SSH to the external machine, then try to SSH to your Pi from that machine. – Bob Brown Feb 07 '20 at 16:38
2

Provided that the SSH server at the PI is actually configured to listen on port 21 instead of the default 22, and that the network public IP address doesn't change. Then you should just SSH to the public IP address of your network.

>ssh 11.111.11.11 21

login the same way as you do when only using the local (home) network. Using no-ip or dydns can help give your network a name in DNS that remains updated if your public IP is changed by your ISP (assuming you don't have a static IP assigned here).

If this doesn't work then make sure that

  1. RPi network parameters are set correctly (particularly gateway)
  2. Port forwarding is correctly configured at the router and that responses are not being blocked by any router firewall.
Charemer
  • 615
  • 4
  • 11
1

Short Answer:

Here's a TESTED and WORKING specimen config from my MikroTik router that you can use as a model for your own router.

chain=dstnat action=dst-nat to-addresses=192.168.0.6 to-ports=21 protocol=tcp in-interface=ether1-Gateway dst-port=60000 log=no log-prefix=""

Where:

  • "ether1-Gateway" is my connection to Internet
  • "dst-port=60000" is an arbitrary port chosen from the "Dynamic/Private" port range.
  • "to-addresses=192.168.0.6" The Pi
  • "to-ports=21" The port on Pi receiving the forwarded traffic

WARNING: Avoid "Privileged" port numbers such as "21"- use a high port in the "Dynamic/Private" port range. "21" is used for FTP and now some routers are offering FTP services...

So when I SSH into the router on port 60000 as follows:

ssh pi@11.111.11.11 -p 60000 

The router sends my traffic to the FORWARDED port "21":

192.168.0.6 on port 21

DON'T FORGET TO CHECK YOUR FIREWALL RULES allow the desired connectivity

Long Answer:

Using DNAT to connect remote-in to internal hosts that are NOT offering public services such as mail or web servers is not ideal. Using a VPN to connect to hosts on Local IPs is.

Here's a comparison between the (2) methods to connect remotely to a host on an RFC 1918 address:

VPN: Preferred Method

Configuration: You'll need to configure VPN on the router, then configure a client on your computer/device to establish a VPN connection to the router.

So once your VPN connection is established to your home/work router, your SSH connection looks like this:

ssh pi@192.168.0.6 -p 60000

Note that you're reaching it on a LOCAL IP.

Upside:

  • Using a VPN, you don't publicly expose your Pi. You can talk directly to it on the local address once you establish the VPN connection to your router.

Downside:

  • Configuring an IPsec VPN on the router requires a little bit of networking knowledge. From the nature of your question, I suspect you don't possess the fundamental skills to do this easily. But hey- great opportunity to learn!

  • Some countries are not VPN friendly and do not want you passing traffic through an encrypted tunnel...

DNAT

In the "Short Answer" I showed you a very simple way to do a DNAT using the ROUTER's own Public IP. The following method is a bit more elegant, where each host has traffic forwarded on a Public IP dedicated to it.

Configuration: A Better DNAT Solution:

Ask your ISP for a block of Public IPs. If you ask for a /29, you're more likely to get your request approved than if you ask for a /28 (or greater). Configure these Public IP addresses on the router and setup a DNAT rule to forward traffic from the public IP to the internal IP. Each host can have use a different public IP to forward traffic to it. Tidy.

Upside:

Less complex to setup a DNAT than an IPsec VPN.

Downside:

  • Just as you can connect to your Pi from the outside world, so can anybody else: traffic is being forwarded from the Public IP that is being mapped by the DNAT rule to the Pi's internal (non world-routable) local IP.

  • Requires your ISP to assign you a block of Public IPv4 IPs, which are getting scarcer and scarcer by the day. And it's very likely they'll charge you a monthly fee for the block they assign to you so potentially a recurring cost to this solution (thanks @goldilocks)

  • You'll lose your current Public IP the router is using, which may or may not be a problem. If they assign you the block of Public IPs, the router's address itself must be from within this range of IPs.

  • If you change ISPs, you will lose this block of Public IPs and your connectivity to the Pi will be busted. The ISP owns the block- they are letting you use their IPs only.

F1Linux
  • 1,589
  • 1
  • 9
  • 28
  • 1
    Thank you for this answer. This is very detailed and informative. I already know more about VPN's than I do DNAT so I think I need to get it set up on my router. My networking knowledge is pretty low to be fair. But I'm only a cub :P – Meeky333 Feb 07 '20 at 14:59
  • 1
    That's how you learn! Everybody has to start somewhere. It's things like this will help get you on the path to being a networking guru ;-) – F1Linux Feb 07 '20 at 15:01
  • 1
    Beware your ISP is unlikely to provide you a stable static IP for free. – goldilocks Feb 07 '20 at 15:40
  • @goldilocks Previously they would bung you a block of /28 without any grief. I guess as the available free public IPv4 space gets smaller and smaller, the ISPs are going to see a charging opportunity ;-). Excellent point. I'll update my answer with your feedback- – F1Linux Feb 07 '20 at 15:43
  • Now when I google VPN, I get bombarded with VPN's for sale from companies... Am I right in not really trusting these? If possible I would like to make my own VPN, VPS and just use that. Is this the right approach? Or should I just succumb to the adverts? – Meeky333 Feb 07 '20 at 15:51
  • 3
    this answer seems to ignore the SSH and port forwarding configuration presented in the question. So whilst providing some general information on VPN, and DNAT which is frankly overkill (use a dyndns instead) it doesn't address the problem using the configuration the user has. – Charemer Feb 07 '20 at 16:27
  • @Charemer The user knows how to use SSH,it's a question about how to connect to a Pi on a non-routable RFC 1918 IP address. If you read the users own comments above your own, you'd see that was the detail he was looking for. Always welcome useful feedback, but this comment proves you don't understand what the problem the user is trying to solve is. – F1Linux Feb 07 '20 at 16:38
  • Instead of a static IP address, consider one one of the dynamic DNS providers. I use DynDNS, which requires a fee, but I believe there are free options. – Bob Brown Feb 07 '20 at 16:40
  • @F1Linux - apparently the user doesn't know how to use SSH - evident from the question. I don't question that some of your answer is useful information. I don't read the same meaning into the users' comment as you do. "My networking knowledge is pretty low..." as they state. VPN (as you describe) might be more secure or not depending on the router implementation (if it's even a feature available on the OPs rotuer) . Certainly DNAT adds nothing useful here. – Charemer Feb 07 '20 at 16:50
  • Well, this doesn't directly answer my question. But since I posted the question, I have since learnt the dangers of doing what I am trying to do. So I know this answer is posted with my best interests in mind. And is quite informative. However I would like to know if I can SSH into my Pi right now with the current setup. – Meeky333 Feb 07 '20 at 17:06
  • @Charemer From reading the OP's comment above, you are correct- he's not au fait with SSH. I implied from his comments he was. Anyhoo, my bad. – F1Linux Feb 07 '20 at 17:09
  • @Meeky333 OK, I updated my answer with SSH specific examples of what the connections look like under each option. – F1Linux Feb 07 '20 at 17:10
  • 1
    @Meeky333 If you are comfortable working with the pi headless you should be comfortable working a self-managed VPS slice. The smallest ones (~$5/month, which is more than a dynamic DNS provider) are fine for running a VPN. Try setting one up using openVPN on a pi first, see how that goes, then decide. – goldilocks Feb 07 '20 at 21:04
  • Thank you for this very detailed response. The short answer got be connected nice and quickly. The long answer has given me a good idea on how I should be setting this up in future to stay secure. For now I will close the port and mark this response as my answer. – Meeky333 Feb 10 '20 at 12:25
  • 1
    @Meeky333 In IT, knowing ***why*** to do something is equally as important as ***how*** to do something. Never hurts to understand your options to achieve a desired outcome. Very happy to help! – F1Linux Feb 10 '20 at 13:41