[Golang] go get: fatal: could not read Username for 'xxx': terminal prompts disabled

1. Error 1: fatal: could not read Username for 'https://github.com': terminal prompts disabled

The issue appears when I tried to download a private golang repository by the command go get github.com/oscarzhou/private-helloworld. The completed error shows as below:

% go get github.com/oscarzhou/private-helloworld               
go: module github.com/oscarzhou/private-helloworld: git ls-remote -q origin in /Users/oscarzhou/go/pkg/mod/cache/vcs/c5fb5660c8bfa54f1b957fc4e651ac76f19b939fc9015bf3bcbc21a34c925af7: exit status 128:
	fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

If you have already googled this issue, you may find that most of the answers tell you to configure your git as shown below:

git config --global url."git@github.com:".insteadOf "https://github.com/"

This command means that every time when git command is executed, if the url field contains https://github.com/, it will be replaced with git@github.com: (":" must be added). For example, git clone https://github.com/oscarzhou/private-helloworld.git will be changed to git clone git@github.com:oscarzhou/private-helloworld.git. If you are careful, you might notice that the updated string is actually the SSH clone string from your Github account.

2. Why can this command help solve the issue?

The reason is that the commands for Golang to download packages, such as go get, go install, and go mod tidy, all use SSH connection behind the scene. However, the package URL is always presented with http format. For example, go get github.com/oscarzhou/private-helloworld. Thus, replacing the http with ssh format can be a solution.

If the above solution works to you, then it is great since you already configured SSH profile for your Github account in a COMPROMISED way. I assume that your SSH profile configuration is like below:

Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/id-rsa

3. Error 2: Host key verification failed

For those who did not configure the SSH profile for your Github account, you can add the above configuration to your ~/.ssh.config file. Otherwise, you may continue to see another error like below. If you do not know how to configure ssh key pair, please read How to configure SSH profile for Github account.

go get github.com/oscarzhou/private-hellworld
go: module github.com/oscarzhou/private-helloworld: git ls-remote -q origin in /Users/oscarzhou/go/pkg/mod/cache/vcs/c5fb5660c8bfa54f1b957fc4e651ac76f19b939fc9015bf3bcbc21a34c925af7: exit status 128:
	Host key verification failed.
	fatal: Could not read from remote repository.
	
	Please make sure you have the correct access rights
	and the repository exists.

4. Error 3: ERROR: Repository not found.

Even though you add the above ssh profile, sometimes another error may also occur:

go get github.com/oscarzhou/private-hellworld
go: module github.com/oscarzhou/private-helloworld: git ls-remote -q origin in /home/oscar/go/pkg/mod/cache/vcs/d46a0f82cebbd36daa17ac3a74bb241c2b993b94c7336d381479b9e2d244e95b: exit status 128:
 ERROR: Repository not found.
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights 
 and the repository exists.     

This is because you may link to the wrong ssh key with IdentityFile ~/.ssh/id-rsa. Double check whether the ssh key path is correct or not.

5. Error 4: no such identity: /home/oscar/.ssh/id_rsa: No such file or directory

go get github.com/oscarzhou/private-hellworld
go: module github.com/oscarzhou/private-helloworld: git ls-remote -q origin in /home/oscar/go/pkg/mod/cache/vcs/d46a0f82cebbd36daa17ac3a74bb241c2b993b94c7336d381479b9e2d244e95b: exit status 128:
 no such identity: /home/oscar/.ssh/id_rsa: No such file or directory
 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.

This can be caused by the wrong ssh key name in your ssh profile.

6. It doesn’t work if my SSH profile Host is not github.com

If you met such problem, again, you can find the answer from my another post How to configure SSH profile for Github account


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !