Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

ssh tunnel - 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...