1. 问题背景:HTTPS认证失败的典型场景
上周五晚上11点,我正在尝试从GitHub克隆一个机器学习项目仓库,突然遇到了Support for password authentication was removed的报错。这已经不是第一次遇到GitHub HTTPS认证问题了——自从2021年8月13日GitHub正式停用密码认证后,所有通过HTTPS协议进行的git操作都需要使用Personal Access Token(PAT)替代密码。
典型的错误场景是这样的:
bash复制$ git clone https://github.com/username/repo.git
Username: your_email@example.com
Password: your_password
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/username/repo.git/'
更麻烦的是,即使用PAT替代密码,在国内网络环境下HTTPS克隆仍然可能因为网络波动导致认证超时。这时候,切换到SSH协议就成了更可靠的解决方案——它不仅避免了频繁的认证提示,还能通过密钥对实现无缝连接。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. SSH协议的优势与准备工作
2.1 为什么选择SSH协议?
与HTTPS相比,SSH协议在Git操作中有三个明显优势:
- 单次配置长期有效:配置好SSH密钥后无需每次输入凭证
- 连接稳定性更高:SSH的长连接特性适合网络波动环境
- 传输效率更优:SSH协议在大型仓库的传输速度通常比HTTPS快20-30%
2.2 检查现有SSH密钥
首先检查本地是否已有SSH密钥对(通常存放在~/.ssh/目录):
bash复制ls -al ~/.ssh/
如果看到id_rsa和id_rsa.pub文件(或类似命名的ed25519密钥),说明已有密钥对。否则需要生成新的。
注意:如果已有密钥但对安全性有更高要求,建议生成新的Ed25519算法密钥(比RSA更安全高效)
2.3 生成新的SSH密钥对
使用以下命令生成Ed25519算法的密钥对(将邮箱替换为你的GitHub注册邮箱):
bash复制ssh-keygen -t ed25519 -C "your_email@example.com"
生成过程中会提示:
- 密钥保存路径(直接回车使用默认位置)
- 设置密钥密码(可选但建议设置)
- 确认密码
成功后会生成两个文件:
~/.ssh/id_ed25519:私钥(必须严格保密)~/.ssh/id_ed25519.pub:公钥(需上传到GitHub)
3. 将SSH密钥添加到GitHub账户
3.1 复制公钥内容
使用以下命令显示并复制公钥内容(注意不要复制多余的空格或换行):
bash复制cat ~/.ssh/id_ed25519.pub | pbcopy # Mac
cat ~/.ssh/id_ed25519.pub | clip # Windows
公钥内容格式类似:
code复制ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJx3qV9Z... your_email@example.com
3.2 在GitHub添加SSH密钥
- 登录GitHub → 点击右上角头像 → Settings
- 左侧菜单选择"SSH and GPG keys"
- 点击"New SSH key"
- 填写:
- Title:标识该密钥的机器名称(如"My MacBook Pro")
- Key type:保持默认Authentication Key
- Key:粘贴刚才复制的公钥内容
- 点击"Add SSH key"
关键检查点:确保公钥完整粘贴,开头是
ssh-ed25519,结尾是你的邮箱
4. 测试SSH连接配置
4.1 首次连接测试
运行以下命令测试SSH连接:
bash复制ssh -T git@github.com
首次连接会看到类似提示:
code复制The authenticity of host 'github.com (140.82.121.3)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
输入yes后,如果看到:
code复制Hi username! You've successfully authenticated, but GitHub does not provide shell access.
说明SSH配置成功。
4.2 常见连接问题排查
如果遇到Permission denied (publickey)错误,按以下步骤排查:
-
验证密钥加载:
bash复制
ssh-add -l如果列表为空,需要手动添加:
bash复制
ssh-add ~/.ssh/id_ed25519 -
检查SSH配置文件:
确保~/.ssh/config包含以下内容:code复制Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes -
验证密钥指纹:
bash复制
ssh-keygen -lf ~/.ssh/id_ed25519.pub对比GitHub上显示的指纹是否一致
5. 迁移现有仓库从HTTPS到SSH
5.1 修改远程仓库URL
对于已有仓库,查看当前远程URL:
bash复制git remote -v
如果显示HTTPS地址,使用以下命令切换为SSH:
bash复制git remote set-url origin git@github.com:username/repo.git
5.2 验证迁移结果
再次查看远程URL确认:
bash复制git remote -v
正确显示应为:
code复制origin git@github.com:username/repo.git (fetch)
origin git@github.com:username/repo.git (push)
5.3 执行测试操作
执行简单的git操作验证:
bash复制git fetch
git pull
如果这些命令不再提示输入凭证,说明迁移成功。
6. 高级配置与优化技巧
6.1 多账户SSH配置
如果你有多个GitHub账户,可以通过~/.ssh/config文件管理:
code复制# 个人账户
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# 工作账户
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
使用时将仓库URL中的github.com替换为对应的Host别名:
bash复制git clone git@github.com-personal:username/repo.git
6.2 SSH连接保持配置
在~/.ssh/config中添加以下配置防止连接超时:
code复制Host *
ServerAliveInterval 60
TCPKeepAlive yes
6.3 加速Git操作的SSH配置
对于大型仓库,可以启用SSH压缩:
code复制Host github.com
Compression yes
CompressionLevel 6
7. 常见问题解决方案
7.1 克隆时出现"Too many authentication failures"
这是因为SSH客户端默认会尝试所有可用密钥。解决方案:
- 在
~/.ssh/config中添加:code复制Host github.com IdentitiesOnly yes - 或指定密钥路径:
bash复制GIT_SSH_COMMAND="ssh -i ~/.ssh/id_ed25519" git clone git@github.com:user/repo.git
7.2 密钥密码输入太频繁
可以使用ssh-agent管理密钥密码:
bash复制# 启动ssh-agent
eval "$(ssh-agent -s)"
# 添加密钥(会提示输入一次密码)
ssh-add ~/.ssh/id_ed25519
7.3 防火墙阻挡SSH连接
如果遇到连接超时,可能是防火墙阻挡了22端口。GitHub也提供443端口的SSH:
在~/.ssh/config中添加:
code复制Host github.com
Hostname ssh.github.com
Port 443
8. 安全最佳实践
- 定期轮换密钥:建议每6-12个月生成新密钥并替换旧密钥
- 使用强密码保护密钥:即使私钥泄露,密码仍能提供保护
- 限制密钥权限:GitHub SSH密钥只应用于代码仓库,不应用于服务器登录
- 及时撤销未使用的密钥:离职或更换设备后立即删除相关密钥
- 启用双因素认证:为GitHub账户启用2FA增加安全性
我在实际工作中发现,很多开发者忽略的一个细节是:当在公共计算机上使用SSH密钥后,一定要记得从ssh-agent中移除密钥:
bash复制# 列出已加载密钥
ssh-add -l
# 移除特定密钥
ssh-add -d ~/.ssh/id_ed25519
# 清除所有密钥
ssh-add -D
