git使用錯誤記錄及解決

NO IMAGE

agent refused operation Permission denied (publickey) Error

異常及解決方案

錯誤描述 sign_and_send_pubkey: signing failed: agent refused operation
Permission denied (publickey)

當你在 Linux 電腦的終端下面通過 SSH 連結到 coding.net 或 github.com 時,你或許會看到如下程式碼:

sign_and_send_pubkey: signing failed: agent refused operation 
Permission denied (publickey).

此時我們可以執行如下命令:

ssh -T [email protected]

或者:

ssh -vT [email protected]

我們或許將看到

OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to git.coding.net [124.116.152.172] port 22.
debug1: Connection established.
......
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa      
SHA256:jok3FH7q5LJ6qvE7iPNehBgXRw51ErE77S0Dn Vg/Ik
debug1: Host 'git.coding.net' is known and matches the RSA host key.
debug1: Found key in /home/chen/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/chen/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
sign_and_send_pubkey: signing failed: agent refused operation
debug1: Trying private key: /home/chen/.ssh/id_dsa
debug1: Trying private key: /home/chen/.ssh/id_ecdsa
debug1: Trying private key: /home/chen/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

解決辦法通過 ssh-add

eval "$(ssh-agent -s)"

我們會看到:

Agent pid 25534 

執行下面程式碼:

ssh-add

顯示如下即成功:

Identity added: /home/chen/.ssh/id_rsa (/home/chen/.ssh/id_rsa)

如果你是多個公鑰管理,你只需新增相應的檔案,比如我在 bitbucket.org 下的公鑰為 /home/chen/.ssh/bitbucket.id_rsa.pub 操作如下:

ssh-add /home/chen/.ssh/bitbucket.id_rsa.pub

ssh-add 命令詳細介紹

語法

ssh-add [-cDdLlXx] [-t life] [file ...]
ssh-add -s pkcs11 
ssh-add -e pkcs11

引數說明

-D :刪除ssh-agent中的所有金鑰;

-d :從ssh-agent中的刪除金鑰;

-e : pkcs11 :刪除 PKCS#11 共享庫 pkcs1 提供的鑰匙;

-s : pkcs11 :新增 PKCS#11 共享庫 pkcs1 提供的鑰匙;

-L :顯示 ssh-agent 中的公鑰;

-l :顯示 ssh-agent 中的金鑰;

-t : life:對載入的金鑰設定超時時間,超時 ssh-agent 將自動解除安裝金鑰;

-X :對 ssh-agent 進行解鎖;

-x :對 ssh-agent 進行加鎖;

示例

把專用金鑰新增到 ssh-agent 的快取記憶體中

ssh-add /home/chen/.ssh/id_rsa

從ssh-agent中刪除金鑰

ssh-add -d /home/chen/.ssh/id_rsa.pub

檢視ssh-agent中的金鑰

ssh-add -l

fatal: refusing to merge unrelated histories

解決如下程式碼

git pull github master --allow-unrelated-histories

[陸續新增更新中]