git 中github clone "Permission denied (publickey). fatal: Could not read from remote repository."

本文介绍了如何配置Git用户名及邮箱,并详细说明了生成SSH密钥的过程,包括查看密钥状态、生成密钥、添加密钥到SSH代理及在GitHub上配置公钥的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次使用然后,,出现上述问题

解决办法均来自互联网:

设置Git的user name和email

git config –global user.name “yourname”
git config –global user.email “youremail”
生成SSH密钥

查看是否已经有了ssh密钥:cd ~/.ssh
如果没有密钥则不会有此文件夹,有则备份删除
生存密钥:
ssh-keygen -t rsa -C “haiyan.xu.vip@gmail.com”
按3个回车,密码为空。
Your identification has been saved in /home/tekkub/.ssh/id_rsa.
Your public key has been saved in /home/tekkub/.ssh/id_rsa.pub.
The key fingerprint is:
………………
最后得到了两个文件:id_rsa和id_rsa.pub
添加密钥到ssh:ssh-add 文件名,需要之前输入密码.

在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥。
打开https://github.com/,在设置中添加密钥

测试:ssh git@github.com
The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.
ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.

解决办法:

1.ssh -v git@github.com

测试ssh连接是否成功,如LZ显示如下:

$ ssh -v git@github.com

OpenSSH_7.3p1, OpenSSL 1.0.2j 26 Sep 2016

[plain] view plain copy
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.253.113] port 22.
debug1: Connection established.
debug1: identity file /c/Users/Administrator/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/Administrator/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.3
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as ‘git’
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: 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/Administrator/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 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: /c/Users/Administrator/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /c/Users/Administrator/.ssh/id_dsa
debug1: Trying private key: /c/Users/Administrator/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/Administrator/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

  1. ssh-agent -s

[plain] view plain copy
$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-GTpABX1a05qH/agent.404; export SSH_AUTH_SOCK;
SSH_AGENT_PID=13144; export SSH_AGENT_PID;
echo Agent pid 13144;

  1. ssh-add ~/.ssh/id_rsa

[plain] view plain copy
$ ssh-add ~/.ssh/id_rsa
Could not open a connection to your authentication agent.
LZ这一步报错了,继续执行:eval ssh-agent -s
无错请忽略以下步骤

[plain] view plain copy
$ eval ssh-agent -s
Agent pid 24460
再次执行ssh-add ~/.ssh/id_rsa

[plain] view plain copy
$ ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/Administrator/.ssh/id_rsa (/c/Users/Administrator/.ssh/id_rsa)

4.将客户端生成的key添加到github账户

首先使用 clip < ~/.ssh/id_rsa.pub 或者文本编辑拷贝id_rsa.pub文本内容

到github右上角账户管理-Setting下面找到左侧“SSH and

GPG keys”菜单,接着点击“Add SSH key”,在”title”栏输入一个自己喜欢的标题,“key”栏中粘贴刚刚复制的公钥内容,最后点击“Add key”按钮。

5.最后一步,验证key

[plain] view plain copy
$ ssh -T git@github.com
Hi JsonJu! You’ve successfully authenticated, but GitHub does not provide shell access.

接下来就可以愉快的去clone github上优秀的项目了。

当采用第二种办法时遇到没有文件夹的时候,我们就应该采用第一种办法建立文件夹

### 解决 Git 克隆仓库时权限被拒绝的问题 当遇到 `git clone` 命令返回 `Permission denied (publickey)` 错误时,这通常意味着客户端无法通过 SSH 密钥验证身份。为了成功执行基于 SSH 协议的 Git 操作,必须确保本地机器已配置有效的 SSH 私钥,并且对应的公钥已在远程服务器(如 GitHub 或 Gerrit)注册。 #### 生成并添加 SSH 密钥到 GitHub/Gerrit 账户 如果 `.ssh` 文件夹不存在名为 `id_rsa.pub` 的文件,则表明尚未创建过 SSH 密钥对。此时可以通过如下命令来生成新的密钥: ```bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 上述指令会提示输入保存位置,默认路径即为 `~/.ssh/id_rsa` 和 `~/.ssh/id_rsa.pub`;接着询问设置密码保护私钥,可根据个人需求决定是否设定[^1]。 完成之后,在终端运行下面这条语句查看新产生的公钥内容以便稍后提交给目标平台: ```bash cat ~/.ssh/id_rsa.pub ``` 随后登录至 GitHub 或者其他托管服务提供商处找到账户安全选项下的 SSH Keys 设置页面,新建条目并将刚才获取到的文字粘贴进去确认添加[^2]。 #### 添加主机名到已知列表 对于首次访问某些特定域名可能会弹出关于其真实性未得到证实的消息框,按照指示键入 “yes”,这样做的目的是让 OpenSSH 客户端信任该站点的身份认证信息从而允许建立连接[^3]。 #### 测试 SSH 连接有效性 最后一步是要检验当前环境能否顺利连通远端仓库提供方的服务接口,可借助此方法尝试发起握手请求: ```bash ssh -T git@github.com ``` 假如一切正常的话应该能看到一条欢迎消息告知关联账号名称;反之则需重新审视前面几步操作是否有遗漏之处或者考虑联系技术支持寻求帮助。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值