2

使用ssh访问远程代码仓库

 2 years ago
source link: https://www.daozhao.com/10241.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用ssh访问远程代码仓库

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

使用ssh访问远程代码仓库

公司一般要求多久就必须修改密码,此时使用ssh的优势就出来了。只要不删除ssh的密钥,修改账号的密码不受影响的。

ssh的公钥太长了,我们一般使用它的指纹来验证更加方便点

公钥一般是这样 file

它的指纹一般是这样,确实方便很多 file

ssh相关信息在 ~/.ssh文件内

配置ssh访问
  • 生成密钥 一般公司的代码使用内网gitlab,自己的代码使用github
    ssh-keygen -t rsa -C "注册的gitlab邮箱" -f ~/.ssh/gitlab_rsa
    ssh-keygen -t rsa -C "注册的github邮箱" -f ~/.ssh/github_rsa

    此时会生成下列文件 github_rsa(密钥)、github_rsa.pub(公钥) github_rsa(密钥)、github_rsa.pub(公钥)。

file

  • 配置~/.ssh/config文件 只需配两项,其他参数默认即可

    Host github
    HostName github.com
    IdentityFile ~/.ssh/github_rsa
    Host gitlab
    HostName gitlab.com
    IdentityFile ~/.ssh/gitlab_rsa
  • 添加新增的rsa到名字列表 这一步很重要,不然会找不到个性化的rsa默认使用系统的rsa

    ssh-add ~/.ssh/gitlab_rsa
    ssh-add ~/.ssh/github_rsa

    我们可以通过ssh-add -l查看当前已经添加的rsa的ssh的指纹

  • 将公钥的字符串(即github_rsa.pub和github_rsa.pub的内容)复制添加到GitHub和gitlab的SSH key中 file

  • ssh -T git@github

如果能打印以下信息代表连接github成功:

Hi ****! You've successfully authenticated, but GitHub does not provide shell access.

服务器公钥是与本地一致

在github上之前添加过的ssh公钥无法再次查看了,但是我们能看到改密钥的指纹 file

我们可以看看是否和自己本地的公钥匹配

ssh-keygen -E sha256 -lf ./github_rsa
ssh-keygen -E sha256 -lf ./github_rsa.pub

file

如果是需要通过md5来匹配的话,就执行

ssh-keygen -E md5 -lf ./github_rsa.pub

前面我们执行ssh-add -l查看的就列表里面的ssh的公钥指纹了,如果我们想看到它们完整的公钥信息的话,就得使用ssh-add -L

file

项目默认都使用ssh

git config --global user.name 用户名1
git config --global user.email 邮箱1

多个ssh之间切换

你日常开发需要同时使用gitlab和github账号对不同的项目进行管理,比如A项目使用gitlab账号,B项目使用github账号,这两个账号的用户名和邮箱一般不会相同。为了不用每次切换的时候都输入邮箱和密码,在上面配置了SSH访问的前提下,可以使用以下操作,一劳永逸。

#在A项目下
git config --local user.name 用户名1
git config --local user.email 邮箱1

#在B项目下
git config --local user.name 用户名2
git config --local user.email 邮箱2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK