ssh訪問主機,git連線github失敗;git多賬戶配置問題;使用ssh-agent儲存祕鑰
技術標籤:Configgitsshubuntuwindows 10github
1. 問題:win10通過ssh連線虛擬機器上的Ubuntu,進行git操作會提示permission denied;而在Ubuntu的終端直接操作,可以正常訪問github;同時也是git多賬戶配置會發生的問題;使用ssh-agent儲存祕鑰
截圖如下
通過ssh -T -v [email protected]檢視詳細報錯如下
OpenSSH_8.0p1, OpenSSL 1.1.1c 28 May 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: Connection established.
debug1: identity file /c/Users/claud/.ssh/id_rsa type -1
debug1: identity file /c/Users/claud/.ssh/id_rsa-cert type -1
debug1: identity file /c/Users/claud/.ssh/id_dsa type -1
debug1: identity file /c/Users/claud/.ssh/id_dsa-cert type -1
debug1: identity file /c/Users/claud/.ssh/id_ecdsa type -1
debug1: identity file /c/Users/claud/.ssh/id_ecdsa-cert type -1
debug1: identity file /c/Users/claud/.ssh/id_ed25519 type -1
debug1: identity file /c/Users/claud/.ssh/id_ed25519-cert type -1
debug1: identity file /c/Users/claud/.ssh/id_xmss type -1
debug1: identity file /c/Users/claud/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.0
debug1: Remote protocol version 2.0, remote software version babeld-6c2374e6
debug1: no match: babeld-6c2374e6
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: [email protected] MAC:
<implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC:
<implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa
SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/claud/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /c/Users/claud/.ssh/id_rsa
debug1: Will attempt key: /c/Users/claud/.ssh/id_dsa
debug1: Will attempt key: /c/Users/claud/.ssh/id_ecdsa
debug1: Will attempt key: /c/Users/claud/.ssh/id_ed25519
debug1: Will attempt key: /c/Users/claud/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info:
server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /c/Users/claud/.ssh/id_rsa
debug1: Trying private key: /c/Users/claud/.ssh/id_dsa
debug1: Trying private key: /c/Users/claud/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/claud/.ssh/id_ed25519
debug1: Trying private key: /c/Users/claud/.ssh/id_xmss
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).
2. 問題成因
2.1. ssh掃描問題
為了區分祕鑰,本人在生成祕鑰輸入祕鑰名稱時,將github對應祕鑰設定為~/.ssh/id_rsa_github,在上面最後的幾個debug1執行Trying private key,並沒有掃描到id_rsa_github,只掃描了id_rsa,導致無法找到祕鑰
2.2.ssh-agent
ssh-agent ,意為 ssh 代理,是一個金鑰管理器,用來管理一個多個金鑰。各作業系統下的ssh都會自帶ssh-agent
ssh使用一個ssh-agent工具來作為祕鑰管理器,其用處如下[1]
① 當其他程式 需要身份驗證的時候,可以將驗證申請交給 ssh-agent 來完成整個認證過程 。使用不同的金鑰連線到不同的主機時,需要要手動指定對應的金鑰,而 ssh 代理可以 自動幫助我們選擇對應的金鑰進行認證。
② 避免重複輸入密碼:如果您的私鑰使用密碼短語來加密了的話,每一次使用 SSH 金鑰對 進行登入的時候,您都必須輸入正確的密碼短語。而 SSH agent 程式能夠將您的已解密 的私鑰快取起來,在需要的時候提供給您的 SSH 客戶端。這樣子,您就只需要在使用ssh-add
時將私鑰加入 SSH agent 快取的時候,輸入一次密碼短語就可以了。這為經 常使用 SSH 連線使用者提供了不少便利。
然而,在win下連線Ubuntu的終端執行 ssh-add -l,會有“could not open a connection to your authentication agent”錯誤,說明ssh-agent未啟動。執行“eval `ssh-agent -s`”啟動ssh-agent,再執行ssh-add -l為空,表明此ssh-agent裡未儲存有祕鑰
但是,在ubuntu下,ssh-add -l可正常執行,並且有祕鑰顯示
說明通過ssh開啟的終端,並沒有喚起ssh-agent,同時手動喚起也沒有祕鑰
3. 解決方案
3.1. 編寫config
在~/.ssh/下新建config檔案,編寫如下
Host github
HostName github.com
User Username
IdentityFile /home/xxx/.ssh/id_rsa_github
Host為host別名,任意起;HostName為地址;User為登入Host的使用者名稱;IdentityFile為祕鑰地址
3.2. 設定ssh-agent自啟動並複用
在每個終端啟動時自啟動ssh-agent,同時保證只使用一個ssh-agent程序,即可保證存入ssh-agent的祕鑰不丟失
在~/.zshrc(本人使用zsh,如果使用其他shell請自行選擇配置檔案)中加入自啟動&複用程式碼[2][3]
SSH_ENV="$HOME/.ssh/agent-environment"
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
儲存,並執行"source~/.zshrc" (source 配置檔案),會有操作成功的提示
3.3. 在ssh-agent中新增祕鑰
執行 "ssh-add ~/.ssh/id_rsa_github",向ssh-agent新增祕鑰
執行後可用 "ssh-add -l"檢視私鑰,用"ssh-add -L"檢視公鑰
4. 結果
至此,可在通過ssh連線Ubuntu的終端下執行git操作。同時此方法也可用於多賬戶配置,比如id_rsa_github、id_rsa_gitlab...
5. 問題
Ubuntu本機終端和win下ssh起的終端有什麼區別?為什麼有程式在Ubuntu本機被執行,ssh遠端沒有被執行?暫時沒有想明白,希望大佬們不吝賜教