![]() |
GIT 错误remote: Invalid username or token |
Username for 'https://github.com': neonmike Password for 'https://neonmike@github.com': remote: Invalid username or token. Password authentication is not supported for Git operations. fatal: Authentication failed for 'https://github.com/neonmike/NetQuality.git/'
本地生成 SSH 密钥(如果已有 ~/.ssh/id_rsa
可跳过):
ssh-keygen -t ed25519 -C "your_email@example.com"
# 或使用 rsa: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
按回车使用默认路径,若设密码短语可增加安全性。
复制公钥内容:
cat ~/.ssh/id_ed25519.pub
# 或 cat ~/.ssh/id_rsa.pub
在 GitHub:Settings → SSH and GPG keys → New SSH key,把公钥粘上并保存。
把远程改为 SSH:
git remote set-url origin git@github.com:neonmike/NetQuality.git
测试连接:
ssh -T git@github.com
# 你应该看到 "Hi neonmike! You've successfully authenticated, but GitHub does not provide shell access."
这条输出说明:SSH 公钥配置已经成功,你和 GitHub 的身份认证没问题了。
接下来就只剩下一个动作:把远程地址从 HTTPS 改成 SSH,这样 git push
/ git pull
才会走你刚配置好的密钥。
执行下面命令更新 remote:
git remote set-url origin git@github.com:neonmike/NetQuality.git
确认一下:
git remote -v
输出应该是:
origin git@github.com:neonmike/NetQuality.git (fetch)
origin git@github.com:neonmike/NetQuality.git (push)
之后再 push:
git push origin main
GitHub 已经禁用了账号密码的 HTTPS 认证。
现在只剩下两种安全方式:
HTTPS + Personal Access Token (PAT)
远程地址是
https://github.com/neonmike/NetQuality.git
每次需要提供 用户名(就是 neonmike
)和 密码(实际上是你生成的 PAT)。
可以配合系统的 credential helper 缓存,不用每次都输入。
好处:和公司防火墙、代理兼容性好(防火墙通常允许 HTTPS 443 端口)。
坏处:PAT 是一串很长的 token,不如 SSH key 顺手。
SSH key
远程地址是
git@github.com:neonmike/NetQuality.git
用本地私钥认证,GitHub 上存公钥。
好处:一劳永逸,不用输入用户名/密码,每次 push/pull 自动用密钥;脚本、CI/CD 很方便。
坏处:如果你在一些受限网络里(只开 443 端口),SSH 默认走 22 端口,可能被屏蔽,需要特殊配置(例如 ssh.github.com:443
)。
版权声明:
感谢您的阅读,除非文中已经注明来源网站,否则均为理科生网原创内容,转载时请务必以超链接(而非纯文本链接)标注来源于理科生网及本文完整链接,感谢!{alertInfo}