欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > git配置ssh key

git配置ssh key

2025/5/2 6:17:06 来源:https://blog.csdn.net/chenchuanhai04/article/details/139831540  浏览:    关键词: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.comHostName gitlab.comPreferredAuthentications publickeyIdentityFile ~/.ssh/gitlab_id-rsa# github
Host github.comHostName github.comPreferredAuthentications publickeyIdentityFile ~/.ssh/github_id-rsa# gitlab.my.com
Host gitlab.my.comHostName 192.168.10.183Port 9022PreferredAuthentications publickeyIdentityFile ~/.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.183example:
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.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词