Git 推送报错 "Password authentication is not supported" 解决全过程
Git 推送报错 "Password authentication is not supported" 解决全过程
记录一次排查 GitHub 推送认证失败的完整过程,坑点不少,留存备忘。
问题现象
执行 git push 时弹出用户名/密码输入框,输入后报错:
1
2
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/xxx/xxx.git/'
根本原因
GitHub 自 2021 年 8 月起不再支持密码认证,推送必须使用 SSH 密钥或 Personal Access Token。
解决步骤
第一步:配置 SSH 密钥
检查是否已有密钥:
1
ls ~/.ssh/id_rsa.pub ~/.ssh/id_ed25519.pub 2>/dev/null
没有则生成:
1
2
ssh-keygen -t ed25519 -C "your_email@example.com"
# 一路回车即可
复制公钥,添加到 GitHub:
1
cat ~/.ssh/id_ed25519.pub
GitHub → Settings → SSH and GPG keys → New SSH key,粘贴保存。
第二步:修复私钥权限
验证 SSH 连接时报错:
1
2
Permissions 0644 for '/root/.ssh/id_rsa' are too open.
This private key will be ignored.
SSH 要求私钥权限必须是 600,修复:
1
chmod 600 ~/.ssh/id_rsa
再次验证:
1
2
ssh -T git@github.com
# Hi xxx! You've successfully authenticated...
第三步:修改远程地址为 SSH
1
git remote set-url origin git@github.com:用户名/仓库名.git
验证:
1
git remote -v
第四步(关键坑):排查 insteadOf 配置
改完远程地址后,git push 仍然提示输入用户名密码,且报错地址依然是 HTTPS。
明明 .git/config 里已经是 SSH 地址:
1
2
[remote "origin"]
url = git@github.com:xxx/xxx.git
排查全局配置:
1
git config --list | grep -i instead
发现问题所在:
1
2
url.https://github.com/.insteadof=ssh://git@github.com/
url.https://github.com/.insteadof=git@github.com:
这两条规则存在于 ~/.gitconfig 中,会强制将所有 SSH 地址替换为 HTTPS,导致配置文件里写的是 SSH,实际请求走的却是 HTTPS。
删除这两条规则:
1
2
git config --global --unset url.https://github.com/.insteadof "ssh://git@github.com/"
git config --global --unset url.https://github.com/.insteadof "git@github.com:"
验证已清除:
1
2
git config --list | grep -i instead
# 无输出即为干净
第五步:正常推送
1
git push
成功!
insteadOf 是怎么来的?
这类配置通常来源于:
- 早期自己按某些教程配置过,用于在不支持 SSH 的网络环境下强制走 HTTPS
- 某个初始化脚本或工具(conda、IDE 等)自动写入
- 服务器运维批量下发的环境配置
可以用以下命令查看完整的全局配置文件:
1
cat ~/.gitconfig
总结
| 步骤 | 问题 | 解决方法 |
|---|---|---|
| 1 | GitHub 不支持密码推送 | 配置 SSH 密钥 |
| 2 | 私钥权限过宽 | chmod 600 ~/.ssh/id_rsa |
| 3 | 远程地址是 HTTPS | git remote set-url 改为 SSH |
| 4 | insteadOf 强制覆盖 SSH 为 HTTPS | git config --global --unset 删除规则 |
最隐蔽的坑是第四步,配置文件看起来完全正确,却被全局规则悄悄覆盖,排查时需要用 git config --list | grep -i instead 专门检查。
本文由作者按照 CC BY 4.0 进行授权