go-git
go-git copied to clipboard
ssh: handshake failed: knownhosts: key is unknown
I am a newbie with golang, when I tried to write git related codes with go, I failed to clone repo with ssh url.
The error is ssh: handshake failed: knownhosts: key is unknown
I'm sure:
- the
id_rsa.pemcan be read, and the path is correct. - I have added the public key in my git account.
here is the commands that I executed to create pem:
openssl rsa -in ~/.ssh/id_rsa -outform pem > id_rsa.pem
chmod 700 id_rsa.pem
the id_rsa is created by this, with no password:
ssh-keygen -t rsa -C "casa"
below is the whole code I wrote to clone a repo with ssh url:
package main
import (
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"log"
)
func main() {
auth, err := ssh.NewPublicKeysFromFile("casa", "/Users/casa/.ssh/id_rsa.pem", "")
checkerr(err)
_, err = git.PlainClone("/Users/casa/Playground/git_go_test/test", false, &git.CloneOptions{
URL: "[email protected]:casatwy/HandyAutoLayout.git",
Auth: auth,
})
checkerr(err)
}
func checkerr(err error) {
if err != nil {
log.Fatal(err)
}
}
could you give some advice to me, or show a code example? I have searched a lot and have read the issue #550 , but find no help.
Can you print your $HOME/.ssh/known_host, the error that you are getting means that the host github.com is not found on it.
I'm sure that GitHub.com have been added into my known_hosts, now I get new error:
2017/11/20 10:48:53 ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
exit status 1
here is part of my known_hosts:
github.com,192.30.255.113 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+ Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
192.30.255.112 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+ Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
here is the ping info in my country:
ping github.com
PING github.com (192.30.255.112): 56 data bytes
64 bytes from 192.30.255.112: icmp_seq=0 ttl=45 time=237.918 ms
64 bytes from 192.30.255.112: icmp_seq=1 ttl=45 time=211.659 ms
64 bytes from 192.30.255.112: icmp_seq=2 ttl=45 time=219.568 ms
@casatwy,
Are you able to clone using the standard git tools?
git clone [email protected]:casatwy/HandyAutoLayout.git /Users/casa/Playground/git_go_test/test
@orirawlings
Yes, I'm sure that I can clone this repo with standard git tools.
> git clone [email protected]:casatwy/HandyAutoLayout.git /Users/casa/Playground/git_go_test/test
Cloning into '/Users/casa/Playground/git_go_test/test'...
remote: Counting objects: 135, done.
remote: Total 135 (delta 0), reused 0 (delta 0), pack-reused 135
Receiving objects: 100% (135/135), 29.29 KiB | 149.00 KiB/s, done.
Resolving deltas: 100% (80/80), done.
I solved this by changing the user name to "git", and after that I can do the project clone.
authSSH, err := ssh.NewPublicKeysFromFile("git", "/home/[my-user]/.ssh/id_rsa", "")
[params] user: git pemFile: My private key configured in git password: Since it is not encrypted, it does not need to pass anything.
I hope it can help ...
@supermock seems your solution not works for me...
@casatwy did you ever solve this? I too can not get SSH to work. I receive the same error.
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
I feel like there is missing documentation.
I ran into this issue when trying to connect to an onprem bitbucket repo on port 7999. It was confusing because everything worked fine using standalone git. It worked once I modified the known_hosts entry to include the port, i.e. instead of
hostname ssh-rsa blob
I put
[hostname]:7999 ssh-rsa blob
Guys, any chance we could get casatwy's sample plus pem generating code as an (wiki?) example please? I've been swearing at the clone sample I've got here and his auth code resolved the issues I had with Gitlab's deploy keys (was getting either "auth failure" or an "exists" message). Sorry that this is a bit off topic! (and thanks casatwy!!)
Hello, so far, did you solve it?
Very similar problem:
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
Found out the User attribute shouldn't be set to your actual username but should instead be: git
Example that works with all auth methods:
import (
...
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
sshgit "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
....
)
..
...
type GitLib struct {
repo *git.Repository
params *GitParams
}
func (g *GitLib) open(params *GitParams, path string) (*git.Repository, error) {
g.params = params
if _, err := os.Stat(g.params.basePath); os.IsNotExist(err) {
auth, err := g.auth()
if err != nil {
return nil, err
}
return git.PlainClone(path, false, &git.CloneOptions{
URL: g.params.repo,
Depth: 1,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", g.params.branch)),
SingleBranch: true,
Auth: auth,
})
}
return git.PlainOpen(g.params.basePath)
}
func (g *GitLib) auth() (transport.AuthMethod, error) {
if strings.HasPrefix(g.params.repo, "http") {
return &http.BasicAuth{
Username: g.params.username,
Password: g.params.password,
}, nil
}
isPrivateKey := func(pass string) bool {
if len(pass) > 1000 && strings.HasPrefix(pass, "-----") {
return true
}
return false
}
if isPrivateKey(g.params.password) {
signer, err := ssh.ParsePrivateKeyWithPassphrase([]byte(g.params.password), []byte(g.params.passphrase))
if err != nil {
return nil, err
}
return &sshgit.PublicKeys{
User: "git",
Signer: signer,
HostKeyCallbackHelper: sshgit.HostKeyCallbackHelper{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
}, nil
}
return &sshgit.Password{
User: g.params.username,
Password: g.params.password,
HostKeyCallbackHelper: sshgit.HostKeyCallbackHelper{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
}, nil
}
@mickael-kerjean thanks for your example it saved my life :+1:
https://github.com/src-d/go-git/issues/1228#issue-508250464 this sould be work
url := "[email protected]:supanadit/gostay.git"
var publicKey *ssh.PublicKeys
sshPath := os.Getenv("HOME") + "/.ssh/id_rsa"
sshKey, _ := ioutil.ReadFile(sshPath)
publicKey, keyError := ssh.NewPublicKeys("git", []byte(sshKey), "")
if keyError != nil {
fmt.Println(keyError)
}
_, err := git.PlainClone(url, false, &git.CloneOptions{
URL: urlGitConversion,
Progress: os.Stdout,
Auth: publicKey,
})