我怎么能阻止 ssh 提供错误的密钥?

Alv*_*eda 39 ssh identity-management gitolite

(这是ssh的问题,不是gitolite)

我已经在我的家庭服务器(ubuntu 12.04 服务器,open-ssh)上配置了 gitolite。我想要一个特殊的身份文件来管理存储库,因此我需要使用两个不同的身份密钥通过 ssh 访问我自己的主机。

这是我的 .ssh/config 文件的内容:

Host gitadmin.gammu.com
User            git
IdentityFile    /home/alvaro/.ssh/id_gitolite_mantra

Host git.gammu.com
User            git
IdentityFile    /home/alvaro/.ssh/id_alvaro_mantra
Run Code Online (Sandbox Code Playgroud)

这是我的主机文件的内容:

# Git
127.0.0.1      gitadmin.gammu.com
127.0.0.1      git.gammu.com
Run Code Online (Sandbox Code Playgroud)

所以我应该能够通过这种方式与 gitolite 通信以访问“正常”帐户:

$ssh git.gammu.com 
Run Code Online (Sandbox Code Playgroud)

以及通过管理帐户访问的这种方式:

$ssh gitadmin.gammu.com
Run Code Online (Sandbox Code Playgroud)

当我尝试使用普通帐户访问时,一切正常:

alvaro@mantra:~/.ssh$ ssh git.gammu.com
PTY allocation request failed on channel 0
hello alvaro, this is gitolite 2.2-1 (Debian) running on git 1.7.9.5
the gitolite config gives you the following access:
    @R_ @W_    testing
Connection to git.gammu.com closed.
Run Code Online (Sandbox Code Playgroud)

当我对管理帐户执行相同操作时:

alvaro@mantra:~$ ssh gitadmin.gammu.com
PTY allocation request failed on channel 0
hello alvaro, this is gitolite 2.2-1 (Debian) running on git 1.7.9.5
the gitolite config gives you the following access:
    @R_ @W_    testing
Connection to gitadmin.gammu.com closed.
Run Code Online (Sandbox Code Playgroud)

它应该显示管理存储库。如果我使用详细选项启动 ssh:

ssh -vvv gitadmin.gammu.com 
...
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/alvaro/.ssh/id_alvaro_mantra (0x7f7cb6c0fbc0)
debug2: key: /home/alvaro/.ssh/id_gitolite_mantra (0x7f7cb6c044d0)
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/alvaro/.ssh/id_alvaro_mantra
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
...
Run Code Online (Sandbox Code Playgroud)

它提供了关键的 id_alvaro_mantra,它不应该!!

当我使用 -i 选项指定密钥时,也会发生同样的情况:

ssh -i /home/alvaro/.ssh/id_gitolite_mantra -vvv gitadmin.gammu.com
...
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/alvaro/.ssh/id_alvaro_mantra (0x7fa365237f90)
debug2: key: /home/alvaro/.ssh/id_gitolite_mantra (0x7fa365230550)
debug2: key: /home/alvaro/.ssh/id_gitolite_mantra (0x7fa365231050)
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/alvaro/.ssh/id_alvaro_mantra
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug2: input_userauth_pk_ok: fp 36:b1:43:36:af:4f:00:e5:e1:39:50:7e:07:80:14:26
debug3: sign_and_send_pubkey: RSA 36:b1:43:36:af:4f:00:e5:e1:39:50:7e:07:80:14:26
debug1: Authentication succeeded (publickey).
...
Run Code Online (Sandbox Code Playgroud)

发生了什么?我错过了一些东西,但我找不到什么。

这些是我家目录的内容:

-rw-rw-r--  1 alvaro alvaro  395 nov 14 18:00 authorized_keys
-rw-rw-r--  1 alvaro alvaro  326 nov 21 10:21 config
-rw-------  1 alvaro alvaro  137 nov 20 20:26 environment
-rw-------  1 alvaro alvaro 1766 nov 20 21:41 id_alvaromaceda.es
-rw-r--r--  1 alvaro alvaro  404 nov 20 21:41 id_alvaromaceda.es.pub
-rw-------  1 alvaro alvaro 1766 nov 14 17:59 id_alvaro_mantra
-rw-r--r--  1 alvaro alvaro  395 nov 14 17:59 id_alvaro_mantra.pub
-rw-------  1 alvaro alvaro  771 nov 14 18:03 id_developer_mantra
-rw-------  1 alvaro alvaro 1679 nov 20 12:37 id_dos_pruebasgit
-rw-r--r--  1 alvaro alvaro  395 nov 20 12:37 id_dos_pruebasgit.pub
-rw-------  1 alvaro alvaro 1679 nov 20 12:46 id_gitolite_mantra
-rw-r--r--  1 alvaro alvaro  397 nov 20 12:46 id_gitolite_mantra.pub
-rw-------  1 alvaro alvaro 1675 nov 20 21:44 id_gitpruebas.es
-rw-r--r--  1 alvaro alvaro  408 nov 20 21:44 id_gitpruebas.es.pub
-rw-------  1 alvaro alvaro 1679 nov 20 12:34 id_uno_pruebasgit
-rw-r--r--  1 alvaro alvaro  395 nov 20 12:34 id_uno_pruebasgit.pub
-rw-r--r--  1 alvaro alvaro 2434 nov 21 10:11 known_hosts
Run Code Online (Sandbox Code Playgroud)

还有一堆其他的钥匙没有提供……为什么提供 id_alvaro_mantra 而不是其他的钥匙?我无法理解。

我需要一些帮助,不知道去哪里找....

ger*_*ijk 56

根据以下联机帮助页,这是预期的行为ssh_config

 IdentityFile
         Specifies a file from which the user's DSA, ECDSA or DSA authentica?
         tion identity is read.  The default is ~/.ssh/identity for protocol
         version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for
         protocol version 2.  Additionally, any identities represented by the
         authentication agent will be used for authentication.  

         [...]

         It is possible to have multiple identity files specified in configu?
         ration files; all these identities will be tried in sequence.  Mul?
         tiple IdentityFile directives will add to the list of identities
         tried (this behaviour differs from that of other configuration
         directives).
Run Code Online (Sandbox Code Playgroud)

基本上,指定IdentityFiles 只是将密钥添加到 SSH 代理已经提供给客户端的当前列表中。

尝试在.ssh/config文件底部使用以下内容覆盖此行为:

Host *
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

  • 此外,您可以在主机级别指定它,这就是我最后所做的:`Host git.gammu.com` `User git` IdentityFile /home/alvaro/.ssh/id_alvaro_mantra IdentitiesOnly yes` (3认同)
  • @AlvaroMaceda 是正确的。将 `IdentitiesOnly yes` 添加到 gitadmin.gammu.com 和 git.gammu.com `Host` 条目就足够了。您不必创建会影响其他主机的通配符条目。 (3认同)

Ale*_*eks 9

对我来说,解决方案是使用以下命令将密钥添加到 ssh 密钥列表中:

ssh-add ~/.ssh/id_name_of_my_rsa_key
Run Code Online (Sandbox Code Playgroud)

所以它可以在连接到服务器时提供。添加ssh后,自动识别正确的。

编辑:

但最近我认为更好的解决方案,也是更持久的解决方案,是去~/.ssh/config添加IdentitiesOnly yes你的配置文件,如下所示:

Host github.com
  HostName github.com
    User git
      IdentityFile ~/.ssh/id_rsa
      IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,你的第二种方法正是我应该做的。旁注:在您的示例中,HostName 过多,因为它的值等于 Host 的值,缩进超过一级是没有意义的,因为仅在 ssh_config 中按 Host 和 Match 进行分组。 (2认同)