以前遇到过一个问题,在用有些 Linux 发行版时,用 ssh-keygen 产生好了密钥对并上传到了目标服务器,但每次登录都要重新输入。

这与 ssh-agent 有关,看如下 man ssh-agent 的输出:

ssh-agent is a program to hold private keys used for public key authentication (RSA, DSA). The idea is
that ssh-agent is started in the beginning of an X-session or a login session, and all other windows or
programs are started as clients to the ssh-agent program. Through use of environment variables the agent
can be located and automatically used for authentication when logging in to other machines using ssh(1).

所以快速的解决办法是启动 ssh-agent 并且导入 key 文件:

# eval `ssh-agent -s`       # ssh-agent 依赖于特殊环境变量,使用 eval 以正确产生环境变量
# ssh-add path/to/.ssh/id_rsa__or__id_dsa

这样就可以使用密钥自动登录 ssh。

但这样会有个问题,当退出运行 ssh-agent 的会话后,ssh-agent 会被关闭,这样一来会影响后续密钥认证过程,导致自动 ssh 失败、ssh 脚本运行出错…

对于这些问题,stackexchange 上的这篇文章给出了详细全面的回答:

对于 Gnome,GNOME SSH Keyring Agent 较好地处理了 ssh-key 的使用问题。

对于非 gnome 的 bash 环境,可以将如下命令添加到 ~/.bash_profile 中来加载无密码的 ssh-key:

if [ -z "$SSH_AUTH_SOCK" ] ; then     # -z means not null string
eval `ssh-agent -s`
ssh-add
fi

如果 ssh-key 有密码,可以考虑在脚本中使用 expect 自动交互加载 key files,这点在文章中有提及。

如果想让 ssh-agent 在后台一直工作,可以考虑使用 keychain,keychain 能让 cron 等工作顺利进行。

安全性与便利性不可兼得,以上方式中,最不方便使用的是每次会话开始都调用 ssh-agent 并为 key 输入密码,但这种方式安全性最好。最方便的是 keychain,但安全性最差。

Linux SSH: key, agent, keychain的更多相关文章

  1. linux ssh key配置方法

    转自:http://blog.csdn.net/zzk197/article/details/7915307 一:简洁的配置文件[root@cisco ~]# vi /etc/ssh/sshd_con ...

  2. (诊断)为GitHub添加SSH key时出现“Could not open a connection to your authentication agent”错误的应对方案(转)

    在为windows 环境下的github账户添加SSH key时,需要在Git Bash执行如下命令: 第一步:检查已有的SSH keys $ ls -al ~/.ssh 第二步:生成新的SSH ke ...

  3. 【Linux】配置SSH Key到GitHub/GitLab

    Linux配置SSH Key到GitHub/GitLab 准备工作 首先检查下本机是否已经安装了SSH,在终端输入ssh即可: 如果没有安装进行yum安装 # yum -y install opens ...

  4. Linux中ssh介绍与ssh+key密钥登陆部署

    环境内核信息: [root@zabbix- ~]# uname -a Linux zabbix- -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 x86 ...

  5. Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking.错误

    Linux SSH命令用了那么久,第一次遇到这样的错误:ECDSA host key "ip地址" for  has changed and you have requested ...

  6. linux ssh使用深度解析(key登录详解)

    linux ssh使用深度解析(key登录详解) SSH全称Secure SHell,顾名思义就是非常安全的shell的意思,SSH协议是IETF(Internet Engineering Task ...

  7. Linux配置使用SSH Key登录并禁用root密码登录(替换同理)

    Linux系统大多说都支持OpenSSH,生成公钥.私钥的最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server r ...

  8. Linux配置使用SSH Key登录并禁用root密码登录

    Linux系统大多数都支持OpenSSH,生成公钥.私钥的最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server r ...

  9. linux生成SSH key

    1. 检查SSH keys是否存在 ls -al ~/.ssh2. 生成新的ssh key 输入 ssh-keygen -t rsa -C your_email@example.com

随机推荐

  1. HTML入门教程(全套)

    http://www.rm5u.com/html_html.html http://learn.shayhowe.com/  moe.mwulu.com  http://www.w3school.co ...

  2. 《STL源码剖析》环境配置

    首先,去侯捷网站下载相关文档:http://jjhou.boolan.com/jjwbooks-tass.htm. 这本书采用的是Cygnus C++ 2.91 for windows.下载地址:ht ...

  3. php pack、unpack、ord 函数使用方法

    string pack ( string $format [, mixed $args [, mixed $... ]] ) Pack given arguments into a binary st ...

  4. PKU 1064 Cable master

    题目链接:点击打开链接 有n段绳子,给定n段绳子的长度,单位为厘米.求能够把这些绳子分成k段的最长的段的长度.题目中的trick是最小是1cm,长度不能小于1cm,因此要转换成int来解,然后二分可以 ...

  5. 【算法Everyday】第三日 KMP算法

    题目 你知道的. 分析 分析不来. 代码 void OutputArray(int* pArr, int iLen) { ; i < iLen; i++) { printf("%d\t ...

  6. 数学(逆元):BZOJ 2186: [Sdoi2008]沙拉公主的困惑

    2186: [Sdoi2008]沙拉公主的困惑 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞 ...

  7. Android安全bug ANDROID-8219321

    ANDROID-8219321漏洞主要源自Android ZipFile函数漏洞:没有进行校验重名entry逻辑漏洞,逻辑漏洞细节详见Google+文章和Bluebox Security提报Andro ...

  8. WebView js 调用Java本地方法

    webView = (WebView) this.findViewById(R.id.webview); WebSettings webSettings = webView.getSettings() ...

  9. codevs3945 完美拓印

    3945 完美拓印 codevs月赛 第一场 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 小Q获得了一个神奇的印章,这个印章宽n ...

  10. memkeys 安装时遇到的问题及解决办法

    某天由于某需要,安装tumblr的开源工具memkeys .但还是一如既往地不是一帆风顺. 在./configure 时出现如下错误信息: configure.in:14: error: possib ...