SCP免密登录
ip |
---|
170.18.9.140 |
170.18.9.141 |
介绍
SSH 为 Secure Shell 的缩写,SSH 为建立在应用层基础上的安全协议。
SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。
利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。
从客户端来看,SSH提供两种级别的安全验证:
第一种级别(基于口令的安全验证):只要你知道自己帐号和口令,就可以登录到远程主机。
所有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。
可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的攻击。
第二种级别(基于密匙的安全验证)ssh-keygen:需要依靠密匙,你必须为自己创建一对密匙,并把公用密匙放在需要访问的服务器上。
如果你要连接到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。
服务器收到请求之后,先在该服务器上你的主目录下寻找你的公用密匙,然后把它和你发送过来的公用密匙进行比较。
如果两个密匙一致,服务器就用公用密匙加密“质询”(challenge)并把它发送给客户端软件。
客户端软件收到“质询”之后就可以用你的私人密匙解密再把它发送给服务器。用这种方式,你必须知道自己密匙的口令。
但是,与第一种级别相比,第二种级别不需要在网络上传送口令。
第二种级别不仅加密所有传送的数据,而且“中间人”这种攻击方式也是不可能的(因为他没有你的私人密匙)。但是整个登录的过程可能需要10秒 。
ssh-keygen有很多的参数,比如这里的-t -b -C是常见参数
- -b:指定密钥长度,对于RSA密钥,最小要求768位,不加默认是2048位,命令中为4096
- -t:指定要创建的密钥类型,密钥的类型有两种,一种是RSA,一种是DSA.默认RSA
- -C:添加注释;-e:读取openssh的私钥或者公钥文件;
- -f:指定用来保存密钥的文件名,默认保存在/家目录/.ssh/id_rsa;
- -i:读取未加密的ssh-v2兼容的私钥/公钥文件,然后在标准输出设备上显示openssh兼容的私钥/公钥;
- -l:显示公钥文件的指纹数据;
- -N:提供一个新密语;
- -P:提供(旧)密语;
- -q:静默模式;
生成公钥与私钥
在170.18.9.140服务器上执行
$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
#key文件保存路径,默认/root/.ssh/id_rsa
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:
SHA256:Zqhks5Ay6chFSaiakdmwOnRpkmwaML+7Pxr4slZ3Ddc root@localhost.localdomain
The key's randomart image is:
+---[RSA 4096]----+
| .. |
|+.. . |
|+O.o. . |
|BB+= o . E |
|X+*o+ . S |
|X+o= = + . |
|o+o.+ . |
|.o... |
|oo++.. |
+----[SHA256]-----+
$ ll /root/.ssh/ #/root/.ssh/目录下会有id_rsa、id_rsa.pub两个文件,id_rsa为私钥,id_rsa.pub为公钥
total 12
-rw-------. 1 root root 3243 Jul 8 13:54 id_rsa
-rw-r--r--. 1 root root 752 Jul 8 13:54 id_rsa.pub
-rw-r--r--. 1 root root 522 Jul 8 13:36 known_hosts
将公钥传到另外的服务器
#将公钥传到170.18.9.141,有两种方法
一:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@'170.18.9.141'
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@170.18.9.141's password: #输入170.18.9.141登录密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@170.18.9.141'"
and check to make sure that only the key(s) you wanted were added.
这时在170.18.9.141的/root/.ssh会生成authorized_keys文件,文件权限为600
二:
在170.18.9.141服务器上的/家目录/.ssh文件下创建authorized_keys文件,
将id_rsa.pub文件中的内容复制到authorized_keys中
并对authorized_keys授权600.
测试SCP
#不需要输入密码即可传输文件
$ scp 1.tar root@'170.18.9.141':/opt/
1.tar 100% 799MB 74.9MB/s 00:10
免密码失败原因定位分析
1、服务器B上.ssh目录的权限必须是700
2、服务器B上.authorized_keys文件权限必须是600
3、服务器B上SELinux为enforcing,需要设置为disabled
4、可能是StrictModes
编辑 vi /etc/ssh/sshd_config
找到#StrictModes yes改成StrictModes no
5、可能是PubkeyAuthentication问题
编辑 vi /etc/ssh/sshd_config
找到PubkeyAuthentication改成yes