Color Scheme
Find People
Username
Password
Sign Up
old site http://222.195.74.11/
中文版 Location: Homepage >> Chenglong Shen : CV - Publications - Blog

IDL & Linux

Poster:

1. 如何实现基于ssh密钥对的自动登录[转贴]
如何实现基于ssh密钥对的自动登录

http://www.faq-it.org/archives/aix/ca7c381ac975c2cac58e7a8c3fb3bc18.php
作者:未知

下面从整体上粗略的介绍了 RSA/DSA 密钥的工作原理。让我们从一种假想的情形开始,假定我们想用 RSA 认证允许一台本地的计算机(称作 localbox)打开 remotebox上的一个远程 shell, remotebox 是我们的 ISP 的一台机器。此刻,当我们试图用 ssh 客户程序连接到 remotebox时,我们会得到如下提示:

%
ssh drobbins@remotebox
drobbins@remotebox's password:

此处我们看到的是 ssh 处理认证的缺省方式的一个示例。换句话说,它要求我们输入 remotebox上的 drobbins 这个帐户的密码。如果我们输入我们在 remotebox 上的密码, ssh 就会用安全密码认证协议,把我们的密码传送给 remotebox 进行验证。但是,和 telnet 的情况不同,这里我们的密码是加密的,因此它不会被偷看到我们的数据连接的人截取。一旦 remotebox 把我们提供的密码同它的密码数据库相对照进行认证,成功的话,我们就会被允许登录,还会有一个 remotebox 的 shell 提示欢迎我们。虽然 ssh 缺省的认证方法相当安全,RSA 和 DSA 认证却为我们开创了一些新的潜在的机会。

但是,与 ssh 安全密码认证不同的是,RSA 认证需要一些初始配置。我们只需要执行这些初始配置步骤一次。之后, localbox 和 remotebox 之间的 RSA 认证就毫不费力了。要设置 RSA 认证,我们首先得生成一对密钥,一把专用密钥和一把公用密钥。这两把密钥有一些非常有趣的性质。公用密钥用于对消息进行加密,只有拥有专用密钥的人才能对该消息进行解密。公用密钥只能用于 加密,而专用密钥只能用于对由匹配的公用密钥编码的消息进行 解密。RSA(和 DSA)认证协议利用密钥对的这些特殊性质进行安全认证,并且不需要在网上传输任何保密的信息。

要应用 RSA 或者 DSA 认证,我们要执行一步一次性的配置步骤。我们把 公用密钥拷贝到 remotebox。公用密钥之所以被称作是“公用的”有一个原因。因为它只能用于对那些给我们的消息进行加密,所以我们不需要太担心它会落入其它人手中。一旦我们的公用密钥已经被拷贝到 remotebox并且为了 remotebox 的 sshd 能够定位它而把它放在一个专门的文件(~/.ssh/authorized_keys)里,我们就为使用 RSA 认证登录到 remotebox上做好了准备。

要用 RSA 登录的时候,我们只要在 localbox 的控制台键入 ssh drobbins@remotebox ,就象我们常做的一样。可这一次, ssh 告诉 remotebox 的 sshd 它想使用 RSA 认证协议。接下来发生的事情非常有趣。 Remotebox 的 sshd 会生成一个随机数,并用我们先前拷贝过去的公用密钥对这个随机数进行加密。然后, sshd 把加密了的随机数发回给正在 localbox 上运行的 ssh 。接下来,轮到我们的 ssh 用 专用密钥对这个随机数进行解密后,再把它发回给 remotebox,实际上等于在说:“瞧,我确实有匹配的专用密钥;我能成功的对您的消息进行解密!”最后, sshd 得出结论,既然我们持有匹配的专用密钥,就应当允许我们登录。因此,我们有匹配的专用密钥这一事实授权我们访问 remotebox。

配置localbox(ssh客户机)

请用您的用户账户在localbox上登录,并运行命令ssh-keygen -t rsa:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/myid/.ssh/id_rsa):
Created directory '/home/myid/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/myid/.ssh/id_rsa.
Your public key has been saved in /home/myid/.ssh/id_rsa.pub.
The key fingerprint is:
f1:e8:ae:a7:b3:f6:64:3f:30:34:1f:c5:07:ce:0f:bc myid@localbox

为了实现自动登录, passphrase为空。

注意:由于passphrase为空,生成的私钥必须绝对禁止未授权的访问!

将生成的id_rsa.pub文件复制到remotebox的/home/drobbins/.ssh/目录下。


配置remotebox(ssh服务器)

请以drobbins账户登录。运行以下命令:
cd .ssh
umask 077
cat id_rsa.pub > authorized_keys

测试ssh自动登录

请用您的用户账户在localbox上登录,并运行命令ssh drobbins@remotebox。您应该能够在不输入drobbins@remotebox口令的情况下登录到remotebox。
Chenglong Shen
Posted at 2007-02-01 09:14 CST
Last modified at 2007-02-01 09:27 CST
Link to this poster
Source Code
Top


Locations | Statistics | FAQ
Best view under the 1024x768 resolution and with the normal size of fonts.
Designed & developed by Yuming Wang, run for 6340 days since December 15, 2006, and last upgraded on May 28th, 2009.
Powered by Apache + PHP coded cOpfOs 3.11.1 last upgraded on February 22nd, 2010.
Copyright © 2006, all rights reserved.