Can I combine these SSH tunneling commands into one command?

喜欢而已 提交于 2020-04-30 05:09:10

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/61402158/can-i-combine-these-ssh-tunneling-commands-into-one-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!