如何重用 ssh 连接 [英] How to reuse an ssh connection

查看:45
本文介绍了如何重用 ssh 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小脚本来更新一些远程服务器(2+)
我正在与每个服务器建立多个连接;有没有办法可以重复使用 SSH 连接,这样我就不必一次打开太多?

I'm creating a small script to update some remote servers (2+)
I am making multiple connects to each server; is there a way I can reuse the SSH connections so I don't have to open too many at once?

推荐答案

如果用-M打开第一个连接:

If you open the first connection with -M:

ssh -M $REMOTEHOST

$REMOTEHOST 的后续连接将捎带"在由主ssh 建立的连接上.最值得注意的是,不需要进一步的身份验证.有关详细信息,请参阅ControlMaster"下的 man ssh_config.使用-S指定共享socket的路径;我不确定默认值是什么,因为我使用配置文件来配置连接共享.

subsequent connections to $REMOTEHOST will "piggyback" on the connection established by the master ssh. Most noticeably, further authentication is not required. See man ssh_config under "ControlMaster" for more details. Use -S to specify the path to the shared socket; I'm not sure what the default is, because I configure connection sharing using the configuration file instead.

在我的 .ssh/config 文件中,我有以下几行:

In my .ssh/config file, I have the following lines:

host *
  ControlMaster auto
  ControlPath ~/.ssh/ssh_mux_%h_%p_%r

这样,我就不用记得用-M-Sssh 确定主机/端口/用户名组合是否已存在可共享连接,并在可能的情况下使用该连接.

This way, I don't have to remember to use -M or -S; ssh figures out if a sharable connection already exists for the host/port/username combination and uses that if possible.

该选项自 2004 年起在 OpenSSH 中可用.

This option is available in OpenSSH since 2004.

这篇关于如何重用 ssh 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆