Linux-免密ssh登陆

IP要能ping通

关闭防火墙

[root@localhost tdd]# systemctl stop firewalld
[root@localhost tdd]# systemctl disable firewalld
[root@localhost tdd]# vim /etc/selinux/config

修改SELINUX=disabled

[root@localhost tdd]# reboot

修改hostname

[root@localhost tdd]# uname -n
localhost.localdomain

修改配置文件 /etc/hostname 保存退出

[root@localhost tdd]# vim /etc/hostname
master

在每台机器的/etc/hosts文件中添加如下内容:

[root@slave2 tdd]# vim /etc/hosts
192.168.44.10 master
192.168.44.11 slave1
192.168.44.12 slave2

同步系统时间

[root@master tdd]# date
Fri Mar 29 04:50:21 CST 2019
[root@master tdd]# hwclock
Fri 29 Mar 2019 04:50:27 AM CST -0.666422 seconds
[root@master tdd]# hwclock -w
[root@master tdd]# ntpdate time.nist.gov
28 Mar 20:50:47 ntpdate[2847]: step time server 132.163.96.2 offset -28799.219966 sec
[root@master tdd]# hwclock -w
[root@master tdd]# date
Thu Mar 28 20:51:23 CST 2019

如果上面time.nist.gov服务器同步不了,可以换下面几个时间服务器试试:

time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org

ssh互信

在master上操作:

首先生成密钥对

[root@master ~]# ssh-keygen -t rsa (提示时,直接回车即可)

导入本机

[root@master .ssh]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

再将master自己的公钥拷贝并追加到slave1、slave2的授权列表文件authorized_keys中

[root@master ~]# ssh-copy-id -i id_rsa root@slave1
[root@master ~]# ssh-copy-id -i id_rsa root@slave2

在slave*上操作:

同master只是@后是其他的机器的hostname

  • 这里用hostname是因为在hosts文件中配置了映射

测试

master:

[root@master .ssh]# ssh slave2
Last failed login: Fri Mar 29 05:08:05 CST 2019 on pts/0
There were 2 failed login attempts since the last successful login.
Last login: Thu Mar 28 21:18:56 2019
[root@slave2 ~]# ssh slave1
Last failed login: Thu Mar 28 21:24:44 CST 2019 from slave2 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Mar 28 21:21:04 2019 from master
[root@slave1 ~]# ssh master
Last login: Thu Mar 28 21:12:29 2019
[root@master ~]#

slave1:

[root@slave1 .ssh]# ssh slave2
Last failed login: Fri Mar 29 05:08:05 CST 2019 on pts/0
There were 2 failed login attempts since the last successful login.
Last login: Thu Mar 28 21:30:35 2019 from master
[root@slave2 ~]# ssh master
Last login: Thu Mar 28 21:27:28 2019 from slave1
[root@master ~]# ssh slave1
Last login: Thu Mar 28 21:27:23 2019 from slave2
[root@slave1 ~]#

slave2:

[root@slave2 .ssh]# ssh slave1
Last login: Thu Mar 28 21:28:15 2019 from master
[root@slave1 ~]# ssh master
Last login: Thu Mar 28 21:28:08 2019 from slave2
[root@master ~]# ssh slave2
Last failed login: Fri Mar 29 05:08:05 CST 2019 on pts/0
There were 2 failed login attempts since the last successful login.
Last login: Thu Mar 28 21:31:21 2019 from slave1
[root@slave2 ~]#
[root@slave2 ~]# exit
logout
Connection to slave2 closed.
[root@master ~]# exit
logout
Connection to master closed.
[root@slave1 ~]# exit
logout
Connection to slave1 closed.
[root@slave2 .ssh]#

上一篇:第一天敲程序的随笔


下一篇:Spring之SpringMVC的Controller(源码)分析