CentOS 配置多个 GitHub 项目 SSH

CentOS 配置多个 GitHub 项目 SSH

我在 GitHub 上面创建了两个项目进行测试,需要先通过 ssh-keygen 生成两个 SSH ,注意修改存储位置避免被覆盖了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ssh-keygen -t rsa -C "hong@hongfs.cn"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/test1
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/test1.
Your public key has been saved in /root/.ssh/test1.pub.
$ ssh-keygen -t rsa -C "hong@hongfs.cn"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/test2
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/test2.
Your public key has been saved in /root/.ssh/test2.pub.

创建完成后把 public key 文件内容添加到 GitHub 相应项目的 Deploy keys。

进行 SSH 配置,通过不同的 Host 来区分要使用的 SSH 。

/root/.ssh/config

1
2
3
4
5
6
7
8
9
Host test1.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/test1

Host test2.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/test2

这样子就配置完成了,我们需要拉取项目下来了。

测试

第一个项目 clone

1
2
3
4
5
6
7
8
9
$ git clone git@test1.github.com:hongfs/test1.git
Cloning into 'test1'...
The authenticity of host 'github.com (140.82.113.3)' can't be established.
RSA key fingerprint is SHA256:nThbg7kXUpJWGl7E1IGOCspRomTxdCARLviKw1E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.113.3' (RSA) to the list of known hosts.
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0

第二个项目 clone

1
2
3
4
5
6
7
$ git clone git@test2.github.com:hongfs/test2.git
Cloning into 'test2'...
Warning: Permanently added the RSA host key for IP address '140.82.112.3' to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

第一个项目 pull

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cd test1
$ git pull
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From test1.github.com:hongfs/test1
31861d1..936c437 master -> origin/master
Updating 31861d1..936c437
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md

第二个项目 pull

1
2
3
4
5
6
7
8
9
10
11
12
$ cd ../test2
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From test2.github.com:hongfs/test2
9a207f0..524338d master -> origin/master
Updating 9a207f0..524338d
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
往上