赞
踩
打开终端,输入命令,-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$
1. 打开~/.ssh/id_rsa.pub文件(~表示用户目录,比如我的windows就是C:\Users\Administrator),复制其中的内容。
2. 打开见面,登录git,找到 用户设置——>SSH密钥,如下图所示。
至此,如果生成的公钥和私钥对是默认路径,便可以通过 git clone ssh://git@gitlab.com/myproject.git来访问了。
当生成的公钥和私钥对不是默认路径或者你需要多个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.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。