如何将具有相同配置的多台计算机添加到〜/ .ssh / config?
说,我的~/.ssh/config
有一些行:
Host machine1 User user HostName machine1 ProxyCommand ssh server nc %h %p 2> /dev/null
这个工作正常,但问题是我有很多machines
: machine1, machine2, machine3, ...
所以如何设置所有这些机器而无需手动复制相同类型的线路
你可以这样做:
Host machine1 machine2 machine3 User user ProxyCommand ssh server nc %h %p 2>/dev/null
您只需要在Host
行中列出主机,用空格分隔,如果它与您在Host
行中给出的名称没有区别,则可以省略HostName
。 请参阅ssh config·U&L中的多个类似条目 。
为了简化它,还有通配符*
和?
可用的通常意义,所以Host machine?
对你的例子来说是可能的。
如果您的主机名符合模式,则可以使用SSH的模式:
您可以在~/.ssh/config
使用模式。 从man ssh_config
:
PATTERNS A pattern consists of zero or more non-whitespace characters, '*' (a wildcard that matches zero or more characters), or '?' (a wildcard that matches exactly one character). For example, to specify a set of declarations for any host in the “.co.uk” set of domains, the following pattern could be used: Host *.co.uk The following pattern would match any host in the 192.168.0.[0-9] network range: Host 192.168.0.?
所以,如果你想代理*.example.com
所有内容,那么在你的~/.ssh/config
,放入:
Host *.example.com User user ProxyCommand ssh server nc %h %p 2> /dev/null
或者,使用ssh
自己的选项,可以避免使用netcat:
Host *.example.com User user ProxyCommand ssh -qW %h:%p server
来自man ssh
:
-W host:port Requests that standard input and output on the client be forwarded to host on port over the secure channel. Implies -N, -T, ExitOnForwardFailure and ClearAllForwardings.
Host machine* User user ProxyCommand ssh server nc %h %p 2> /dev/null
是的,主机名是多余的。