
我有一個~/.ssh/config
工作正常的文件。它的格式如下:
Host *
IdentityFile ~/.ssh/id_null
TCPKeepAlive no
ServerAliveCountMax 5000
Host *.domain.com
User mydomainuser
IdentityFile ~/.ssh/id_domain
Host github.com
User gcb
IdentityFile ~/.ssh/id_domain
(它有更多的域,但現在讓我們忽略這些)
它工作得很好,甚至手冊說更具體的主機模式應該放在最上面,這樣它總是工作得很好。我總是透過 ssh 連接到這兩個網域,並且可以很好地獲取正確的身份金鑰。我從來沒有遇到過對這兩個網域嘗試使用空密鑰的問題。我用它很多年了。始終獲得正確的用戶和密鑰。我在 debian、fedora 和 osx 上都使用了相同的設定檔。
今天,在 osx 10.10(OpenSSH_6.2p2,OSSLShim 0.9.8r 8 Dec 2011)上(之前可以工作),我也嘗試添加User unkown
到*
頂部的規則。突然間,現在我嘗試的每次 ssh 都會發送 user=unkown 和identityfile=id_null,這將無法在任何地方登入。這讓我去閱讀 ssh_config 的手冊頁,並完全按照書本保留所有內容......但它仍然無法運作。我也嘗試刪除該User
條目,*
但這並沒有解決問題。
我目前的.ssh/config
文件是
Host exact.hostname.domain.com
User mydomainuser
IdentityFile ~/.ssh/id_domain
Host *.domain.com
User mydomainuser
IdentityFile ~/.ssh/id_domain
Host github.com
User gcb
IdentityFile ~/.ssh/id_domain
Host *
IdentityFile ~/.ssh/id_null
TCPKeepAlive no
ServerAliveCountMax 50
當 i 時,它仍然會嘗試使用 id_null 作為身分識別檔ssh exact.hostname.domain.com
。我還嘗試使用多種模式而不是Host *.domain.com exact.hostname.domain.com
重複。什麼都不起作用。有什麼想法有什麼問題嗎?
我怎樣才能在每個域的基礎上擁有預設值User
和覆蓋它們?IdentityFile
編輯1:我Host exact.hostname.domain.com
在之前和之後都添加了*
。並ssh -vvv
顯示:
debug1: /Users/.../.ssh/config line 1: Applying options for exact.hostname.domain.com
debug1: /Users/.../.ssh/config line 19: Applying options for *
debug1: /Users/.../.ssh/config line 37: Applying options for exact.hostname.domain.com
但我還是到了最後no such identity: .ssh/id_null: No such file or directory
編輯2:我看到的其他一些奇怪的事情ssh -vvv
它總是嘗試使用我擁有的所有鑰匙。所以我想因為它可能有一些硬編碼的預設值.ssh/id_rsa*
,所以我將所有的私鑰移至.ssh/keys/
該預設值以隱藏它,並更新了特定的Host
條目...結果,它正在嘗試來自所有主機條目的密鑰!即使是那些沒有匹配的!
回顧一下,我在文件的頂部和底部有一個特定的主機匹配(只是為了安全起見),然後是不匹配的主機,還有一個*
。
但 ssh -vvv 顯示:
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /Users/x/.ssh/keys/id_rsa_git (0x7f954b700530),
debug2: key: /Users/x/.ssh/keys/id_rsa (0x7f954b415700), explicit
debug2: key: /Users/x/.ssh/id_null (0x0), explicit
.ssh/keys/id_rsa_git
如果這是IdentityFile
下面唯一提到的,那到底為什麼要加呢Host github.com
?為什麼它最終嘗試發送,id_null
即使它Host exact.hostname.domain.com
與調試輸出中所說的明確匹配debug1: /Users/.../.ssh/config line 1: Applying options for exact.hostname.domain.com
?
編輯3:
在我將所有密鑰移至.ssh/keys/*
並僅指定其中每個主機的密鑰後ssh_config
,現在我在每個ssh
實例化中看到:
/Users/me/.ssh/id_*[^p][^u][^b]: No such file or directory
但即使在那之後,它仍然從載入 IdentityFiles其他連線時 ssh_config 中的主機。為什麼?
答案1
man ssh-config
說:
由於使用每個參數的第一個獲得的值,因此應在文件開頭附近給出更多特定於主機的聲明,並在末尾給出一般預設值。
因此,請確保您想要的配置位於第一個符合的Host
條目中,如匹配行所示ssh -vv
:Applying options for
。
在一般情況下,這意味著Host *
應該是最後的。