最小化安装的CentOS6配置SSH免密码登录

前端之家收集整理的这篇文章主要介绍了最小化安装的CentOS6配置SSH免密码登录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最小化安装的CentOS6的root目录下是没有.ssh目录的,如下所示。

  1. [root@zookeeper1 ~]# ls -la
  2. 总用量 56
  3. dr-xr-x---. 3 root root 4096 4 24 02:27 .
  4. dr-xr-xr-x. 22 root root 4096 4 24 02:28 ..
  5. -rw-------. 1 root root 1111 4 24 00:47 anaconda-ks.cfg
  6. -rw-------. 1 root root 134 4 24 02:27 .bash_history
  7. -rw-r--r--. 1 root root 18 5 20 2009 .bash_logout
  8. -rw-r--r--. 1 root root 176 5 20 2009 .bash_profile
  9. -rw-r--r--. 1 root root 176 9 23 2004 .bashrc
  10. -rw-r--r--. 1 root root 100 9 23 2004 .cshrc
  11. -rw-r--r--. 1 root root 8017 4 24 00:47 install.log
  12. -rw-r--r--. 1 root root 3384 4 24 00:46 install.log.syslog
  13. drwxr-xr-x. 2 root root 4096 4 24 01:09 .oracle_jre_usage
  14. -rw-r--r--. 1 root root 129 12 4 2004 .tcshrc
  15. -rw-------. 1 root root 1909 4 24 02:27 .viminfo
  16. [root@zookeeper1 ~]#

我们直接在root目录下生成秘钥,如下所示,输入ssh-keygen -t rsa命令后,每次暂停就敲一次回车,敲三次回车即可生成秘钥,如下所示。
  1. [root@zookeeper1 ~]# 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. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 09:4e:f5:dc:77:44:0f:18:40:42:f1:60:03:7a:2b:80 root@zookeeper1
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | .oOoo..o..o|
  14. | . . o B .. o.|
  15. |E . . + + . . o|
  16. | . + o . . . |
  17. | . o S |
  18. | . |
  19. | |
  20. | |
  21. | |
  22. +-----------------+
  23. [root@zookeeper1 ~]#

我们再看root目录下,发现这时多了一个.ssh文件夹,如下所示。
  1. [root@zookeeper1 ~]# ll -la
  2. 总用量 60
  3. dr-xr-x---. 4 root root 4096 4 24 02:41 .
  4. dr-xr-xr-x. 22 root root 4096 4 24 02:28 ..
  5. -rw-------. 1 root root 1111 4 24 00:47 anaconda-ks.cfg
  6. -rw-------. 1 root root 134 4 24 02:27 .bash_history
  7. -rw-r--r--. 1 root root 18 5 20 2009 .bash_logout
  8. -rw-r--r--. 1 root root 176 5 20 2009 .bash_profile
  9. -rw-r--r--. 1 root root 176 9 23 2004 .bashrc
  10. -rw-r--r--. 1 root root 100 9 23 2004 .cshrc
  11. -rw-r--r--. 1 root root 8017 4 24 00:47 install.log
  12. -rw-r--r--. 1 root root 3384 4 24 00:46 install.log.syslog
  13. drwxr-xr-x. 2 root root 4096 4 24 01:09 .oracle_jre_usage
  14. drwx------. 2 root root 4096 4 24 02:41 .ssh
  15. -rw-r--r--. 1 root root 129 12 4 2004 .tcshrc
  16. -rw-------. 1 root root 1909 4 24 02:27 .viminfo
  17. [root@zookeeper1 ~]#

我们到这个.ssh目录下看看都生成了什么文件,如下所示。可以看到有id_rsa和id_rsa.pub两个文件,分别是秘钥和公钥。
  1. [root@zookeeper1 ~]# cd .ssh/
  2. [root@zookeeper1 .ssh]# ll
  3. 总用量 8
  4. -rw-------. 1 root root 1675 4 24 02:41 id_rsa
  5. -rw-r--r--. 1 root root 397 4 24 02:41 id_rsa.pub
  6. [root@zookeeper1 .ssh]#

在.ssh目录下新建一个authorized_keys文件,如下所示
  1. [root@zookeeper1 .ssh]# touch authorized_keys
  2. [root@zookeeper1 .ssh]# ll
  3. 总用量 8
  4. -rw-r--r--. 1 root root 0 4 24 02:50 authorized_keys
  5. -rw-------. 1 root root 1675 4 24 02:41 id_rsa
  6. -rw-r--r--. 1 root root 397 4 24 02:41 id_rsa.pub
  7. [root@zookeeper1 .ssh]#

由于设备自己ssh登录自己也需要输入密码,为了让自己ssh登录自己不用输密码,我们把公钥拷给自己一份,拷贝完之后,我们查看一下authorized_keys文件中的内容,所看到的便是zookeeper1这台设备的公钥,如下所示。
  1. [root@zookeeper1 .ssh]# cp id_rsa.pub authorized_keys
  2. cp:是否覆盖"authorized_keys" y
  3. [root@zookeeper1 .ssh]# more authorized_keys
  4. ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvnYJjHUMsOebTzJ8p5M/S7gwFWwRk7iDYL92CxSS7SGO/PwB9jlPvNXqXN8cRwyyTA0slpPhJqful0ng2fSDeltkgWe9pWgQUUNt/2VT20CNHtQq1
  5. wtm4vvkHLJn3pcUdhwVKsa3ITV7pAIx6LKyxhxkOtXGGNVFZYF0CXAaC+9B5Sz2YbKyJQycaa4JekvFRw6qvvrAxIhBxvtaaNZRpv3iz+0VU3w82ADbaJ8xbI8F3Ooad1LR7c0od68D4RnvGTDv9O
  6. 4rZ1vwltlP0PDvYCB5UACt0DZbAsnCAykC3cgcc9RBzz9VsRFk3JBKkUf2EvKvaT8OYL6NnEyTw6ibyQ== root@zookeeper1
  7. [root@zookeeper1 .ssh]#

接下来我们尝试以ssh的方式登录下自己,发现确实不用输入密码即可登录了。如下所示。
  1. [root@zookeeper1 .ssh]# ssh zookeeper1
  2. The authenticity of host 'zookeeper1 (192.168.156.23)' can't be established.
  3. RSA key fingerprint is d2:f2:34:34:31:07:df:18:d5:3e:e7:ea:93:08:bb:38.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added 'zookeeper1,192.168.156.23' (RSA) to the list of known hosts.
  6. Last login: Mon Apr 24 02:32:09 2017 from 192.168.156.100
  7. [root@zookeeper1 ~]#

我们以同样的方式再配置一台设备192.168.156.24,然后我们想从当前设备以ssh免密码登录的方式登录192.168.156.24,那么我们便把192.168.156.23上的公钥拷给192.168.156.24一份,如下所示。
  1. [root@zookeeper1 ~]# ssh-copy-id 192.168.156.24
  2. The authenticity of host '192.168.156.24 (192.168.156.24)' can't be established.
  3. RSA key fingerprint is d2:f2:34:34:31:07:df:18:d5:3e:e7:ea:93:08:bb:38.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added '192.168.156.24' (RSA) to the list of known hosts.
  6. root@192.168.156.24's password:
  7. Now try logging into the machine,with "ssh '192.168.156.24'",and check in:
  8.  
  9. .ssh/authorized_keys
  10.  
  11. to make sure we haven't added extra keys that you weren't expecting.
  12.  
  13. [root@zookeeper1 ~]#

下面我们便尝试以ssh登录的方式从192.168.156.23登录到192.168.156.24上,如下所示,发现确实不用输入密码即可登录。这就实现了SSH免密码登录功能了。
  1. [root@zookeeper1 ~]# ssh zookeeper2
  2. The authenticity of host 'zookeeper2 (192.168.156.24)' can't be established.
  3. RSA key fingerprint is d2:f2:34:34:31:07:df:18:d5:3e:e7:ea:93:08:bb:38.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added 'zookeeper2' (RSA) to the list of known hosts.
  6. Last login: Mon Apr 24 02:33:13 2017 from 192.168.156.100
  7. [root@zookeeper2 ~]#

注:之所以可以直接ssh zookeeper2是因为在两台设备的/etc/hosts文件当中配置IP与主机名的映射关系,如下所示。

  1. [root@zookeeper1 ~]# more /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.156.23 zookeeper1
  5. 192.168.156.24 zookeeper2
  6. [root@zookeeper1 ~]#

猜你在找的CentOS相关文章