概述

Public Key认证的主要魅力在于认证时承诺不必提供密码就能够同远程系统建立连接。

Public Key认证的基础在于一对密钥,public key和private key,public key对数据进行加密而且只能用于加密,private key 只能对所匹配的public key加密过的数据进行解密。

我们把public key放在远程系统合适的位置,然后从本地开始进行ssh连接。

此时,远程的sshd会产生一个随机数并用我们产生的public key进行加密后发给本地,本地会用private key进行解密并把这个随机数发回给远程系统。

最后,远程系统的sshd会得出结论我们拥有匹配的private key允许我们登录。

SFTP免密码登录操作方法

1) 登陆服务端主机,在当前用户根目录执行(以RSA算法为例)

如下命令:

ssh-keygen –t rsa
屏幕显示内容均可直接以回车使用默认项,此时在$home/.ssh目录下生成I d_rsa私钥(private key)及id_rsa.pub公钥(public key)两个文件。

2) 将服务端的 $home/.ssh/id_rsa.pub 文件上传到客户端机的 $home/.ssh 目录下

通过scp命令传到 客户端

scp  $home/.ssh/id_rsa.pub username@client_ip:$home/.ssh
在客户端机 $home/.ssh 目录下执行命令:

cat id_rsa.pub >> authorized_keys
3)将客户端各主机信息添加到$home/.ssh/known_hosts文件中

例如:

服务端IP是10.45.7.154

客户端为:10.45.7.61

在服务端执行 sftp usename@10.45.7.61命令提示如下:

qdgcp@mdw:~/.ssh> sftp qmdb@10.45.7.61
Connecting to 10.45.7.61...
The authenticity of host '10.45.7.61 (10.45.7.61)' can't be established.
RSA key fingerprint is ea:53:c8:cf:24:31:18:d8:47:68:8d:18:65:a4:e8:20.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.45.7.61' (RSA) to the list of known hosts.
这样,在服务端的$home/.ssh/目录会出现一个known_hosts文件,客户端主机信息将添加到这个文件中。

按上述方法实现服务端主机和客户端主机间的公私钥配对后,从服务端主机向客户端主机就可以实 现同一用户名 的sftp自动安全登陆操作了。

说明:

在配置ssh自动传输的时候注意,

.ssh目录的属主、属组使用当前用户与用户组,
.ssh目录的权限请保持700,
authorized_keys的权限为644,
id_rsa的权限为600,
id_rsa.pub的权限为644,
同时检查用户$HOME目录权限必须为755。

确认权限 。

r 4 w 2 x 1

chmod 644 dir 通过chmod 赋权限。

实操

以下操作的是同一台主机, 一般都用于两台主机之间。

[root@entel2 ~]# cd $HOME
[root@entel2 ~]# 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:
79:e6:c1:13:7d:05:39:8a:c8:e8:84:9f:8e:c6:0e:0f root@entel2
The key's randomart image is:
+--[ RSA 2048]----+
|              .o.|
|           .  o. |
|     . o ....... |
|    . o oo....   |
|     + .S *      |
|      +  + o     |
|  E. o    .      |
|   ++ .          |
|   oo            |
+-----------------+
[root@entel2 ~]# cd .ssh/
[root@entel2 .ssh]# ll
total 24
-rw------- 1 root root  1671 Nov  3 14:05 id_rsa
-rw-r--r-- 1 root root   393 Nov  3 14:05 id_rsa.pub


[root@entel2 .ssh]# cat id_rsa.pub >>authorized_keys

[root@entel2 .ssh]# ll
total 28
-rw-r--r-- 1 root root   393 Nov  3 14:08 authorized_keys
-rw------- 1 root root  1671 Nov  3 14:05 id_rsa
-rw-r--r-- 1 root root   393 Nov  3 14:05 id_rsa.pub


[root@entel2 .ssh]# sftp root@10.45.7.198
Connecting to 10.45.7.198...
The authenticity of host '10.45.7.198 (10.45.7.198)' can't be established.
RSA key fingerprint is 5f:b5:f8:02:5a:b0:ad:6b:10:ce:6a:cc:53:55:68:c7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.45.7.198' (RSA) to the list of known hosts.
sftp> exit


查看目录 出现了 known_hosts
[root@entel2 .ssh]# ll
total 32
-rw-r--r-- 1 root root   393 Nov  3 14:08 authorized_keys
-rw------- 1 root root  1671 Nov  3 14:05 id_rsa
-rw-r--r-- 1 root root   393 Nov  3 14:05 id_rsa.pub
-rw-r--r-- 1 root root   393 Nov  3 14:09 known_hosts


[root@entel2 .ssh]# cat known_hosts
10.45.7.198 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1WCBN8XDrGX2Ersub6U8CDtY2a0Ug5YT9fvqTcJm+DCQVj/DZVyvc5ODBC03iDp54ugSOToFluo1RabJjrTEZGMWjYKCZzEXRYO3sq4waGIP1D27bYLs9GW23VZUP0qwpmPKuJs8Asx7S1m9knoQO+wIeyQQt0gkw3TKM4QJSPAsfqSl0XuSTOczS9kbNTGOCpYoB7kThsE8Q87OqRLfjR2ePN3bDTL6RKX8iDm2PelQ2g+oAQCCmWlUF11TYwBahN0bKY7rNPsEQWlYpk0QGdNVTwMGy1j2DrlzoLSyuLfMu5+NgPVgx9MKmLfQlPVdFQyb/n5NShGxG+VAPA+6CQ==



[root@entel2 .ssh]# ssh root@10.45.7.198
Last login: Thu Nov  3 14:06:17 2016 from 10.45.22.168



[root@entel2 ~]# sftp root@10.45.7.198
Connecting to 10.45.7.198...
sftp>