man ssh
说:
SSH_ASKPASS
If ssh needs a passphrase, it will read the passphrase from the
current terminal if it was run from a terminal. If ssh does not
have a terminal associated with it but DISPLAY and SSH_ASKPASS
are set, it will execute the program specified by SSH_ASKPASS
and open an X11 window to read the passphrase.
Run Code Online (Sandbox Code Playgroud)
我希望 SSH 使用 askpass 程序,即使它是从终端运行的。
有时,我必须连接到服务器,在那里显示密码提示会有一些延迟(可能是由于网络问题,也可能是由于尝试反向 DNS 查找,……)。我很生气并切换到其他东西,而忘记了尝试的连接。(插入关于金鱼注意力跨度的笑话。)当我终于回到它时,提示超时,即使是正确的密码也会导致连接关闭。
密钥将是一种解决方案,但并非我使用的每个系统都有我常用的 SSH 密钥。不过我一般用的是Ubuntu系统,Ubuntu默认安装了SSH askpass程序。
但是,如果弹出一个询问通行证窗口,我会立即意识到这一点。这对我来说是一个足够好的妥协,只要我能让它工作。
mku*_*ski 10
使用 OpenSSH 8.4,您可以将$SSH_ASKPASS_REQUIRE
环境变量设置为force
. 引用ssh(1)手册页:
SSH_ASKPASS_REQUIRE Allows further control over the use of
an askpass program. If this variable
is set to "never" then ssh will never
attempt to use one. If it is set to
"prefer", then ssh will prefer to use
the askpass program instead of the TTY
when requesting passwords. Finally, if
the variable is set to "force", then
the askpass program will be used for
all passphrase input regardless of
whether DISPLAY is set.
Run Code Online (Sandbox Code Playgroud)
由于 OpenSSH 8.4 于2020 年 9 月 27 日发布,您需要等待一段时间才能在任何主要 Linux 发行版中使用此功能。
这会有点复杂,但几个部分的组合将使其工作:
要强制ssh
使用$SSH_ASKPASS
程序,您不能允许ssh
看到真实的tty
. 这只是条件。这可以通过使用setsid
和使用-n
switch to 来完成ssh
。
这种情况会启动连接,但您将无法与 shell 交互,这可能也是您的要求 ;)(并且还会破坏您的本地 TTY)。
但是你可以放弃“第一次会议”。您还应该添加-N
switch,这将抑制远程命令并只进行身份验证。
&> /dev/null
如果您对它不感兴趣,也可以将可能的输出“垃圾”重定向到。
设立ControlMaster
在ssh_config
。这是一个很酷的功能,一旦建立连接,您就可以非常快地“启动”会话。这个片段~/.ssh/config
应该这样做:
ControlPath ~/.ssh/controlmasters/%r@%h:%p
ControlMaster auto
ControlPersist 5m
Run Code Online (Sandbox Code Playgroud)
您可以将其添加到host
列出您的“慢速候选人”的某个块中,或者添加到任何地方。这几乎没有开销。
然后您应该能够以这种方式连接到您预计需要一段时间的主机:
setsid ssh -nN host
# wait, insert password in the X11 prompt
ssh host
# will bring you directly to your session
Run Code Online (Sandbox Code Playgroud)
整个过程可能会通过alias
bash 函数一步完成而简化,但这留给读者想象。
您可以在命令行上将两者连接在一起,而无需ssh_config
参与:
setsid ssh -nNMS ~/.ssh/masters/%C host
# wait, insert password in the X11 prompt
ssh -S ~/.ssh/masters/%C host
# will bring you directly to your session
Run Code Online (Sandbox Code Playgroud)
当未指定 SSH 选项时,以下函数应该可以工作:
ControlPath ~/.ssh/controlmasters/%r@%h:%p
ControlMaster auto
ControlPersist 5m
Run Code Online (Sandbox Code Playgroud)
-f
指示 SSH 在程序执行之前进入后台,也就是在它获得密码之后。-w
告诉setsid
等待程序结束。在这种情况下,当 SSH 进入后台时就会发生这种情况。结合ssh -f
,可以消除两个 SSH 命令之间的手动等待。 归档时间: |
|
查看次数: |
5336 次 |
最近记录: |