【问题标题】:How to do other "stuff" in the same terminal where you establish an SSH tunnel如何在建立 SSH 隧道的同一终端中执行其他“操作”
【发布时间】:2015-12-29 14:36:47
【问题描述】:

我经常使用 ssh 隧道。我打开一个终端来创建隧道(例如ssh -L 1111:servera:2222 user@serverb)。然后我打开一个新终端来完成我的工作。有没有办法在终端中建立隧道并以某种方式将其置于后台,这样我就不需要打开新的终端?我试着把“&”放在最后,但这并没有奏效。在我输入密码之前,隧道就进入了后台。然后我做了fg,输入了密码,卡在了ssh会话中。

我知道一种可能的解决方案是使用 screen 或 tmux 或类似的东西。我缺少一个简单的解决方案吗?

【问题讨论】:

标签: ssh ssh-tunnel


【解决方案1】:

-f-N 选项正是为此而生:

-f     Requests ssh to go to background just before command execution.  This is useful if
       ssh is going to ask for passwords or passphrases, but the user wants it in the
       background. This implies -n. The recommended way to start X11 programs at a remote
       site is with something like ssh -f host xterm.

       If  the  ExitOnForwardFailure  configuration option is set to ``yes'', then a client 
       started with -f will wait for all remote port forwards to be successfully established
       before placing itself in the background.

-N     Do not execute a remote command.  This is useful for just forwarding ports
       (protocol version 2 only).

所以完整的命令是ssh -fNL 1111:servera:2222 user@serverb

防止 ssh 询问密码的一种方法是使用SSH public keys 与代理进行身份验证,该代理要么保存密码,要么使用外部图形程序(如 pinentry)提示密码。

查看autossh 也可能对您有用,如果连接断开,它将自动重新连接您的 SSH。

【讨论】:

  • 我试了ssh -f -L 1234:servera:5678 user@serverb,得到了Cannot fork into background without a command to execute.
  • 是的,我一直懒得设置密钥。还没有努力学习如何做到这一点。 autossh 看起来很有趣。
  • 添加-N 参数来解决这个问题。
猜你喜欢
  • 2016-03-27
  • 1970-01-01
  • 2015-05-05
  • 2020-09-14
  • 2020-08-25
  • 2018-05-02
  • 2020-06-21
  • 2019-03-18
  • 2023-04-02
相关资源
最近更新 更多