ssh 无密码设置

情景:服务器A 通过ssh 服务器B无需输入密码,像可以用于crontab执行rsync等服务中
aip: 192.168.16.240 bip:192.168.95.200
第一步:First log in on A as user a and generate a pair of authentication keys
转到root目录下,执行以下命令
a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
此时就会在A机器的当前用户的根目录下产生一个私钥跟公钥文件,下一步需要将公钥文件拷贝到B机器上
第二步:
a@A:~>scp /a/.ssh/id_rsa.pub [email protected] :/root/
[email protected]'s password:
id_rsa.pub                                    100% 392     0.4KB/s   00:00
只不过把公钥文件拷贝到了b机器,可以放在任意一个位置,只要你愿意就好啦

第三步:进入b机器,将公钥加入到B机器的authorized_keys下就可以了,如果在B机器的
/root/.ssh/下有此文件的话就直接加入里面好了,若没有,需要自己创建的:
a@A:~>ssh [email protected]  ‘cat id_dsa.pub >> /root/.ssh/authorized_keys’
[email protected]'s password:
此时已经完成了将公钥文件导入了B的文件认证文件中

第四步:修改authorized_keys的权限
[root@hostB root]# chmod 600 /root/.ssh/authorized_keys
 
第五步:实验
此时ssh">a@A:~>ssh [email protected]
便不需要密码啦!
到此为止,已经顺利完成啦,哇哈哈!
 

基本 原理
如果你希望从A作为用户user1 SSH 到B作为用户user2,若要用RSA键对法认证且不需要密码,则B上的ssh后台程序拿出~user2/.ssh/authorized_keys中的与A有关的RSA公钥来对A上的以user1身份运行的ssh客户程序进行挑战,前面我们已经把A上的identity.pub拷贝到B上作为authorized_keys了。则A保持私钥identity,而B上的sshd又拿出A的公钥来挑战,因此成功,你可以加很多行到authorized_keys以允许其它 服务器的公钥可以加进来。
实际 应用举例:
使用SSH命令连接一台服务器,默认情况下会提示我们输入密码:
willis:~ # ssh 192.168.4.24
Password:
创建密钥:
willis:~ # 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:
ae:1e:a6:49:b9:51:1e:27:bc:a6:7d:dd:bb:6b:1a:55root@willis
willis:~ #
id_rsa.pub是公钥,id_rsa是私钥,known_hosts保存着所有已发生连接服务器的信息,需要将公钥里的信息拷贝到登陆服务器用户主目录中的隐藏目录.ssh的文件authorized_keys中
 
参考网站:http://linuxproblem.org/art_9.html
http://hi.baidu.com/jianxin33333/blog/item/34fe861006e81208213f2e55.html
 

你可能感兴趣的:(职场,ssh,无密码,休闲)