git配置ssh key

打印 上一主题 下一主题

主题 1801|帖子 1801|积分 5403

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、天生ssh公钥和私钥对

        打开终端,输入下令,-C 后是git邮箱,在 Enter file in which to save the key (/home/my/.ssh/id_rsa): 后可以输入公钥和私钥对保存路径及文件名,默认是 /home/my/.ssh/id_rsa,别的的全部按回车即可。(公钥和私钥对保存路径及文件名不是默认的必要配置 config)
  1. my@ubuntu:~$ ssh-keygen -t rsa -C '84848493@qq.com'
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/my/.ssh/id_rsa): /home/my/.ssh/id_rsa_ldc
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /home/my/.ssh/id_rsa_ldc
  7. Your public key has been saved in /home/my/.ssh/id_rsa_ldc.pub
  8. The key fingerprint is:
  9. SHA256:O2EBiWUW+VyztfbRIC7wMN1s0sNHJu6heCVdL+X86bg chenchuanhai@ldcems.com
  10. The key's randomart image is:
  11. +---[RSA 3072]----+
  12. |     o*+ . =..+ .|
  13. |    .oo.+ =oX+o= |
  14. |       o.*.B==.o+|
  15. |        oo+=+...+|
  16. |        S oo.. o.|
  17. |       . +    +  |
  18. |        o    . . |
  19. |         .    .  |
  20. |             E   |
  21. +----[SHA256]-----+
  22. my@ubuntu:~/.ssh$
复制代码

二、添加SSH密钥

        1. 打开~/.ssh/id_rsa.pub文件(~表示用户目次,好比我的windows就是C:\Users\Administrator),复制其中的内容。
        2. 打开见面,登录git,找到 用户设置——>SSH密钥,如下图所示。


        至此,假如天生的公钥和私钥对是默认路径,便可以通过 git clone ssh://git@gitlab.com/myproject.git来访问了。

三、config

        当天生的公钥和私钥对不是默认路径或者你必要多个git服务器时,必要配置config文件。打开文件。
  1. vim ~/.ssh/config
复制代码
        在文件中输入以下内容
  1. # gitlab
  2. Host gitlab.com
  3.    HostName gitlab.com
  4.    PreferredAuthentications publickey
  5.    IdentityFile ~/.ssh/gitlab_id-rsa
  6. # github
  7. Host github.com
  8.     HostName github.com
  9.     PreferredAuthentications publickey
  10.     IdentityFile ~/.ssh/github_id-rsa
  11. # gitlab.my.com
  12. Host gitlab.my.com
  13.     HostName 192.168.10.183
  14.     Port 9022
  15.     PreferredAuthentications publickey
  16.     IdentityFile ~/.ssh/id_rsa_my
  17.   ​
  18. # 配置文件参数
  19. # Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
  20. # HostName : 要登录主机的主机名
  21. # User : 登录名
  22. # IdentityFile : 指明上面User对应的identityFile路径
  23. # Port : 端口
复制代码
        对于gitlab.my.com中由于主机名是ip,必要配置hosts,打开hosts文件
  1. sudo vim etc/hosts
复制代码
        添加一行
  1. 192.168.10.183 gitlab.my.com
复制代码
        访问时使用:git clone ssh://git@gitlab.my.com:9022/myproject.git
四、验证
  1. ssh-keygen -R 172.22.195.183
  2. ssh -p 8022 git@172.22.195.183
  3. example:
  4. cch@ldc:~/work/notes$ ssh git@gitee.com
  5. Hi 奋斗的cch(@struggling-cch)! You've successfully authenticated, but GITEE.COM does not provide shell access.
  6. Connection to gitee.com closed.
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表