CentOS 7下无法go get私有GitHub仓库模块的问题求助
问题描述
执行go get拉取GitHub私有仓库模块时,触发认证失败错误:
go get go: github.com/xxxxx/xxxxx-service@v0.0.0-20230419012042-5a43328285a5: invalid version: git ls-remote -q origin in /usr/local/bin/pkg/mod/cache/vcs/e3eb4c4c224c608f41c609b576ad32d5a979877e8a7f951b716f1242957c7ddd: exit status 128: 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/xxxxx/xxxxx-service/
已尝试以下操作但未解决问题:
- 在
~/.netrc中添加GitHub凭据:machine github.com login <username> password <Personal Access Token> - 在
~/.gitconfig中配置HTTPS转SSH:[url "git@github.com:"] insteadOf = https://github.com/ [url "ssh://git@github.com/"] insteadOf = https://github.com/ - 添加环境变量:
export GOPRIVATE=github.com/<username>/* export GO111MODULE=on
解决方案
1. 验证SSH密钥有效性
先确认本地SSH密钥已成功关联GitHub账号:
执行ssh -T git@github.com,若返回Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.则SSH认证正常。
若认证失败,重新生成并配置SSH密钥:
ssh-keygen -t ed25519 -C "your-email@example.com" eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
将~/.ssh/id_ed25519.pub内容复制到GitHub的SSH密钥管理页面中。
2. 清理Go模块缓存
模块缓存可能留存旧认证信息,执行清理后重试:
go clean -modcache
3. 修正~/.netrc配置(HTTPS方式)
若使用HTTPS而非SSH,确保配置合规:
- 设置文件权限为
600(Git会忽略权限过高的文件):chmod 600 ~/.netrc - 移除配置末尾多余符号,修正后内容:
注意:个人访问令牌(PAT)需勾选machine github.com login <your-username> password <your-personal-access-token>repo权限。
4. 确认GOPRIVATE环境变量生效
检查变量是否正确加载:
echo $GOPRIVATE
若输出异常,将变量写入~/.bashrc或~/.zshrc,执行source ~/.bashrc(对应shell配置文件)使其生效。
5. 排查Git凭据助手干扰
若之前配置过Git凭据助手,可能影响认证:
git config --global credential.helper
若输出非store/cache,临时禁用后重试:
git config --global --unset credential.helper
内容的提问来源于stack exchange,提问作者Vishwanath Kumar
相关产品推荐
相关产品推荐

