当前位置:   article > 正文

Centos 7 做ssh免密登录_/usr/bin/ssh-copy-id: info: 1 key(s) remain to be

/usr/bin/ssh-copy-id: info: 1 key(s) remain to be installed -- if you are pr

一、准备阶段

 1、两台Centos系统,并已经安装了ssh

   jerry7-11   10.4.7.11
   jerry7-12   10.4.7.12

  2、关闭防火墙,如果防火墙开启,将端口加入到防火墙规则中。

 

二、操作步骤

  1、 22端口修改

    可以修改ssh的22端口,或者不改 vim /etc/ssh/sshd_config,找到如下部分

    #Port 22

    将#去除,22改为想要的端口号。然后重启sshd服务 
    systemctl restart sshd
    使用netstat -tlunp | grep sshd查看端口号

   2、单向免密登录 

    jerry7-11使用ssh远程jerry7-12不需要密码,反之需要密码
    在jerry7-11上使用ssh-keygen生成公钥和私钥(这里使用默认的rsa),一路默认即可

  1. [root@ jerry7-11 ~]# ssh-keygen -t rsa //默认指定算法的是rsa,可以没有-t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. //保存私钥的路径默认/root/.ssh/id_rsa,可以指定生成地址
  5. Created directory '/root/.ssh'.
  6. Enter passphrase (empty for no passphrase): //密码
  7. Enter same passphrase again:
  8. Your identification has been saved in /root/.ssh/id_rsa. 生成私钥
  9. Your public key has been saved in /root/.ssh/id_rsa.pub. 生成公钥
  10. The key fingerprint is:
  11. SHA256:PXTg9KW4P582PWJMEwUstPfyXk6NWdrB518yCORbisc root@hdss7-11
  12. The key's randomart image is:
  13. +---[RSA 2048]----+
  14. | +...o |
  15. | o =.+ . |
  16. | =.=.. |
  17. | = o..o |
  18. | S * ...+o|
  19. | o B +oB+|
  20. | . E * BoB|
  21. | . *+B=|
  22. | ..+o+|
  23. +----[SHA256]-----+
  24. [root@ jerry7-11 ~]#

在没有指定生成地址时,会默认生成到家目录下的.ssh/目录下。使用rsa就会生成id_rsa和id_rsa.pub两个文件,如果使用的是dsa则生成的是id_dsa和id_dsa.pub两个文件。
[root@ jerry7-11 ~]# ll /root/.ssh/
-rw-------. 1 root root 1679 9月  16 14:53 id_rsa
-rw-r--r--. 1 root root  395 9月  16 14:53 id_rsa.pub

接着使用命令ssh-copy-id命令将本机生成的公钥发给jerry7-12被远程的服务器

  1. [root@ jerry7-11 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@10.4.7.12
  2. //-p是指你要发送对方的端口号
  3. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  4. The authenticity of host '10.4.7.12 (10.4.7.12)' can't be established.
  5. ECDSA key fingerprint is SHA256:iGP1Ez7V8/O5JAgo9FJ5GZ20TMU9l/dEVQkpRQrPO58.
  6. ECDSA key fingerprint is MD5:d9:7f:07:c6:a7:77:8f:56:32:bc:69:83:cb:17:a6:c2.
  7. Are you sure you want to continue connecting (yes/no)? yes //yes继续
  8. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  9. /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
  10. root@10.4.7.12's password: //输入jerry7-12服务器上的root用户的密码
  11. Number of key(s) added: 1
  12. Now try logging into the machine, with: "ssh -p '22' 'root@10.4.7.12'"
  13. and check to make sure that only the key(s) you wanted were added.
  14. [root@ jerry7-11 ~]#

公钥传完后在本地.ssh/ 下生成known_hosts文件
[root@jerry7-11 ~]# ll /root/.ssh/
-rw-------. 1 root root 1679 9月  16 14:53 id_rsa
-rw-r--r--. 1 root root  395 9月  16 14:53 id_rsa.pub
-rw-r--r--. 1 root root  171 9月  16 15:02 known_hosts

而在jerry7-12 服务器的root用户的家目录下生成.ssh目录,并含有authorized_keys文件。
[root@jerry7-12 .ssh]# ll /root/.ssh/
-rw------- 1 root root 395 9月  16 15:02 authorized_keys

测试:在hdss7-11测试ssh jerry7-12 
[root@jerry7-11~]# ssh root@10.4.7.12 -p 22
Last login: Wed Sep 16 12:59:08 2020 from 10.4.7.1
[root@jerry7-12 ~]#


3、双向免密登录

双向免密就是互换公钥即可,这里接着上面把jerry7-12 的公钥发送到jerry7-11 上,并进行测试。

jerry7-12 :
[root@jerry7-12 .ssh]# ssh-keygen
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:
SHA256:0N5SHnrZtgCwqcXd+W1waUrPpKaZN864xfKVp8LWiqU root@hdss7-12
The key's randomart image is:
+---[RSA 2048]----+
|      .          |
|     . * . .   . |
|      * + = o =  |
|     o o * * @   |
|    .   S * B =  |
|         o O o . |
|          =.B.o .|
|           %+o.o |
|          Eo=o.  |
+----[SHA256]-----+

[root@jerry7-12 .ssh]# cd /root/.ssh/
[root@jerry7-12 .ssh]# ll
-rw------- 1 root root  395 9月  16 15:02 authorized_keys
-rw------- 1 root root 1679 9月  16 15:21 id_rsa
-rw-r--r-- 1 root root  395 9月  16 15:21 id_rsa.pub

[root@jerry7-12 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@10.4.7.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.4.7.11 (10.4.7.11)' can't be established.
ECDSA key fingerprint is SHA256:iGP1Ez7V8/O5JAgo9FJ5GZ20TMU9l/dEVQkpRQrPO58.
ECDSA key fingerprint is MD5:d9:7f:07:c6:a7:77:8f:56:32:bc:69:83:cb:17:a6:c2.
Are you sure you want to continue connecting (yes/no)? yes
/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@10.4.7.11's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -p '22' 'root@10.4.7.11'"
and check to make sure that only the key(s) you wanted were added.

jerry7-11 :
[root@jerry7-12 ~]# ll /root/.ssh/
-rw------- 1 root root  395 9月  16 15:02 authorized_keys
-rw------- 1 root root 1679 9月  16 15:21 id_rsa
-rw-r--r-- 1 root root  395 9月  16 15:21 id_rsa.pub
-rw-r--r-- 1 root root  171 9月  16 15:22 known_hosts

jerry7-12 :
[root@jerry7-12 .ssh]# ssh 10.4.7.11 -p 22
Last login: Wed Sep 16 12:59:07 2020 from 10.4.7.1
[root@jerry7-11 ~]# 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号