• [Git/GitLab]使用SSH远程登录GitLab/GitHub


    1 前言

    近日,换了台新电脑。
    今日,正要更新(git pull)GitLab的源码时,在配置(用户名,邮箱,密码git config --global -l)完全无误的情况下,却报出如下错误:

    $ git pull
    git@10.0.5.16's password:
    Permission denied, please try again.
    git@10.0.5.16's password:
    
    Permission denied, please try again.
    git@10.0.5.16's password:
    git@10.0.5.16: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    查了一下网友们的博客,可以无疑确定问题所在:
    因新电脑的SSH密钥与GitLab个人账户中的SSH公钥匹配不上,导致远程自动登录失败,远程拉取Git代码错误。
    (PS: (由于新电脑,.ssh下根本就不存在私钥))

    经过捣鼓一翻,还真成!那么,现在记录一下叭!

    2 使用SSH远程登录GitLab

    • step0 打开Git Bash

    • step1 确认/配置 Git的用户名和邮箱 无误

    $ git config --global user.name "u$er"
     [更改全局用户名]
    $ git config --global user.email "yyyy@xxxx.com"
     [更改全局用户邮箱]
    
    $ git config --global -l
    user.name=u$er
    user.email=yyyy@xxxx.com
    
    • step2 切换到当前用户主目录的ssh目录下
      若提示 “ No such file or directory”,可在系统对应路径下新建.ssh文件夹即可mkdir ~/.ssh
    $cd ~/.ssh
    
    $ pwd
    /c/Users/Johnny/.ssh
    
    • step3 本地生成SSH通信的公钥和私钥
    $ ssh-keygen -t rsa -C "yyyy@xxxx.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/Johnny/.ssh/id_rsa): 【Enter】
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again: 【可输入SSH密码,也可不输入。一旦输入,以后git pull/push等操作时,均需输入SSH密码】
    Your identification has been saved in /c/Users/Johnny/.ssh/id_rsa.
    Your public key has been saved in /c/Users/Johnny/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:1UMiwerwehwfhiuwehdsvbk234vjsdv453k456IL567pqwgKexRitL2bkHN6+yhkJY yyyy@xxxx.com
    The key's randomart image is:
    +---[RSA 2048]----+
    |        ..o .    |
    |        .o.+     |
    |     +   .--o    |
    |    . o .+   .   |
    |   + . +S.       |
    |++o++ = +        |
    |@*E. .O+ .       |
    |B++o+.B.o        |
    |o. +++..         |
    +----[SHA256]-----+
    
    $ ll
    total 6
    -rw-r--r-- 1 Johnny 197121 1766 9月  10 19:25 id_rsa
    -rw-r--r-- 1 Johnny 197121  403 9月  10 19:25 id_rsa.pub
    

    这样系统路径下就生成了2个密钥文件:id_rsa(私钥,存在本地)和id_rsa.pub(公钥,发放给客户端,例如:GitLab个人账户中)

    • step4 全文拷贝生成的SSH公钥到GitLab中
    $ clip < ~/.ssh/id_rsa.pub
      【clip: 拷贝/剪切板命令】
    
    • step5 验证
    $ ssh -T git@GitLabHostAddress
        [方式1]
    
    $ git pull
    Enter passphrase for key '/c/Users/Johnny/.ssh/id_rsa': 【若之前设置了SSH密码,则:此时需输入正确的SSH密码】
    remote: Enumerating objects: 1468, done.
    remote: Counting objects: 100% (1468/1468), done.
    remote: Compressing objects: 100% (735/735), done.
    remote: Total 1468 (delta 781), reused 1360 (delta 719)
    Receiving objects: 100% (1468/1468), 10.93 MiB | 5.03 MiB/s, done.
    Resolving deltas: 100% (781/781), completed with 95 local objects.
    ...
        [方式2]
    

    X 参考文献

  • 相关阅读:
    python的数据类型+常用操作符
    厉害了
    git merge ignore line ending
    CNAME , DNS , A Record , Domain Name

    setcookie无效
    magic quote gpc htmlspecialchars
    整型 浮点型 不能转化
    git push -f带来的conflict如何解决
    git pull --rebase
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/13648273.html
Copyright © 2020-2023  润新知