1. 首先使用命令 ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/github_id_rsa 来生成 github 的密钥。就是 github_id_rsa 和 github_id_rsa.pub

2. 创建或者修改 config 文件。 vim ~/.ssh/config

Host github
        HostName github.com
        User git
        IdentityFile ~/.ssh/github_id_rsa

注意上面如果没有 User git,那么使用 ssh -T github 会以当前的用户名来登录 github,但是正常情况下,当前 linux 的用户名和 github 的用户名不会一样,所以会错误。这里设置 user 以后,会以 git@github.com 来登录,这时候,就可以了,返回信息是: Hi ptz1986! You've successfully authenticated, but GitHub does not provide shell access.

3. git clone 可以使用简化的方式来操作。

git clone github:your_name/your_repo 相当于 git clone git@github.com:your_name/your_repo.git

4. gitee 也是类似

Host gitee
        HostName gitee.com
        User git
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/gitee_id_rsa

ssh -T gitee 会提示 The authenticity of host 'gitee.com (180.97.125.228)' can't be established. 直接按 yes 就可以了,会自动向 known_hosts 里面添加 gitee 的内容,下次就不需要了。

5. 本地的 gitlab 也是类似

Host gitlab
        HostName xxx.xxx.xxx.xxx
        User gitlab
        IdentityFile ~/.ssh/id_rsa

注意这个 User 可以看你 git clone 时候,@ 前面的那个就是。

参考: https://zhuanlan.zhihu.com/p/31253789
https://blog.csdn.net/u012860695/article/details/88953802
https://www.cnblogs.com/okokabcd/p/9065534.html
https://www.jianshu.com/p/c30e1f787b92
https://gitee.com/help/articles/4229#article-header0
https://blog.csdn.net/M_WBCG/article/details/79156781

标签: git, SSH

添加新评论