25

Is it possible to do the following via an SSH tunnel...

  1. Host-1 establishes an SSH connection to a Remote Server
  2. I wish to log into the Remote Server and execute commands over SSH back on Host-1

Host-1 is a device that I will not have access to directly. Host-1 is set up to automatically establish an SSH connection to a remote server via cron. At any point while Host-1 has established an SSH connection to the Remote Server, I wish to log into the Remote Server in order to perform maintenance on Host-1 via SSH.

I am looking for an example of how this would work if its possible.

Barry
  • 722
  • 1
  • 9
  • 13
  • I don't have an example of how this would work or be practical but it would be possible to do by simpliy changing ports on the second ssh conection in order to not interfere with eachother – brendosthoughts Apr 13 '13 at 03:18

1 Answers1

39

Like this:

host1$  ssh -N -R 8822:localhost:22 remote.host.com

The optional -N says "don't execute a command" (helpful to prevent accidents caused by leaving remote shells laying around.)

Now from remote, you can SSH to host1 like this: (The remote port 8822 forwards to host1, but only on the loopback interface.)

remote$ ssh -p 8822 localhost

For extra credit, you can export the forwarding to the whole world, allowing anyone get to host1 by hitting remote's port 8822. (Note the extra initial colon)

host1$  ssh -N -R :8822:localhost:22 remote.host.com
BraveNewCurrency
  • 12,654
  • 2
  • 42
  • 50
  • 1
    Exactly what I needed! I can verify that a connection was established using `ssh -p -v 8822 localhost` so I marked your answer as accepted. I don't suppose you have an idea of what is causing my next problem? debug1: Connecting to localhost [127.0.0.1] port 8822. debug1: Connection established. debug1: identity file /home/ubuntu/.ssh/id_rsa type -1 debug1: identity file /home/ubuntu/.ssh/id_rsa-cert type -1 ssh_exchange_identification: Connection closed by remote host – Barry Apr 13 '13 at 04:24
  • 1
    I figured out the solution to the **ssh_exchange_id: Connection closed by remote host** error. Execute both `chmod 700 ~/.ssh` and `chmod 600 ~/.ssh/authorized_keys` to get the permissions set correctly. – Barry Apr 13 '13 at 04:47