A-A+

使用ssh公钥实现ssh免密码登录

2016年01月04日 站长资讯 暂无评论

昨晚熬夜测试了使用SSH公钥来进行无密码远程登录,一切都比较顺利,下面是这次测试的详细记录。

客户机的ip地址:192.168.2.11

远端主机的IP地址:192.168.2.12

在客户机以root用户执行下述命令:

  1. [root@localhost .ssh]# /usr/bin/ssh-keygen -t rsa  
  2. Generating public/private rsa key pair.  
  3. Enter file in which to save the key (/root/.ssh/id_rsa):   
  4. Enter passphrase (empty for no passphrase):   
  5. Enter same passphrase again:   
  6. Your identification has been saved in /root/.ssh/id_rsa.  
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.  
  8. The key fingerprint is:  
  9. 30:f6:d7:2a:ac:56:eb:3f:fa:40:25:8d:90:96:68:cb root@localhost.localdomain  

说明:

该命令将在用户的主目录/.ssh目录下面产生一对密钥

一般采用的ssh的rsa密钥:

id_rsa 私钥

id_rsa.pub 公钥

下述命令产生不同类型的密钥

  1. ssh-keygen -t dsa   
  2. ssh-keygen -t rsa   
  3. ssh-keygen -t rsa1   
  4. -------------------------------------------------------  
  5. [root@localhost .ssh]# scp /root/.ssh/id_rsa.pubroot@192.168.2.12:/root/.ssh/authorized_keys  
  6. The authenticity of host '192.168.2.12 (192.168.2.12)' can't be established.  
  7. RSA key fingerprint is 4b:a5:74:fb:2e:08:60:af:fa:76:d4:b0:26:4c:13:75.  
  8. Are you sure you want to continue connecting (yes/no)? yes  
  9. Warning: Permanently added '192.168.2.12' (RSA) to the list of known hosts.  
  10. root@192.168.2.12's password:  
  11. id_rsa.pub    

100% 236 0.2KB/s 00:00

说明:

将公钥拷贝到远端主机,并写入授权列表文件

你也可以把公钥文件拷贝过去后,在远端主机下直接执行

  1. touch /root/.ssh/authorized_keys  
  2. cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys  

操作完毕,登陆检查。

  1. [root@localhost .ssh]# ssh 192.168.2.12  
  2.   
  3. root@192.1682.12's password:  

请注意此时如果仍提示输入密码,请检查如下文件夹和文件的操作权限,这是非常重要的, 否则ssh公钥认证体制不能正常工作:

  1. 192.168.2.11(客户端)  
  2. /home/root文件夹的权限是600  
  3. /home/root/.ssh文件夹的权限是600 (好像这个权限关系不是很大)  
  4. /home/root/.ssh/id_dsa私钥的权限600  
  5. 192.168.2.12(远端主机)  
  6. /home/root文件夹的权限是644  
  7. /home/root/.ssh文件夹的权限是644 (好像这个权限关系不是很大)  
  8. /root/.ssh/authorized_keys公钥的权限644  
  9. --------------------------------------------------------------   
  10. [root@localhost ~]# ssh 192.168.2.12  
  11. Last login: Sat Dec 15 21:10:17 2007 from 192.168.2.11  
  12. [root@localhost ~]#   

无密码SSH登陆成功!

标签:

给我留言