后台 SSH 隧道(端口转发)

AKM*_*adi 5 linux ssh ssh-tunneling port-forwarding background-process

我有一个命令将端口从我的计算机转发到服务器,如下所示:

ssh -L 8000:localhost:8888 myserver.com
Run Code Online (Sandbox Code Playgroud)

我想在后台运行这个命令。我不需要输入用户名和密码,因为我已经设置了公钥。

我尝试&在末尾添加,如下所示: ssh -L 8000:localhost:8888 myserver.com& 但是,我收到以下错误:

[1]  + 30825 suspended (tty output)
Run Code Online (Sandbox Code Playgroud)

我也尝试过nohup,如下:

nohup ssh -L 8000:localhost:8888 myserver.com&
exit
Run Code Online (Sandbox Code Playgroud)

但端口没有转发。

最后我尝试了一下ssh -f,如下:

ssh -L 8000:localhost:8888 myserver.com
Run Code Online (Sandbox Code Playgroud)

并且,我收到以下错误:

Cannot fork into background without a command to execute.
Run Code Online (Sandbox Code Playgroud)

我的目标是在后台保持 ssh 连接处于活动状态,而不保持终端打开。有什么帮助吗?

roa*_*ima 10

您提到了ssh -f,这是正确的,但您错过了-N,这是拼图的剩余部分:

-N不要执行远程命令。这对于转发端口很有用。[...]

很近!尝试这个

ssh -fN -L 8000:localhost:8888 myserver.com
Run Code Online (Sandbox Code Playgroud)

  • 我怎样才能阻止这条隧道呢? (2认同)