Jam*_*ell 8 ssh openssh ssh-config
我有一个通过 ssh 连接的主机。有时我在同一个网络内,可以直接通过 ssh 进入它,有时我在它之外,我需要使用代理。
因为通过代理服务器的 ssh 比直接慢得多,我想设置我的 ssh 配置,以便我尝试直接连接,如果失败则回退到代理。
目前配置如下:
Host proxy_server
User user
Port port
Hostname some_domain
Host target_host
User user
Port port
Hostname ip_addr_of_host
Match exec not_inside_network
ProxyCommand ssh -W %h:%p proxy_server
Run Code Online (Sandbox Code Playgroud)
该target_host条目是我的配置文件中的最后一个条目,但not_inside_network会被与配置文件中不相关服务器的任何 ssh 连接调用。我怎样才能Match只适用于这台服务器?
Bow*_*Red 12
Match与 相当Host。它不像其他选项那样作为 Host 的子集存在。
但是您可以在一个匹配项上指定多个条件,它们似乎作为一个短路 AND 操作。所以这对你来说应该是可能和有用的:
Match host target_host exec not_inside_network
ProxyCommand ssh -W %h:%p proxy_server
Run Code Online (Sandbox Code Playgroud)
将在每个 ssh 上检查此规则。但是对于与“target_host”不匹配的主机,匹配会立即失败并移动到下一个Match或Host关键字(如果有)。只有当主机是“target_host”时才会发生 exec。然后该语句的真实性将确定是否调用了 ProxyCommand。
要查看发生的逻辑,请使用 -vvv 运行。您应该会在 debug3 处看到一些匹配检查。