Linux CentOS7 两台机器之间免输入密码相互登陆(密钥对认证)

Linux CentOS7 两台机器之间免输入密码相互登陆(密钥对认证)

两台机器为:html

主机名:fxq-1,IP:192.168.42.181linux

主机名:fxq-2, IP:192.168.42.182api

w命令能够查看当前登陆用户的信息bash

[root@fxq-1 ~]# w
 23:59:42 up 12 min,  1 user,  load average: 0.00, 0.07, 0.11USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.42.2     23:48    6.00s  0.13s  0.07s w
[root@fxq-1 ~]# w
 00:00:18 up 12 min,  2 users,  load average: 0.00, 0.06, 0.11USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.42.2     23:48    2.00s  0.08s  0.02s w
root     pts/1    192.168.42.181   00:00    5.00s  0.07s  0.07s -bash

whoami ##查看当前登陆用户 ssh -p 22 root@192.168.42.182 ##指定端口和用户进行登陆markdown

1.关闭SELINUX(每台机器都要关闭)

  • 临时关闭SELINUXapp

setenforce 0 ##临时关闭SELINUXdom

getenforce 0 ##查看SELINUX设置ssh

  • 永久关闭SELINUXide

vi /etc/selinux/conf测试

改其中第六行为: SELINUX=disabled 保存退出

在fxq-1机器上:

ssh-keygen生成密钥对

[root@fxq-1 ~]# 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:
68:6a:c4:24:60:3d:dc:2a:cc:4b:04:58:0a:a8:3f:a9 root@fxq-1The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|Bo+ .            |
|=+ + .           |
|* . +            |
|.= =   .         |
|..o.o o S        |
| .+. o           |
| . .o            |
|E  .             |
|                 |
+-----------------+
[root@fxq-1 ~]#
[root@fxq-1 ~]# ls /root/.ssh/id_rsa  id_rsa.pub  known_hosts
[root@fxq-1 ~]#

在fxq-1机器上输入,下面命令,按提示输入fxq-2的root用户的密码:

scp /root/.ssh/id_rsa.pub root@192.168.42.182:/root/.ssh/authorized_keys

此步骤是把fxq-1机器的公钥写入到fxq-2的authorized_keys文件中,若是fxq-2以前已有authorized_keys文件,那么须要手工把公钥内容粘贴到fxq-2的authorized_keys文件中。不然会把原有的其余机器的公钥覆盖。

在fxq-2机器上操做以下:

[root@fxq-2 ~]# 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:
68:6a:c4:24:60:3d:dc:2a:cc:4b:04:58:0a:a8:3f:a9 root@fxq-1The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|Bo+ .            |
|=+ + .           |
|* . +            |
|.= =   .         |
|..o.o o S        |
| .+. o           |
| . .o            |
|E  .             |
|                 |
+-----------------+
[root@fxq-2 ~]# [root@fxq-2 ~]#
[root@fxq-1 ~]# ls /root/.ssh/id_rsa  id_rsa.pub  known_hosts
[root@fxq-1 ~]#

 在fxq-2机器上输入,下面命令,按提示输入fxq-1的root用户的密码:

scp /root/.ssh/id_rsa.pub root@192.168.42.181:/root/.ssh/authorized_keys

此步骤是把fxq-2机器的公钥写入到fxq-1的authorized_keys文件中,若是fxq-1以前已有authorized_keys文件,那么须要手工把公钥内容粘贴到fxq-1的authorized_keys文件中。不然会把原有的其余机器的公钥覆盖。

完成后能够在fxq-1上测试,不用输入密码就能登陆fxq-2成功:

[root@fxq-1 ~]# ssh 192.168.42.182
Last login: Thu Aug  3 17:28:57 2017
[root@fxq-2 ~]#
相关文章
相关标签/搜索