LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-25-2023, 09:01 PM   #1
Daedra
Senior Member
 
Registered: Dec 2005
Location: Springfield, MO
Distribution: Slackware64-15.0
Posts: 2,690

Rep: Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376
Question about SSH and Port Forwarding


Just curious on what you guys think the "Best Practice" is in this case.

Lets say I have 5 machines on my network
Main - 192.168.1.50
VM1 - 192.168.1.51
VM2 - 192.168.1.52
VM3 - 192.168.1.53
VM4 - 192.168.1.54

To access my network via ssh from the outside I set my router to port forward my SSH port to the main machine. Everything works, but lets say I want to access the other machines via ssh. I know that I can set a different ssh port on each machine and forward that port to what ever specific machine I want. I also could ssh into my Main machine and then ssh into the others from there, I guess thats called "nested ssh". I have done both of these methods, but I was just wondering what the best practice is in this type of situation?

Thanks

Last edited by Daedra; 02-25-2023 at 09:02 PM.
 
Old 02-25-2023, 09:52 PM   #2
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 415
Blog Entries: 2

Rep: Reputation: Disabled
ProxyCommand via ssh, stick it in your private ssh_config (from a Linux client). It starts a ssh session and tunnels it to the real remote machine.
 
Old 02-25-2023, 11:12 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by elgrandeperro View Post
ProxyCommand via ssh, stick it in your private ssh_config (from a Linux client). It starts a ssh session and tunnels it to the real remote machine.
Close. In recent years, it would be easier to use ProxyJump for that. It can be accessed via the -J option for one-off sessions, or placed in ~/.ssh/config for permanence. So, for example, connecting from the outside you connect via your router (203.0.113.224) to your main computer to one of your VMs (192.168.1.53)

Code:
ssh -J 203.0.113.224 daedrea@192.168.1.53
Or in ssh_config it could look like this. with the IPv4 address for "main" being set to your router's IPv4 address:

Code:
Host main
    HostName 203.0.113.224
    IdentityFile /home/%u/.ssh/main.ed25519

Host vm1
    HostName 192.168.1.51
    IdentityFile /home/%u/.ssh/vm1.ed25519

Host vm2
    HostName 192.168.1.52
    IdentityFile /home/%u/.ssh/vm2.ed25519

# etc

Host vm?
    ProxyJump main

Host *
    IdentitiesOnly yes
    ServerAliveCountMax 3
    ServerAliveInterval 15
With that, you could be outside your LAN and then type "ssh vm2" to pass through Main to VM2, with the connections being made automatically.

It will work for IPv6 addresses, too.

Because the settings are chosen on a first-match basis, the specific setting go first and progressively more general settings later.

If you want to get really fancy a Match block can be used to determine whether you are on the LAN or outside and only use ProxyJump outside the LAN.

Last edited by Turbocapitalist; 02-25-2023 at 11:14 PM.
 
Old 02-25-2023, 11:16 PM   #4
Daedra
Senior Member
 
Registered: Dec 2005
Location: Springfield, MO
Distribution: Slackware64-15.0
Posts: 2,690

Original Poster
Rep: Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376Reputation: 1376
Quote:
Originally Posted by Turbocapitalist View Post
Close. In recent years, it would be easier to use ProxyJump for that. It can be accessed via the -J option for one-off sessions, or placed in ~/.ssh/config for permanence. So, for example, connecting from the outside you connect via your router (203.0.113.224) to your main computer to one of your VMs (192.168.1.53)

Code:
ssh -J 203.0.113.224 daedrea@192.168.1.53
Or in ssh_config it could look like this. with the IPv4 address for "main" being set to your router's IPv4 address:

Code:
Host main
    HostName 203.0.113.224
    IdentityFile /home/%u/.ssh/main.ed25519

Host vm1
    HostName 192.168.1.51
    IdentityFile /home/%u/.ssh/vm1.ed25519

Host vm2
    HostName 192.168.1.52
    IdentityFile /home/%u/.ssh/vm2.ed25519

# etc

Host vm?
    ProxyJump main

Host *
    IdentitiesOnly yes
    ServerAliveCountMax 3
    ServerAliveInterval 15
With that, you could be outside your LAN and then type "ssh vm2" to pass through Main to VM2, with the connections being made automatically.

It will work for IPv6 addresses, too.

Because the settings are chosen on a first-match basis, the specific setting go first and progressively more general settings later.
Excellent! Thank you sir, and in my first post when I said from the outside I should of specified via the internet, in case that wasn't clear. So for accessing from the internet do I use WAN IP with the -J option
 
Old 02-25-2023, 11:36 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by Daedra View Post
So for accessing from the Internet do I use WAN IP with the -J option
Yes, you would point -J at the external IP address for your router when you are connecting from outside your LAN.

The shortcuts will only work from outside the LAN as there are above, because most routers don't support loopback NAT aka NAT hairpinning. If you wish to keep the same shortcuts, you'll have to throw in a quick check for whether you are on the LAN or not. Use a Match block to test if you are on your LAN. The test condition can be anything, even a Perl script, but it has to figure out whether you are on your LAN, not just any other LAN.
 
Old 02-25-2023, 11:40 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Here is a guess, untested. See the link in the previous post.

Code:
Host main
    HostName 203.0.113.224
    IdentityFile /home/%u/.ssh/main.ed25519

Host vm1
    HostName 192.168.1.51
    IdentityFile /home/%u/.ssh/vm1.ed25519

Host vm2
    HostName 192.168.1.52
    IdentityFile /home/%u/.ssh/vm2.ed25519

Match originalhost vm? !exec "somescript-to-test-which-network-you-are-on"
    ProxyJump main

Host *
    IdentitiesOnly yes
    ServerAliveCountMax 3
    ServerAliveInterval 15

Last edited by Turbocapitalist; 02-25-2023 at 11:46 PM. Reason: originalhost
 
Old 02-26-2023, 03:05 AM   #7
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 4,461
Blog Entries: 7

Rep: Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561
Question about SSH and Port Forwarding

I use OpenVPN for this. Then I can ssh to every machine at the office from home.

It's only one port to forward, and it supports other kinds of traffic too. Eg: RDP to Windows machines.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] IPtables : ssh port forwarding one port to another port issue routers Linux - Networking 7 08-07-2018 08:41 AM
Redirec port in device eth0 to port+ip in device wlan0 ( port forwarding on hostpd wireless network) MattFly Linux - Networking 2 08-28-2016 07:21 PM
Port number used by server when using dynamic port forwarding in SSH? kreeder Linux - Networking 4 11-21-2011 02:07 PM
Shorewall: port forwarding problem, port is closed even after forwarding Synt4x_3rr0r Linux - Networking 2 12-13-2009 04:36 PM
IPCHAINS port forwarding and IPTABLES port forwarding ediestajr Linux - Networking 26 01-14-2007 07:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration