SSH:未按预期使用配置文件

LeG*_*GEC 2 ssh 16.04

我最近从 Trusty (14.04) 更新到 Xenial (16.04),现在我在使用 ssh 和我的.ssh/config文件时有不同的行为。

以前:在配置文件中,当第一个规则应用于主机a以将目标Hostname从更改ab,另一个规则应用于 时b,将应用第二个规则。

现在:仅应用第一条规则,忽略第二条规则。

以下是这种新行为的示例:

# I added two extra names for "127.0.0.1" :
legec@Workstation[.ssh]$ head -2 /etc/hosts
127.0.0.1   localhost
127.0.0.1   foo foo.homesweethome.com

# config file :
legec@Workstation[.ssh]$ cat ~/.ssh/config
# this rule expands "foo" to "foo.homesweethome.com"
Host foo
    Hostname foo.homesweethome.com

# this rule sets default port and user for "foo.homesweethome.com" :
Host foo.homesweethome.com
    Port 2222
    User foobar

# when running ssh :    
legec@Workstation[.ssh]$ ssh -v foo
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
  # as you can see in the following two lines,
  # the first config rule is applied
debug1: Reading configuration data /home/legec/.ssh/config
debug1: /home/legec/.ssh/config line 1: Applying options for foo
  # the second rule is skipped
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to foo.homesweethome.com [127.0.0.1] port 22.
debug1: Connection established.

  [.. extra debug messages about possible keys to present, protocol setup ... ]

debug1: Next authentication method: password
# I was hoping to see a connection on port 2222,
# and asking the password for user foobar :
legec@foo.homesweethome.com's password: 
Run Code Online (Sandbox Code Playgroud)

我的 ssh 库的当前版本是:

legec@Workstation[.ssh]$ ssh -V
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
Run Code Online (Sandbox Code Playgroud)

[编辑]:正如fkraeim提醒我的那样,OpenSSH在 Trusty (14.04) 中的版本是 6.6 。

问题

  • ssh 的行为发生了什么变化?
  • 如何在上面的示例中同时应用这两个规则?

注意:对于更完整的上下文,在我的真实配置文件中,配置如下所示:

Host bar baz
    Hostname %h.homesweethome.com

Host foo
    Hostname foo.homesweethome.com

Host *.homesweethome.com
    User foobar
    Port 2222
Run Code Online (Sandbox Code Playgroud)

fkr*_*iem 7

这是一个很好的例子,有时,一个人的错误是另一个人的特征......

OpenSSH 在 14.04 中的这种行为实际上是OpenSSH 6.6(这是 Ubuntu 14.04 中的版本)中引入的一个错误,并在 6.8 中修复(另请参阅更改日志)。做你想做的正确方法是

Host bar baz
    Hostname %h.homesweethome.com

Host foo
    Hostname foo.homesweethome.com

Host foo bar baz *.homesweethome.com
    User foobar
    Port 2222
Run Code Online (Sandbox Code Playgroud)

或者,也许规范化真的是你想要的......例如

CanonicalizeHostname yes
CanonicalDomains homesweethome.com

Host *.homesweethome.com
    User foobar
    Port 2222
Run Code Online (Sandbox Code Playgroud)

...可以为你工作。与您当前配置的不同之处在于,例如,如果goo.homesweethome.com存在,ssh goo将尝试连接到它。