git@github.com:权限被拒绝(Linux 指南)
GitHub SSH 身份验证和电子邮件隐私指南(git@github.com:权限被拒绝)
如果您遇到以下错误,请务必查看本指南:
git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
先决条件
1.检查现有的 SSH 密钥
首先,检查您是否已拥有 SSH 密钥:
ls -al ~/.ssh
查找如下文件:
2. 生成新的 SSH 密钥
如果您想删除现有密钥(id_rsa、id_rsa.pub)并重新开始,请按照以下步骤操作:
# Remove private key rm ~/.ssh/id_rsa # Remove public key rm ~/.ssh/id_rsa.pub
或者删除多个密钥对(如果有)
# Remove all RSA keys rm ~/.ssh/id_rsa*
如果未找到现有密钥,则生成一个新的 SSH 密钥:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
密钥生成步骤:
3. 电子邮件隐私配置
GitHub 电子邮件隐私
GitHub 提供了几种选项来保护你的电子邮件隐私:
配置电子邮件隐私
步骤 1:GitHub 账户设置
第 2 步:配置本地 Git 电子邮件
使用 GitHub 提供的私人电子邮件进行提交:
# Set global Git email (replace with your GitHub username) git config --global user.email "username@users.noreply.github.com" # Optionally set your name git config --global user.name "Your Name"
确认
git config --global user.email git config --global user.name
4.显示你的公共 SSH 密钥
cat ~/.ssh/id_rsa.pub
复制整个输出 - 这是您将添加到 GitHub 的密钥。
5. 将 SSH 密钥添加到 GitHub
Web 界面步骤:
6.配置SSH代理
启动 SSH 代理并添加您的密钥:
# Start the SSH agent eval "$(ssh-agent -s)" # Add your SSH private key ssh-add ~/.ssh/id_rsa
7. 测试 GitHub 连接
验证您的 SSH 连接:
ssh -T git@github.com
预期输出包括一条带有您的 GitHub 用户名的成功消息,如下所示:
"Hi! You've successfully authenticated, but GitHub does not provide shell access."
常见问题故障排除
权限被拒绝错误
chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub