git@gitee.com: Permission denied (publickey).

这篇博客讲述了作者在首次使用Git时遇到的SSH连接权限问题。通过查阅资料和修改ssh_config文件,将特定名称的私钥文件路径指定给gitee.com,最终成功解决了重启后SSH连接失效的难题。

第一次使用git工具,按照gitee官网教程设置完密钥 ,重启以后再次ssh,出现错误

# ssh -T git@gitee.com
git@gitee.com: Permission denied (publickey).

查了网上资料,说是要先启动ssh-agent,然后运行ssh-add,将git仓库设置的公钥对应的私钥添加

#ssh-add /root/.ssh/id_rsa_git 设置完果然有用,高兴之时发现重启后又失效了!

细心的网友可能会发现我的私钥文件名不是默认的id_rsa,这是因为我的id_rsa这个私钥文件是用来登陆我自己 远程服务器设置的,生成git密钥对的时候不想覆盖,所以设置了一个新名字,问题恰恰可能就出现这个名字上面。

man ssh看看

-F configfile
             Specifies an alternative per-user configuration file.  If a con‐
             figuration file is given on the command line, the system-wide
             configuration file (/etc/ssh/ssh_config) will be ignored.  The
             default for the per-user configuration file is ~/.ssh/config.

找到这么一段,有个ssh_config文件?赶紧去目录找找,在/etc/ssh/找到了

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com
#   EscapeChar ~
#   Tunnel no

 这大概是个模板,需要把这个文件另存到/root/.ssh/目录下,名字改为config

Host gitee.com 
#   ForwardAgent no
#   ForwardX11 no
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
   IdentityFile ~/.ssh/id_rsa_git 
#   Port 22
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com

 在host填入gitee.com,下面指定私钥文件路径,再次运行ssh -T git@gitee.com

# ssh -T git@gitee.com
H! You've successfully authenticated, but GITEE.COM does not provide shell access.

成功。并且重启也还是ok的,包括git push也不会出现连不上的问题。

```markdown ### 问题分析 错误信息: ``` git@gitee.com: Permission denied (publickey) ``` 表明 SSH 无法通过公钥认证连接 Gitee。这是由于本地 SSH 密钥未正确生成、未添加到 Gitee 账户,或 SSH Agent 未加载密钥所致。 --- ### 解决方案 #### ✅ 1. **检查是否存在 SSH 密钥** 运行命令查看是否已有 RSA 密钥: ```bash ls ~/.ssh/id_rsa.pub ``` 若提示“No such file”,说明尚未生成密钥。 #### ✅ 2. **生成新的 SSH 密钥** 执行以下命令并按提示完成(邮箱替换为你的 Gitee 注册邮箱): ```bash ssh-keygen -t rsa -C "your_email@example.com" ``` - 默认保存路径:`~/.ssh/id_rsa` - 可设置密码保护(可选) #### ✅ 3. **复制公钥内容** 输出公钥内容: ```bash cat ~/.ssh/id_rsa.pub ``` 复制从 `ssh-rsa` 开始直到邮箱的整段内容。 #### ✅ 4. **添加公钥到 Gitee** 1. 登录 [Gitee 官网](https://gitee.com) 2. 进入「用户设置」→「SSH 公钥」 3. 标题自定义(如 `my-linux-pc`) 4. 粘贴刚才复制的公钥内容 5. 点击「确定」 #### ✅ 5. **启动 SSH Agent 并添加私钥** 确保 SSH Agent 正在运行并加载密钥: ```bash eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa ``` #### ✅ 6. **测试连接** 运行测试命令: ```bash ssh -T git@gitee.com ``` 成功时会返回: ``` Hi <username>! You've successfully authenticated, but Gitee.com does not provide shell access. ``` --- ### **知识点(列出该代码中遇到的知识点)** - **SSH 公钥认证**:通过非对称加密验证身份,客户端保留私钥,服务端存储公钥以实现免密登录。 - **SSH Agent 作用**:管理私钥的临时驻留程序,避免重复输入密码,需手动添加密钥。 - **Git over SSH 协议**:使用 `git@host:path/repo.git` 格式地址,依赖 SSH 密钥完成安全通信。 ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值