大网数据广告活动
服务器运维问答

How to set SSH config to grab part of ssh hostname and use as variable?

· 52CE 我爱测

I'm SSH'ing into multiple datacenters that are setup identically. Each datacenter has a jumpbox, and behind that there's access to various db servers etc. It's a pain to manually specify entries for all these servers, when they'r…

问题原文

原帖链接 · Jeff Widman · CC BY-SA 3.0

I'm SSH'ing into multiple datacenters that are setup identically. Each datacenter has a jumpbox, and behind that there's access to various db servers etc.

It's a pain to manually specify entries for all these servers, when they're almost identical across datacenters, other than the jumpbox IP. So I'd like to set my .ssh/config file so that I can type datacenter_name-server_behind_jumpbox and have SSH grab the datacenter_name off the front of the host (so that I can re-use it in the ProxyCommand.

For example, if I want to access the Maria server in datacenter1, I'd create an SSH entry for the jumpbox called datacenter1. Then for the Maria server behind it, I'd setup something like:

Host %dc%-maria
    Hostname maria
    User jeff
    ProxyCommand ssh -q -W %h:%p %dc

That way I only need to setup a single entry for each type of server that's located behind the jumpbox, and it automatically knows which datacenter jumpbox to hit.

Is something like this possible?

社区高分解答

原帖链接 · mbrandeis · CC BY-SA 3.0

I add a fake suffix to the hostname with different proxy hosts

Host *.dc1
    ProxyCommand ssh -q %r@dc1 -W %h:%p

Host *.dc2
    ProxyCommand ssh -q %r@dc2 -W %h:%p

Then do something like ssh server1.dc1 and it will use the proxy host.

You can add Host entries for custom settings like this:

Host server1.*
    User jeff

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。