首页 > 代码库 > 【CentOS】SSH实现免密码登录与文件分发
【CentOS】SSH实现免密码登录与文件分发
实验环境:
10.0.0.9:分发服务器(用于保存SSH生成的密钥和后期的文件的分发工作)
10.0.0.10、10.0.0.11:节点服务器(用于保存SSH生成的公钥文件和接收分发服务器的文件)
#分发服务器与节点服务器的系统版本与内核如下 [root@C58/]# cat /etc/redhat-release CentOS release 5.8 (Final) [root@C58/]# uname -a Linux C58 2.6.18-308.el5 #1 SMP Tue Feb 21 20:06:06 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
IP的分配:
分发服务器IP:10.0.0.9 节点服务器A的IP:10.0.0.10 节点服务器B的IP:10.0.0.11
开始实验:
1、在分发服务器上面使用SSH生成一对密钥与公钥
[root@C58-NFS-Server ~]# ssh-keygen -t rsa #生成以rsa加密算法的公钥与密钥 Generating public/private rsa key pair. #默认密钥存放的位置在当前用户的根目录下的.ssh目录中 Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh‘. 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: 9f:82:dc:54:7f:0b:aa:64:d5:56:e5:2d:eb:50:72:76 root@C58-NFS-Server 进入.ssh目录,查看是否生成密钥文件 [root@C58-NFS-Server ~]# cd ~/.ssh/ [root@C58-NFS-Server .ssh]# ll total 16 -rw------- 1 root root 1675 Jan 22 21:53 id_rsa -rw-r--r-- 1 root root 401 Jan 22 21:53 id_rsa.pub
2、使用ssh-copy-i将公钥文件id_rsa.pub发送到节点服务器A上
[root@C58-SSH-Server .ssh]# ssh-copy-id -i id_rsa.pub 10.0.0.10 10 The authenticity of host ‘10.0.0.10 (10.0.0.10)‘ can‘t be established. RSA key fingerprint is 53:23:dc:c0:66:05:e6:34:b1:ee:b2:f6:e5:d5:f1:de. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘10.0.0.10‘ (RSA) to the list of known hosts. root@10.0.0.10‘s password: Now try logging into the machine, with "ssh ‘10.0.0.10‘", and check in: .ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting. #在节点服务器上面查看是否传输成功(有authorized_keys,说明成功) [root@C58-SSH-ClientA .ssh]# cd ~/.ssh [root@C58-SSH-ClientA .ssh]# ll total 8 -rw------- 1 root root 401 Jan 22 22:06 authorized_keys
3、进行验证
#在节点服务器上使用ssh登录到节点服务器A,验证是否需要密码 [root@C58-SSH-Server .ssh]# ssh 10.0.0.10 Last login: Wed Jan 22 22:05:18 2014 from 10.0.0.1 [root@C58-SSH-ClientA ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:B0:82:67 inet addr:10.0.0.10 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb0:8267/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2803 errors:0 dropped:0 overruns:0 frame:0 TX packets:2301 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:342373 (334.3 KiB) TX bytes:275960 (269.4 KiB) #在节点服务器上使用ssh登录到节点服务器B,验证是否需要密码 [root@C58-SSH-Server .ssh]# ssh 10.0.0.11 #要求输入密码 root@10.0.0.11‘s password: Last login: Wed Jan 22 18:09:28 2014 from 10.0.0.9 [root@C58-SSH-ClientB ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:33:22:7E inet addr:10.0.0.11 Bcast:10.0.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe33:227e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1474 errors:0 dropped:0 overruns:0 frame:0 TX packets:1080 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:181772 (177.5 KiB) TX bytes:127440 (124.4 KiB)
4、文件分发测试
#分发文件aa到节点服务器A [root@C58-SSH-Server tmp]# scp -p 22 aa 10.0.0.10:/tmp/ 22: No such file or directory aa 100% 0 0.0KB/s 00:00 [root@C58-SSH-ClientA tmp]# ll total 4 -rw-r--r-- 1 root root 0 Jan 22 18:11 aa #分发文件aa到节点服务器B [root@C58-SSH-Server tmp]# scp -p 22 aa 10.0.0.11:/tmp/ root@10.0.0.11‘s password: 22: No such file or directory aa 100% 0 0.0KB/s 00:00
注意
该认证只是单向的,即从分发服务器ssh登录到节点服务器上是无须密码的,而反过来则需要。
ssh-copy-id所能传送的只能是公钥,无法传送密钥文件
本文出自 “NowSun” 博客,请务必保留此出处http://nowsun.blog.51cto.com/522159/1427325
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。