Ssh – How to set up SSH over the internet

port-forwardingssh

I think this type of questions were answered before but due to lack of networking knowledge, for a couple of years I searched the internet and read many websites in an attempt to connect to an SSH server over the internet. But I have failed. I want to know if it is possible to connect two systems using SSH without using third party services like ngrok.

I have two computers running ArchLinux.
Computer A and B are inside a local network but I want to set up the ssh so that I can access A from B via SSH over the internet (like how Anydesk, Teamviewer and other such remote desktop software connects).

Systems Details:
1. The systems are connected to a [Digisol] router.
2. Both have openssh package installed. System A is running sshd. A can get connected to B over the LAN.

Hostname of system A: archlinux-pc

Hostname of system B: archlinux

On both the systems the username is sourav. That is, I want to connect to sourav on archlinux-pc.

  1. Set up Virtual Server configuration on the router (Digisol):
Current Virtual Server Forwarding Table
ServerName  Protocol    Local IP Address    Local Port  WAN Port    State   Action
SSH     tcp     192.168.2.9     22-22   22-22   Enable  
  1. My public IP is xx.xxx.171.113 as reported by ident.me. The IP doesn't change in a period of many months.

whatismyipaddress.com shows the same IPv4 but IPv6: Not detected

My router has a section called WAN Configuration which shows some different IP:

WAN Configuration
Interface   Protocol    IP Address  Gateway     DNS     Status
WAN     PPPoE   xx.xxx.18.27    xx.xxx.0.1  xxx.xxx.91.2 8.8.8.8 xxx.xxx.88.2   Link Up(PPPoE)

I have already tried using ssh sourav@xx.xxx.171.113 from system B, which takes a long time but doesn't connect at all.

I have also tried these links:

https://raspberrypi.stackexchange.com/questions/13861/setting-up-ssh-over-internet-on-my-pi

https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding


How do I get connected to system A from system B using SSH without any other services like ngrok if something like that is possible? Also, can I connect to system A over SSH using termux running on my Android phone?

Best Answer

Assuming both systems are on the same local network, behind the same router:

Connecting to sourav@xxx.xxx.171.113 will get you to 192.168.2.9

from there you can ssh to the next system, using it's local address (presumably 192.168.2.X)

If you want to access it externally, you have to add an additional port forward:

ServerName  Protocol    Local IP Address    Local Port  WAN Port    State   Action
       SSH       tcp         192.168.2.9         22-22     22-22    Enable  
      SSH2       tcp         192.168.2.X         22-22 63000-63000  Enable  

Now you should be able to connect to sourav@xxx.xxx.171.113 port 63000 and reach the other system. It is good to move to a non default SSH port to lower the amount of brute force login attempts that you are going to receive from having your service available like this.