为不同的Git服务器配置不同的SSH认证
发布于 / 2017-08-21
简介:在开发过程中,使用的Git仓库可能来自多个网站,如果此时在每个Git仓库网站使用的用户名和邮箱是相同的,只需要生成一套SSH公私钥即可;但有时候我们在每个网站使用的用户名和邮箱并不相同,此时就需要每个网站配置不同的SSH公私钥;这种模式是可以实现的,只需要使用SSH的config文件进行配置即可;下面将详细介绍如何操作。
在开发过程中,使用的Git仓库可能来自多个网站,如果此时在每个Git仓库网站使用的用户名和邮箱是相同的,只需要生成一套SSH公私钥即可;但有时候我们在每个网站使用的用户名和邮箱并不相同,此时就需要每个网站配置不同的SSH公私钥;这种模式是可以实现的,只需要使用SSH的config文件进行配置即可;下面将详细介绍如何操作。
1. 生成本地SSH公私钥
我们一般会使用ssh-keygen
命令生成公私钥,一般的操作中会一路回车生成默认的文件名。当我们需要生成多对公私钥时,就应该分开命名。如,我们先为Github上的仓库生成公私钥:
- $> ssh-keygen -t rsa -C "i@coderap.com"
- Generating public/private rsa key pair.
- Enter file in which to save the key (/Users/LennonChin/.ssh/id_rsa): /Users/LennonChin/.ssh/id_rsa_github.com
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /Users/LennonChin/.ssh/id_rsa_github.com.
- Your public key has been saved in /Users/LennonChin/.ssh/id_rsa_github.com.pub.
- The key fingerprint is:
- SHA256:UlOzLSDjccgrfIjZOXWqoZGERqa9YqiuEP4VOwHwzzg i@coderap.com
- The key's randomart image is:
- +---[RSA 2048]----+
- |.= .+.o o |
- |+o+ .+=.o + |
- |o..O +.+o o . |
- |. =.& +. . . |
- |+..E @. S |
- |+o. o +. |
- |o. + |
- |o . . . |
- |o. . |
- +----[SHA256]-----+
上面的命令生成了注释信息为i@coderap.com
的公私钥文件,建议在生产时指定该注释信息,方便后期管理。在出现Enter file in which to save the key (/Users/LennonChin/.ssh/id_rsa):
提示时,我们应该手动为生成的文件指定存储路径和文件名,上面的例子中我们指定为/Users/LennonChin/.ssh/id_rsa_github.com
,此时会在/Users/LennonChin/.ssh/目录下生成id_rsa_github.com和id_rsa_github.com.pub两个文件,分别对应私钥和公钥。
如法炮制,我们为bitbucket.org生成指定的公私钥文件:
- $> ssh-keygen -t rsa -C "me@coderap.com"
- Generating public/private rsa key pair.
- Enter file in which to save the key (/Users/LennonChin/.ssh/id_rsa): /Users/LennonChin/.ssh/id_rsa_bitbucket.org
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /Users/LennonChin/.ssh/id_rsa_bitbucket.org.
- Your public key has been saved in /Users/LennonChin/.ssh/id_rsa_bitbucket.org.pub.
- The key fingerprint is:
- SHA256:J1OeTjn2rISLxMuv4ynrJDxt3ERCI+igxJsOOsuHcRk me@coderap.com
- The key's randomart image is:
- +---[RSA 2048]----+
- |... o |
- |oo o . |
- |= o . . . |
- |o+ E o o o |
- |+ o . S O |
- |o+ = + O + |
- |.oB = + . o o |
- |.o *.o.+ o . |
- | ..o+B+o . |
- +----[SHA256]-----+
在操作完后,我们会在/Users/LennonChin/.ssh/路径下得到下面的几个文件:
- $> ls -al
- total 32
- drwxr-xr-x 6 LennonChin staff 192 3 4 18:10 .
- drwx------ 11 LennonChin staff 352 3 4 18:10 ..
- -rw------- 1 LennonChin staff 1679 3 4 18:07 id_rsa_bitbucket.org
- -rw-r--r-- 1 LennonChin staff 396 3 4 18:07 id_rsa_bitbucket.org.pub
- -rw------- 1 LennonChin staff 1675 3 4 18:04 id_rsa_github.com
- -rw-r--r-- 1 LennonChin staff 395 3 4 18:04 id_rsa_github.com.pub
2. 创建SSH Config文件
有了上面的准备后,我们子啊在/Users/LennonChin/.ssh/目录下创建一个文件名为config
的文件,然后在该文件配置以下内容:
- # 配置github.com
- Host github.com
- HostName github.com
- IdentityFile /Users/LennonChin/.ssh/id_rsa_github.com
- PreferredAuthentications publickey
- User LennonChin
- # 配置bitbucket.org
- Host bitbucket.org
- HostName bitbucket.org
- IdentityFile /Users/LennonChin/.ssh/id_rsa_bitbucket.org
- PreferredAuthentications publickey
- User LennonChin
配置解释如下:
- Host:可以取为任意内容,用于标识仓库的配置,但该配置会影响相关命令,例如配置Host为
gittest
时,那么在使用SSH访问Git仓库时命令中的地址应该为git@gittest:LennonChin/Test.git
,即git@
后面紧跟的名字改为gittest
。 - HostName:该配置填写仓库的真实的域名或IP地址。
- IdentityFile:该配置即为生成的私钥文件的路径。
- PreferredAuthentications:配置登录时用什么权限认证,可设为
publickey
、password publickey
、keyboard-interactive
等。 - User:配置使用用户名,需要与Git仓库使用的用户名对应。
3. 测试
做完上面的准备后,分别在对应的Git仓库网站中添加SSH公钥,然后使用下面的命令分别测试一下:
- $> ssh -T git@github.com
- Hi LennonChin! You've successfully authenticated, but GitHub does not provide shell access.
- $> ssh -T git@bitbucket.org
- logged in as LennonChin.
- You can use git or hg to connect to Bitbucket. Shell access is disabled.
接下来就可以使用Git进行各类操作了。另外需要注意的是,此时我们在本地的每个Git仓库应该单独设置Git的user.name和user.email的配置,覆盖Git的全局配置,如在Github的仓库中:
- $> git config user.name "LennonChin"
- $> git config user.email "i@coderap.com"
推荐阅读
Java多线程 46 - ScheduledThreadPoolExecutor详解(2)
ScheduledThreadPoolExecutor用于执行周期性或延时性的定时任务,它是在ThreadPoolExe...