OPS Notes By 枯木

Ssh认证代理

| Comments

简介

ssh允许用户把密钥存储在内存中,这就是ssh认证代理[ssh-agent]。认证代理为用户提供了使用RSA密钥而不必随时键入口令字的能力,这对于不必在所有登录、X会话或运行脚本时都要键入口令字提供便利是很有效的。ssh-agent是个长时间持续运行的守护进程(daemon),设计它的唯一目的就是对解密的专用密钥进行高速缓存。ssh包含的内建支持允许它同ssh-agent通信,允许ssh不必每次新连接时都提示您要密码才能获取解密的专用密钥。

实例

Before ssh-agent

1
2
3
# ssh root@192.168.80.130
Enter passphrase for key '/root/.ssh/id_rsa':   # 正常输入密码
Last login: Sat Sep  7 12:40:02 2013 from 192.168.80.131

After ssh-agent

1
2
3
4
5
6
7
8
9
10
# ssh-agent bash
# ssh-add       
# 默认ssh-add添加~/.ssh/id_rsa私钥,按提示输入密码即可,可添加多个密钥
# 如果要加入其它密钥直接 ssh-add 私钥文件 即可
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
# ssh-add -l                    # 显示添加密钥添加列表
2048 4f:a8:76:04:42:90:b4:18:c1:6b:13:06:31:c8:59:bf /root/.ssh/id_rsa (RSA)
# ssh root@192.168.80.130       # 不需要密码登录了
Last login: Sat Sep  7 12:58:09 2013 from 192.168.80.128

ssh-agent + ForwardAgent

A->B->C…

1
2
3
4
# ssh root@192.168.80.130
Last login: Sat Sep  7 14:05:34 2013 from 192.168.80.128
# ssh root@192.168.80.131       # 默认ForwardAgent默认值为no,因此登录到B时再登录C需要密码了
Enter passphrase for key '/root/.ssh/id_rsa':

如果想A->B->C…都不需要密码则可以设置~/.ssh/config如下

1
2
Host *
    ForwardAgent yes

设置之后

1
2
3
4
# ssh root@192.168.80.130
Last login: Sat Sep  7 14:06:01 2013 from 192.168.80.128
# ssh root@192.168.80.131       # 没有再次提示输入密码,直接登录
Last login: Sat Sep  7 14:05:20 2013 from 192.168.80.130

参考和拓展文档

–EOF–

Comments