如何连接到以 -f -N 为背景的 SSH 会话?

Kev*_*vin 7 linux ssh unix shell ssh-tunnel

我正在做一些需要使用代理链关闭机器的工作,所以我要连接到系统并像这样绑定本地端口......

ssh -f -N -D 9000 user@host.com
Run Code Online (Sandbox Code Playgroud)

...打开连接后,我会返回到我的命令提示符。然后我可以使用代理链之类的东西通过host.com.

我的问题是:如何与同一个会话连接/交互,以便在 host.com 上获得远程 shell?我现在的做法是用一个简单的 ssh 会话打开另一个 ssh 会话ssh user@host.com,但我认为必须有一种方法来利用我打开的第一个会话。

use*_*686 11

If it wasn't created with multiplex enabled, you cannot.

Next time, start the background session like this:

ssh -f -N -M -S ~/.ssh/S.user@host.com -D 9000 user@host.com
Run Code Online (Sandbox Code Playgroud)

Here -M enables multiplex master mode, and -S sets the socket path.

Now you can use ssh -S ~/.ssh/S.user@host.com dummyhost to open a second session over the same connection. It's possible to control the master by giving -O exit, -O check, and various other options. (Sadly, -O forward is a very recent addition.)

To make this automatic, you can use the following options in .ssh/config:

Host *
    ControlPath ~/.ssh/S.%l.%r@%h:%p
    ControlMaster auto
    ControlPersist 10m
Run Code Online (Sandbox Code Playgroud)

Setting ControlPath means you do not need to specify -S every time.

Setting ControlMaster auto means that every new connection will automatically continue in background as a multiplex master. (Without it, you can still start new masters with ssh -fNM host).

设置ControlPersist 10m意味着当它们没有任何活动会话时,自动母版将停留 10 分钟。(这是最近添加的选项。)

请注意,通过多路复用连接进行批量传输将导致交互式会话变得非常缓慢......


dav*_*dgo 0

保留 -f(后台 SSH)和 -N(不执行远程命令)参数,因此使用

ssh -D 9000 user@host.com