Skip to content

为不同域名下的Git远程仓库分配不同的SSH key #19

@rainyear

Description

@rainyear

一般私人的Git远程仓库采用SSH方式进行git push & git pull操作,而不用每次输入用户名和密码。其中的原理是采用SSH协议,将公钥(如~/.ssh/id_rsa.pub)上传到服务端,客户端保留私钥(如~/.ssh/id_rsa)。创建密钥的指令:

ssh-keygen -t rsa -C "name@your.domain"
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/rainyear/.ssh/id_rsa): 

一直回车就可以在~/.ssh/目录下分别生成一个公钥文件和一个私钥文件。虽然就目前来看SSH协议还是安全的,也就是说其他人活的公钥之后是无法推算出私钥的。但是为了谨慎起见,最好还是不要到处暴露同一公钥。例如我在github.com上保存了自己的SSH-key(也就是~/.ssh/id_rsa.pub),那么在另外一家服务如coding.io最好采用另外一对私钥/公钥,这时需要创建新的密钥:

ssh-keygen -t rsa -C "name@your.domain"
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/rainyear/.ssh/id_rsa):  /home/rainyear/.ssh/id_rsa_coding

生成新的密钥~/.ssh/id_rsa_coding & ~/.ssh/id_rsa_coding.pub,然后添加配置文件~/.ssh/config

Host github.com
  HostName github.com
  User git
  IdentityFile /Users/rainy/.ssh/id_rsa
  IdentitiesOnly yes

Host git.coding.net
  HostName git.coding.net
  User git
  IdentityFile /Users/rainy/.ssh/id_rsa_coding
  IdentitiesOnly yes

~/.ssh/id_rsa_coding.pub添加到coding.io帐号的SSH-key,接下来针对不同域名下的远程仓库,git将实用不同的私钥进行SSH操作。

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions