git配置ssh key

一、生成ssh公钥和私钥对

        打开终端,输入命令,-C 后是git邮箱,在 Enter file in which to save the key (/home/my/.ssh/id_rsa): 后可以输入公钥和私钥对保存路径及文件名,默认是 /home/my/.ssh/id_rsa,其它的全部按回车即可。(公钥和私钥对保存路径及文件名不是默认的需要配置 config

my@ubuntu:~$ ssh-keygen -t rsa -C '84848493@qq.com'
Generating public/private rsa key pair.
Enter file in which to save the key (/home/my/.ssh/id_rsa): /home/my/.ssh/id_rsa_ldc
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/my/.ssh/id_rsa_ldc
Your public key has been saved in /home/my/.ssh/id_rsa_ldc.pub
The key fingerprint is:
SHA256:O2EBiWUW+VyztfbRIC7wMN1s0sNHJu6heCVdL+X86bg chenchuanhai@ldcems.com
The key's randomart image is:
+---[RSA 3072]----+
|     o*+ . =..+ .|
|    .oo.+ =oX+o= |
|       o.*.B==.o+|
|        oo+=+...+|
|        S oo.. o.|
|       . +    +  |
|        o    . . |
|         .    .  |
|             E   |
+----[SHA256]-----+
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文件。打开文件。

vim ~/.ssh/config

        在文件中输入以下内容

# gitlab
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/gitlab_id-rsa

# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_id-rsa

# gitlab.my.com
Host gitlab.my.com
    HostName 192.168.10.183
    Port 9022
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_my

  ​
# 配置文件参数
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
# HostName : 要登录主机的主机名
# User : 登录名
# IdentityFile : 指明上面User对应的identityFile路径
# Port : 端口

        对于gitlab.my.com中由于主机名是ip,需要配置hosts,打开hosts文件

sudo vim etc/hosts

        添加一行

192.168.10.183 gitlab.my.com

        访问时使用:git clone ssh://git@gitlab.my.com:9022/myproject.git

四、验证

ssh-keygen -R 172.22.195.183
ssh -p 8022 git@172.22.195.183

example:
cch@ldc:~/work/notes$ ssh git@gitee.com
Hi 奋斗的cch(@struggling-cch)! You've successfully authenticated, but GITEE.COM does not provide shell access.
Connection to gitee.com closed.

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值