当我尝试将私有仓库从 GitHub 拉到远程 Linux 服务器时,遇到了公钥权限问题。
更新
在本地终端上运行命令ssh -v -o IdentitiesOnly=yes -i ~/.ssh/id_rsa [email protected]
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/username/.ssh/config
debug1: /Users/username/.ssh/config line 88: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/username/.ssh/id_rsa type 0
debug1: identity file /Users/username/.ssh/id_rsa-cert type -1
debug1: identity file /Users/username/.ssh/id_rsa type 0
debug1: identity file /Users/username/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version babeld-dd067d10
debug1: compat_banner: no match: babeld-dd067d10
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
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: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /Users/username/.ssh/known_hosts:92
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: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 1 keys
debug1: Will attempt key: /Users/username/.ssh/id_rsa RSA SHA256:ByqPOdPMwOMKXVfMItyopkVjqG/iZHpOjMgPPwrE7J0 explicit agent
debug1: Will attempt key: /Users/username/.ssh/id_rsa RSA SHA256:ByqPOdPMwOMKXVfMItyopkVjqG/iZHpOjMgPPwrE7J0 explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/username/.ssh/id_rsa RSA SHA256:ByqPOdPMwOMKXVfMItyopkVjqG/iZHpOjMgPPwrE7J0 explicit agent
debug1: Server accepts key: /Users/username/.ssh/id_rsa RSA SHA256:ByqPOdPMwOMKXVfMItyopkVjqG/iZHpOjMgPPwrE7J0 explicit agent
Authenticated to github.com ([140.82.113.3]:22) using "publickey".
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: client_input_hostkeys: searching /Users/username/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching /Users/username/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: no new or deprecated keys from server
debug1: Sending environment.
debug1: channel 0: setting env LANG = "en_CA.UTF-8"
PTY allocation request failed on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Connection to github.com closed.
Transferred: sent 3368, received 2956 bytes, in 0.1 seconds
Bytes per second: sent 22573.3, received 19811.9
debug1: Exit status 1
username
替换服务器上的实际用户名。
我在服务器上创建了一个空目录,并使用 初始化了 Git git init
。权限和所有权设置为drwxr-xr-x 3 username username 4.0K Oct 18 14:19 testing
。
使用 添加了 GitHub repo 。git remote add origin [email protected]:<username>/<repo_name>.git
命令git remote -v
返回:
origin [email protected]:<username>/<repo_name>.git (fetch)
origin [email protected]:<username>/<repo_name>.git (push)
当我运行时git pull origin main
,终端中返回以下内容
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
我的 GitHub 帐户上的公钥似乎是正确的,因为我已经使用 SSH 密钥从那里克隆并提取了各种存储库到我的本地计算机。
为了检查是否存在潜在的损坏问题,我删除了 GitHub 帐户上的所有密钥,然后id_rsa.pub
再次添加了密钥。我在所有远程 Linux 服务器上都使用此密钥,并且没有出现任何连接或权限问题。
我已经从其中一台受影响的服务器运行,并包含了下面的输出。ssh -vT [email protected]
OpenSSH_8.9p1 Ubuntu-3ubuntu0.4, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [140.82.113.3] port 22.
debug1: Connection established.
debug1: identity file /home/username/.ssh/id_rsa type -1
debug1: identity file /home/username/.ssh/id_rsa-cert type -1
debug1: identity file /home/username/.ssh/id_ecdsa type -1
debug1: identity file /home/username/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/username/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/username/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/username/.ssh/id_ed25519 type -1
debug1: identity file /home/username/.ssh/id_ed25519-cert type -1
debug1: identity file /home/username/.ssh/id_ed25519_sk type -1
debug1: identity file /home/username/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/username/.ssh/id_xmss type -1
debug1: identity file /home/username/.ssh/id_xmss-cert type -1
debug1: identity file /home/username/.ssh/id_dsa type -1
debug1: identity file /home/username/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.4
debug1: Remote protocol version 2.0, remote software version babeld-dc5ec9be
debug1: compat_banner: no match: babeld-dc5ec9be
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /home/username/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
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: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /home/username/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /home/username/.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: /home/username/.ssh/id_rsa
debug1: Will attempt key: /home/username/.ssh/id_ecdsa
debug1: Will attempt key: /home/username/.ssh/id_ecdsa_sk
debug1: Will attempt key: /home/username/.ssh/id_ed25519
debug1: Will attempt key: /home/username/.ssh/id_ed25519_sk
debug1: Will attempt key: /home/username/.ssh/id_xmss
debug1: Will attempt key: /home/username/.ssh/id_dsa
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/username/.ssh/id_rsa
debug1: Trying private key: /home/username/.ssh/id_ecdsa
debug1: Trying private key: /home/username/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/username/.ssh/id_ed25519
debug1: Trying private key: /home/username/.ssh/id_ed25519_sk
debug1: Trying private key: /home/username/.ssh/id_xmss
debug1: Trying private key: /home/username/.ssh/id_dsa
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).
用户的 ~/.ssh 目录和文件的权限和所有权是:
drwx------ 2 username username 4.0K Oct 18 15:26 .ssh
-rw------- 1 username username 595 Oct 18 15:04 authorized_keys
-rw------- 1 username username 142 Oct 18 14:03 known_hosts
pbcopy < ~/.ssh/id_rsa.pub
为了测试目的,我已使用并手动从我的计算机复制了公钥。
权限问题适用于主帐户用户和我附加到该帐户的组织下的存储库。
我使用 与服务器交互sudo
,但并非所有git
命令都使用它。
我尝试创建新的 SSH 密钥对,并将其添加到服务器和 GitHub,但出现了同样的问题。我使用了GitHub 生成新的 SSH 密钥并将其添加到 ssh-agent作为参考。
在我的 MacBook 上,我按照 GitHub 支持的建议运行了以下与 SSH 相关的命令。
eval "$(ssh-agent -s)"
返回Agent pid 28566
ssh-add -l -E sha256
返回该代理人没有身份。
ssh-add --apple-use-keychain ~/.ssh/id_rsa
返回添加的身份:/Users/username/.ssh/id_rsa([电子邮件保护])
运行上述命令后,返回以下内容:3072 SHA256:ByqPOdPMwOMKXVfMItyopkVjqG/iZHpOjMgPPwrE7J0 [email protected] (RSA)
文件中内容如下~/.ssh/config
:
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
我查看了各种帮助文章,但无法解决这个问题。希望这里有人能提供一些帮助。
谢谢!
答案1
看起来它正在与更新的 ssh 命令一起工作:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
如果不需要,我建议您从 .ssh 文件夹中删除其他 ssh 密钥。或者您可以配置 git 以使用自定义 SSH 命令:https://stackoverflow.com/questions/4565700/how-to-specify-the-private-ssh-key-to-use-when-executing-shell-command-on-git