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

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

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

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

我们直接在root目录下生成秘钥,如下所示,输入ssh-keygen -t rsa命令后,每次暂停就敲一次回车,敲三次回车即可生成秘钥,如下所示。
[root@zookeeper1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:
09:4e:f5:dc:77:44:0f:18:40:42:f1:60:03:7a:2b:80 root@zookeeper1
The key's randomart image is:
+--[ RSA 2048]----+
|      .oOoo..o..o|
| .   . o B ..  o.|
|E . . +   + . . o|
|   . + o .   . . |
|    . o S        |
|     .           |
|                 |
|                 |
|                 |
+-----------------+
[root@zookeeper1 ~]#

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

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

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

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

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

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

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@zookeeper1 ~]#

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

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

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

猜你在找的CentOS相关文章