博客统计信息

51cto推荐博客
用户名:jamm118
文章数:126
评论数:164
访问量:115264
无忧币:906
博客积分:1560
博客等级:6
注册日期:2009-03-29

ssh 无密码登陆
2010-01-02 16:36:53
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://shitou118.blog.51cto.com/715507/254368
在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的。ssh有一套很有用的工具,其中的ssh-keygen可以用来生成private和public密钥.将生成的public密钥拷贝到远程机器后,可以使ssh到另外一台机器的登陆不用密码.具体方法如下.1) 在本地机器中的~/.ssh/目录下执行下命令,ssh-keygen -t dsa将生成两个文件,id_dsa和id_dsa.pub.2) 将id_dsa.pub拷贝到远程机器,并且将id_dsa.pub的内容添加到~/.ssh/authorized_keys中.cat id_dsa.pub >>authorized_keys注意:目录.ssh和文件authorized_keys的权限必须是600.完成这些工作后,用户从本地机器到远程机器的登录就不用密码了.该设置简单实用.
 
以下是dsa加密算法实验过程,rsa 的一模一样。
 
[root@test3 .ssh]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
24:cb:cb:9f:29:48:60:fc:b2:a6:93:0a:f8:00:6f:60 root@test3.yuhang.gov.cn
[root@test3 .ssh]# scp ~/.ssh/id_dsa.pub 10.32.184.234:~/.ssh/
root@10.32.184.234's password:
id_dsa.pub                                                                                  100%  614     0.6KB/s   00:00
[root@test3 .ssh]# ssh 10.32.184.234
root@10.32.184.234's password:
Last login: Sat Jan  2 16:07:32 2010 from 10.32.184.233
[root@test4 ~]# cd .ssh/
[root@test4 .ssh]# ls
authorized_keys  id_dsa.pub  id_rsa  id_rsa.pub  known_hosts
[root@test4 .ssh]# cat id_dsa.pub >authorized_keys
[root@test4 .ssh]# exit
logout
Connection to 10.32.184.234 closed.
[root@test3 .ssh]# ssh 10.32.184.234
Last login: Sat Jan  2 16:08:59 2010 from 10.32.184.233
[root@test4 ~]#
 
 
以下是rsa实验:
[root@test4 ~]# vim .ssh/authorized_keys
[root@test4 ~]# exit
logout
Connection to 10.32.184.234 closed.
[root@test3 .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
b4:6a:da:19:b7:62:5a:8c:8c:b0:75:a9:b9:40:09:7e root@test3.yuhang.gov.cn
[root@test3 .ssh]# scp ~/.ssh/id_rsa.pub 10.32.184.234:~/
[root@test3 .ssh]# ls
id_dsa  id_dsa.pub  id_rsa  id_rsa.pub  known_hosts
[root@test3 .ssh]# scp ~/.ssh/id_rsa.pub 10.32.184.234:~/
root@10.32.184.234's password:
id_rsa.pub                                                                                  100%  406     0.4KB/s   00:00
[root@test3 .ssh]# ssh 10.32.184.234
root@10.32.184.234's password:
Last login: Sat Jan  2 16:09:43 2010 from 10.32.184.233
[root@test4 ~]# cp ~/id_rsa.pub ~/.ssh/authorized_keys
cp:是否覆盖“/root/.ssh/authorized_keys”? y
[root@test4 ~]#
[root@test4 ~]# exit
logout
Connection to 10.32.184.234 closed.
[root@test3 .ssh]# ssh 10.32.184.234
Last login: Sat Jan  2 16:13:25 2010 from 10.32.184.233
[root@test4 ~]#
 
 
如果要多台主机都这样无须通过密码直接登陆,只需将自己的rsa 或者是dsa 公钥附加到~/.ssh/authorized_keys文件中即可。
看下面例子:
[root@test3 ~]# ssh 10.32.184.232
The authenticity of host '10.32.184.232 (10.32.184.232)' can't be established.
RSA key fingerprint is 60:19:ce:e5:82:ff:aa:04:d5:a3:0c:36:40:bf:e3:51.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.32.184.232' (RSA) to the list of known hosts.
root@10.32.184.232's password:
Last login: Fri Jan  1 17:59:43 2010 from test0.yuhang.gov.cn
[root@test2 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
55:f9:02:15:b8:5e:3b:37:dd:0b:57:1e:b8:21:18:d5 root@test2.yuhang.gov.cn
[root@test2 ~]# scp .ssh/id_dsa.pub 10.32.184.234:~/
The authenticity of host '10.32.184.234 (10.32.184.234)' can't be established.
RSA key fingerprint is 60:19:ce:e5:82:ff:aa:04:d5:a3:0c:36:40:bf:e3:51.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.32.184.234' (RSA) to the list of known hosts.
root@10.32.184.234's password:
id_dsa.pub                                                                                  100%  614     0.6KB/s   00:00
[root@test2 ~]# ssh 10.32.184.234
root@10.32.184.234's password:
Last login: Sat Jan  2 16:14:42 2010 from 10.32.184.233
[root@test4 ~]# cat id_dsa.pub >> ~/.ssh/authorized_keys
[root@test4 ~]# exit
logout
Connection to 10.32.184.234 closed.
[root@test2 ~]# ssh 10.32.184.234
Last login: Sat Jan  2 16:21:25 2010 from 10.32.184.232
[root@test4 ~]#

注意:目录.ssh和文件authorized_keys的权限必须是600.
完成这些工作后,用户从本地机器到远程机器的登录就不用密码了.
该设置简单实用.
 
补充说明:
做完了刚才才发现openssh-clients安装包中还有一个工具自动的将客户端的public-key 复制到服务器端的authorized_keys文件中,相当方便。
工具名:ssh-copy-id
使用说明可以看man ssh-copy-id 手册

 

本文出自 “石头博客” 博客,请务必保留此出处http://shitou118.blog.51cto.com/715507/254368

分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:Linux技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
2010-01-05 16:31:00
哈哈 果然厉害!

2010-01-05 20:23:42
你好,我很冒昧,希望你不要介意。事情是这样的:
我家有个孩子离家出走了我们很着急找不到他,只知道他的QQ号码和痴迷的《地下城与勇士》的游戏,不知道你能否根据他的QQ号找到他的IP地址,无意中打开了你的博客,看到你的博文,感觉你是个高手吧,如果你可以帮我的话可以加我的QQ547710226
或者发邮件到WWZZX@126.COM,多谢了!
博主回复:
2010-01-07 08:54:59
这事情也有?真的假的?

 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: