AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 986479
Accepted
LeGEC
LeGEC
Asked: 2017-12-16 01:18:19 +0800 CST2017-12-16 01:18:19 +0800 CST 2017-12-16 01:18:19 +0800 CST

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

  • 772

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

以前:在配置文件中,当第一条规则将应用于主机a以将目标Hostname从更改a为b,并且另一条规则将应用于 时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 :
[email protected]'s password: 

我的 ssh lib 的当前版本是:

legec@Workstation[.ssh]$ ssh -V
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016

[编辑]:正如fkraeim提醒我的那样,OpenSSHin 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
16.04
  • 2 2 个回答
  • 1512 Views

2 个回答

  • Voted
  1. Best Answer
    fkraiem
    2017-12-16T01:50:24+08:002017-12-16T01:50:24+08:00

    这很好地说明了有时,一个人的错误是另一个人的特征......

    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
    

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

    CanonicalizeHostname yes
    CanonicalDomains homesweethome.com
    
    Host *.homesweethome.com
        User foobar
        Port 2222
    

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

    • 7
  2. Olorin
    2017-12-16T01:35:39+08:002017-12-16T01:35:39+08:00

    我不知道 14.04 和 16.04 之间发生了什么变化,但该CanonicalizeHostnames选项似乎发挥了作用。来自man ssh_config:

    CanonicalizeHostname
     Controls whether explicit hostname canonicalization is performed.
     The default, “no”, is not to perform any name rewriting and let
     the system resolver handle all hostname lookups.  If set to “yes”
     then, for connections that do not use a ProxyCommand, ssh(1) will
     attempt to canonicalize the hostname specified on the command
     line using the CanonicalDomains suffixes and
     CanonicalizePermittedCNAMEs rules.  If CanonicalizeHostname is
     set to “always”, then canonicalization is applied to proxied
     connections too.
    
     If this option is enabled, then the configuration files are
     processed again using the new target name to pick up any new
     configuration in matching Host and Match stanzas.
    

    注意最后一段。当我添加:

    Host *
      CanonicalizeHostname yes
    

    我得到了预期的结果:

    $ ssh foo                
    ssh: connect to host foo.homesweethome.com port 2222: Connection refused
    
    • 3

相关问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve