> ## Content Index
> Fetch the complete content index at: https://blog.hacking.pub/llms.txt
> Use this file to discover other available public pages before exploring further.

# 管理多个ssh账号
- URL: https://blog.hacking.pub/2016/09/20/guan-li-duo-ge-sshzhang-hao/
- Published: 2016-09-20T05:43:24.000Z
- Updated: 2016-09-20T05:43:24.000Z
- Author: chainhelen

#### 公钥秘钥生成

```
ssh-keygen -t rsa

```

###### 1.查看ssh-key代理

```
ssh-add -l 

```

如果出现如下:  
Could not open a connection to your authentication agent.  
则说明系统代理中没有任务key  
那么执行如下

```
exec ssh-agent bash

```

同时如果需要清除ssh-key代理

```
ssh-add -D

```

###### 2.添加ssh-key代理

把.ssh目录下的秘钥加到ssh-agent

```
ssh-add ~/.ssh/id_rsa_test
ssh-add ~/.ssh/id_rsa_api
ssh-add ~/.ssh/id_rsa_mine

```

每次开一个bash，都需要手动添加比较麻烦，故在\~/.ssh目录下添加.bash\_profile，内容如下:

```
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa_mine
ssh-add ~/.ssh/id_rsa_test
ssh-add ~/.ssh/id_rsa_api

```

###### 3.添加本机配置文件

```
vim ~/.ssh/config

```

配置如下:

```
HOST test  //别名
   HostName 120.24.xxx.xxx //服务器地址
   User root  //登陆用户
   IdentityFile ~/.ssh/id_rsa_test //采用哪个私钥
HOST api
   HostName 120.25.xxx.xxx
   User root
   IdentityFile ~/.ssh/id_rsa_api
HOST mine
   HostName 162.243.xxx.xxx
   User root
   IdentityFile ~/.ssh/id_rsa_mine

```

###### 4.服务器

在服务器端\~/.ssh/目录下，使用如下命令创建文件authorized\_keys

```
cat id_rsa.public > authorized_keys

```

###### 5.其他

此时可以使用别名进行操作了

```
ssh api                 //或者
scp api:/ ./

```