曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4634|回复: 0
打印 上一主题 下一主题

无密码 ssh 连接

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34387
跳转到指定楼层
楼主
发表于 2010-10-15 00:27:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
实验环境:
服务器: CentOS 5.5
客户端: Ubuntu 10.04

在两台主机间拷贝文件,经常用到的是 scp 命令,但默认情况下是要输入密码的,如果经常要在这两台主机间拷贝文件,这样显然感觉有点麻烦。若是要使用脚本,你必须将密码以明文的形式放在脚本中,这回带来很大的安全性。

下面的方法可以让 ssh 进行无密码连接。

1. 生成密钥对,这里使用 rsa 密钥:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/beyes/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/beyes/.ssh/id_rsa.
Your public key has been saved in /home/beyes/.ssh/id_rsa.pub.
The key fingerprint is:
c7:a7:08:3c:97:bb:af:96:04:80:35:7c:26:4e:91:73 beyes@beyes-desktop
The key's randomart image is:
+--[ RSA 2048]----+
|   ++o           |
|  . B.E          |
|   o B           |
|    ...  o       |
|      +.S o .    |
|       +.+ o     |
|       .o..      |
|        o.       |
|       .oo.      |
+-----------------+
上面,Enter file in which to save the key (/home/beyes/.ssh/id_rsa): 提示输入保存密钥的路径,默认是括号里的路径,直接回车表示接受默认保存位置。
Enter passphrase (empty for no passphrase):  提示输入密码,如果不想每次都输入密码,这里仍然直接回车使密码留空。
这样,公钥就保存在:
Your public key has been saved in /home/beyes/.ssh/id_rsa.pub.
私钥保存在:
Your identification has been saved in /home/beyes/.ssh/id_rsa.

2. 使用以下命令,会自动将密钥文件上传到远程服务器上,并且创建相应的目录以及为密钥文件设定正确的权限:
$ ssh-copy-id -i .ssh/id_rsa.pub beyes@centos
beyes@centos's password:
Now try logging into the machine, with "ssh 'beyes@centos'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
上面,centos 是服务器。.ssh 是在服务器上的 beyes 这个用户的家目录下创建用来存放密钥文件的目录。authorized_keys 是被改名后的密钥文件。

3.  CentOS 系统的话,单单这么改也许还不成功(别的发行版也许到此就行了)。以下步骤可能是需要需要的,修改 /etc/ssh/sshd_conf 文件,将该文件中以下两行的注释去掉:
RSAAuthentication yes
PubkeyAuthentication yes
保存退出后,需要重启一下 sshd 服务:
#/etc/init.d/sshd restart

4. 现在尝试连接服务器
如果客户端是 ubuntu 系统,你可能直接连接服务器,会看到返回:
Agent admitted failure to sign using the key
这样的错误提示。
这样,需要先检查进程里是否包含有 ssh-agent 这个进程,如果没有就运行它。之后将 id_rsa 添加到 ssh-agent:
$ssh-add id_rsa

到此,应该可以无密码的 ssh 上服务器了。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-19 13:40 , Processed in 0.064069 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表