Push to GitHub without a password using ssh-key

前端 未结 6 1617
青春惊慌失措
青春惊慌失措 2020-12-12 08:59

I generated an SSH key pair without a password and added the public key to GitHub.

Connection with

user@dev:/var/www/project# ssh -T git@github.com
H         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 09:38

    In case you are indeed using the SSH URL, but still are asked for username and password when git pushing:

    git remote set-url origin git@github.com:/.git
    

    You should try troubleshooting with:

    ssh -vT git@github.com
    

    Below is a piece of sample output:

    ...
    debug1: Trying private key: /c/Users/Yuci/.ssh/id_rsa
    debug1: Trying private key: /c/Users/Yuci/.ssh/id_dsa
    debug1: Trying private key: /c/Users/Yuci/.ssh/id_ecdsa
    debug1: Trying private key: /c/Users/Yuci/.ssh/id_ed25519
    debug1: No more authentication methods to try.
    Permission denied (publickey).
    

    I actually have already added the public key to GitHub before, and I also have the private key locally. However, my private key is of a different name called /c/Users/Yuci/.ssh/github_rsa.

    According to the sample output, Git is trying /c/Users/Yuci/.ssh/id_rsa, which I don't have. Therefore, I could simply copy github_rsa to id_rsa in the same directory.

    cp /c/Users/Yuci/.ssh/github_rsa /c/Users/Yuci/.ssh/id_rsa
    

    Now when I run ssh -vT git@github.com again, I have:

    ...
    debug1: Trying private key: /c/Users/Yuci/.ssh/id_rsa
    debug1: Authentication succeeded (publickey).
    ...
    Hi ! You've successfully authenticated, but GitHub does not provide shell access.
    ...
    

    And now I can push to GitHub without being asked for username and password :-)

提交回复
热议问题