fredrik
fredrik

Reputation: 10281

Can I combine these SSH tunneling commands into one command?

I have a two step solution to access a certain server via SSH:

Step 1, in bash:

ssh -L 127.0.0.1:5000:server2.com:22 server1.com

Step 2, in a new bash session:

ssh -P 5000 127.0.0.1  # This gets me into server2.com

Q1: Is there any way I can combine these two commands into one ssh command, and...
Q2: can I set up one single host entry in my ~/.ssh/config for this connection (allowing me to just type e.g. ssh my-tunnel)?

I suppose this comes down to chaining the hosts in some way. I am new to this and can't quite figure this out...

Upvotes: 3

Views: 1589

Answers (1)

Maxim Sagaydachny
Maxim Sagaydachny

Reputation: 2218

I came accross this question and was surprised by the fact that ssh supports jump hosts.

You can use single command to connect to the target server while ssh will take care about intermediate hop.

ssh -J server1.com server2.com

-J [user@]host[:port] Connect to the target host by first making a ssh connection to the jump host and then establishing a TCP forwarding to the ultimate destination from there. Multiple jump hops may be specified separated by comma characters. This is a shortcut to specify a ProxyJump configuration directive

And here is the corresponding jump host configuration for SSH config:

Host jumphost
    Hostname server1.com
    User $YOUR_USERNAME
    Port 22
Host my-tunnel
    Hostname server2.com
    User $YOUR_USERNAME
    Port 22
    ProxyJump jumphost

...enabling the command: ssh my-tunnel

Upvotes: 4

Related Questions