Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git多帐号配置

在使用git,gitlab的时候,会在.ssh目录下生成对应rsa文件,那如果是有多个配置,该怎么处理?

如何生成ssh密钥,可以参考 generating-ssh-keys
##1. 多帐号配置 需要在.ssh目录下,增加config配置,config可以配置多个git的帐号

 #Host myhost(这里是自定义的host简称,以后连接远程服务器就可以用命令ssh myhost)[注意下面有缩进]
     #User 登录用户名(如:git)
     #HostName 主机名可用ip也可以是域名(如:github.com或者bitbucket.org)
     #Port 服务器open-ssh端口(默认:22,默认时一般不写此行
     #IdentityFile 证书文件路径(如~/.ssh/id_rsa_*)

具体的实例配置

 #github yeungeek@gmail.com
host github
    hostname github.com
    User yeungeek
    IdentityFile /home/yeungeek/.ssh/github_id_rsa

  #gitlab yeungeek@gmail.com
host gitlab
    hostname gitlab.widget-inc.com
    User yeungeek
    Port 65422
    IdentityFile /home/yeungeek/.ssh/id_rsa
host oschina
    hostname git.oschina.net
    User yeungeek
    IdentityFile /home/yeungeek/.ssh/oschina_id_rsa

注意点

  • 在配置文件中的,IdentityFile文件位置是rsa密钥,不是pub文件
  • 提交代码的时候,需要修改git config 可以之设置一个全局的user.email和user.name,然后需要不同的配置的仓库,单独设置
//设置global
git config --global user.name "yeungeek"
git config --global user.email "yeungeek@gmail.com"

//设置仓库的user.email和user.name
git config  user.email "yeungeek@gmail.com"
git config  user.name "yeungeek"

这样配置就ok了

##2. ssh-add说明 上面的配置完成完成,使用命令ssh-add -l可以看到所有的密钥列表

yeungeek@yeungeek:~/.ssh$ ssh-add -l
2048 db:6a:da:46:6e:03:da:94:f0:2c:43:1d:91:8a:bd:67 yeungeek@gmail.com (RSA)
2048 07:59:9f:93:d2:e3:80:e1:df:77:ed:c9:5d:2e:3d:04 yeungeek@gmail.com (RSA)
2048 4f:ba:6c:00:12:41:0e:a8:50:be:f5:6e:2e:7a:10:91 yeungeek@gmail.com (RSA)

ssh-add的作用主要将密钥添加到 ssh-agent 的高速缓存中,这样在当前会话中就不需要再次输入密码了 具体的可以参考 SSH Keys

@AgoniNemo

This comment has been minimized.

Copy link

@AgoniNemo AgoniNemo commented Nov 24, 2017

在使用ssh -T git@gitlab-zxf测试时,返回ssh: Could not resolve hostname gitlab-zxf:1100: nodename nor servname provided, or not known是什么问题?

@myclcs

This comment has been minimized.

Copy link

@myclcs myclcs commented Apr 18, 2019

同问

@amither

This comment has been minimized.

Copy link

@amither amither commented Apr 23, 2019

~/.ssh/config文件中User要写成git才行

@dreamdameng

This comment has been minimized.

Copy link

@dreamdameng dreamdameng commented Sep 12, 2019

请问这个是什么问题$ ssh -T git@github.com
kex_exchange_identification: read: Connection reset by peer

@zhangguanzhang

This comment has been minimized.

Copy link

@zhangguanzhang zhangguanzhang commented Nov 16, 2020

User内网gitlab不用写,其次记得ssh-add 私钥,多个git的用户信息配置可以这样

[user]
	name = zhangguanzhang
	email = zhangguanzhang@qq.com
[winUpdater]
	recentlySeenVersion = 2.24.0.windows.1
[includeIf "gitdir:D:/workCode/**"]
    path = ~/.gitconfig_company
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.