How to connect through SSH to a remote device that is not in the same network

stats con chris
2023-03-14
0

...

We show the steps to follow in Linux to connect through SSH to a remote device that is not in the same network.

We show the steps to follow in Linux to connect through SSH to a remote device that is not in the same network.

We are going to consider two devices that are not part of the same network. For example, one laptop in your office and one laptop in your home. In this case, in contrast to the example given in the article: "How to connect through SSH to a device that is in the same network," both devices are connected to different routers. In Fig. 1 we depict these devices. We call them office and hp.

Fig. 1: Devices in different networks.

Our goal is to connect to hp using our office device. Notice that hp has two users, ortiz and guest, then we are going to connect to each of them. A summary of this article with some extra info appears in the following video:

The steps to follow are:

1. Install SSH server

You have to have admin rights to install SSH server. Install it in the device that you want to access remotely. In our example it is the hp device and the user with admin rights is ortiz. Then, in its terminal type:

sudo apt install openssh-server

If you think you already installed openssh-server, then you can check the version by typing:

sudo dpkg -l openssh-server

Further details are given in the article: "How to connect through SSH to a device that is in the same network."

2. Set port forwarding

Type the following in the terminal:

ip route show default
default via 192.168.178.1 dev wlp60s0 proto dhcp metric 600

This shows your router IP. In this example it is 192.168.178.1. Type this IP in your browser (Chrome, Firefox, Safari, Edge, or any other). It gives you access to your router login page, see Fig. 2.

Fig. 2: Router login page.

To log in, type the password of your router. Keep in mind that this password is different from the password that you use to connect to the internet. Both are likely printed on the back of your router. If you cannot find it, check the documents that came with your router or contact the company that gave it to you. Once you type the password you have access to the dashboard of your router. In the dashboard you need to look for the option port forwarding. Because every router is different I cannot guide you to find this option in yours. In this example, it is in the section port releases, see Fig. 3.

Fig. 3: Port releases.

As you can see, the IPv4 of this router is 87.123.243.210. Notice that your router has two IPv4s, or IPs for short, an internal one (192.168.178.1) that is accessed locally (by devices in the local network), and an external one (87.123.243.210) that is accessed globally (by devices on the internet). In the following page: whatismyipaddress.com, you can track the location of any router knowing its external IPv4. In Fig. 3, to add the hp device, click on Add device for sharing. The setup is given in Fig. 4.

Fig. 4: Device setup.

As seen, the local IPv4 address of the hp device is 192.168.178.31. In Fig. 4 we have chosen to release the device via IPv4. There is also the option to choose IPv6 (Internet Protocol version 6), but we will stick to IPv4 because it is commonly used in the world. Once the device is set up, we click on New release to configure port forwarding, see Fig. 5.

Fig. 5: Port forwarding setup.

In our case, the available external IPv4 ports are 1132 and 1151, We choose 1132. We set an SSH connection, which uses the protocol TCP. We also set the internal port to 22 (the port to device ranges from 22 to 22), which is the default SSH port value. In this sense any information arriving to port 1132 in the router is forwarded to port 22 in the hp device. In your case you may not need to add all this as your router may be configured to always work with default values; i.e., any information arriving to port 22 in the router will be forwarded to port 22 in the device. Once this is set we click OK.

Fig. 6: Port forwarding is active.

As seen in Fig. 6, in the port releases section, port forwarding is now active. You can test that it is active in the following page: portchecktool.com. The output should show a success message, see Fig. 7.

Fig. 7: Testing port forwarding.

If it is not successful you may have a firewall preventing this.

3. SSH from a remote device

To connect remotely to ortiz@hp, in the office terminal type:

ssh 87.123.243.210 -p 1132

The password of ortiz@hp is requested. If you type the correct password you will be connected to ortiz@hp. To go back to ortiz@office type Ctrl+D. Similarly, to connect to guest@hp, type the following:

ssh guest@87.123.243.210 -p 1132

The password of guest@hp is requested. If you type the correct password you will be connected to guest@hp. Notice that in this second case we specify the username. In the first case we didn't do it because office and hp shared the same name (ortiz).

To conclude, it is worth mentioning that if you connect to your home router, it is likely that it uses a dynamic IP, that is, the IP number changes over time. Therefore, to avoid checking what the current IP number is, it is advisable to consider a DNS (Domain Name System), which will be fixed no matter if the IP changes. To configure it, I invite you to read the article: "How to configure a free domain name for a dynamic IP." Moreover, to avoid continually typing the remote user's password, you can consider the public key authentication, which is explained in the article: "How to connect through SSH with passwords: The public key authentication."

Views: 1

stats con chris

A writer who learned to add

A writer who learned to add

Notifications

Receive the new articles in your email

2022 © nepy

One single breath

Configure

Choose your own style

Color

Choose the perfect color mode for you


Navigation Position

Select a suitable navigation system


Vertical Navbar Style

Switch between styles for your vertical navbar

Customize