您的位置:首页 > 运维架构 > Linux

同一台Linux电脑关于多个SSH KEY管理

2017-02-14 13:21 309 查看
使用环境:关于同一台电脑LInux系统下使用多个SSH key 切换使用(或者多用户使用ssh提交代码)要求,可以创建不同的 PUBLIC KEY ,根据下面步骤设置.(1)比如有aaa,bbb,ccc 三个帐号需要生成不同的 PUBLIC KEY
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_aaa.
Your public key has been saved in /root/.ssh/id_rsa_aaa.pub.
The key fingerprint is:
9b:92:f6:1f:d2:72:bd:72:19:45:42:5f:e4:65:33:64 root@AY140122145815620396Z
The key's randomart image is:
+--[ RSA 2048]----+
|           .. .E=|
|            ..o++|
|             o. .|
|              .  |
|        S    .   |
|       . + ..    |
|      + = + .o   |
|     . o +..o.   |
|        ...o.    |
+-----------------+
进过上面的shell 我们在 /root/.ssh/ 目录下创建 id_rsa_aaa 私钥 和 id_rsa_aaa.pub 公钥注意这里
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa  #设置路径,如果不设置默认生成 id_rsa  和  id_rsa.pub
(2)按照上面的步骤逐个生成 bbb 和 ccc 对应的公钥和私钥 ........
(3)查看系统ssh-key代理,执行如下命令
$ ssh-add -l
Could not open a connection to your authentication agent.
如果发现上面的提示,说明系统代理里没有任何key,执行如下操作
exec ssh-agent bash
如果系统已经有ssh-key 代理 ,执行下面的命令可以删除
$ ssh-add -D

(4)把 .ssh 目录下的3个私钥添加的 ssh-agent
$ ssh-add ~/.ssh/id_rsa_aaa
$ ssh-add ~/.ssh/id_rsa_bbb
$ ssh-add ~/.ssh/id_rsa_ccc
依次执行上面三条shell 把三个私钥添加到 ssh-key 代理里面(5)打开github 或者 开源中国 ssh 管理页面把 对应的公钥提交保存到代码管理服务器 (.pub 结尾)(6)在 .ssh 目录创建 config 配置文件
nano ~/.ssh/config
输入如下配置信息
#aaa  (github 配置)
Host aaa
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_aaa

#bbb  (开源中国 配置)
Host bbb
HostName git.oschina.net
User git
IdentityFile ~/.ssh/id_rsa_bbb

#ccc
........
(6)记住上面一步 Host 里设置的别名,开始克隆项目,以开源中国为例
git clone git@git.oschina.net:userName/projectName.git
附:进阶配置,已经测试通过:

Host 192.168.0.19,192.168.0.25
HostName 192.168.0.19,192.168.0.25
IdentityFile ~/.ssh/id_rsa.pub
User billtan
Host
HostName www.test.com
IdentityFile ~/.ssh/www.test.com.pub
PreferredAuthentications publickey
User admin
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多个 免密码 ssh公钥