Linux笔记-设置SSH公钥免密码登录

三台内网机对应IP和名称如下:

名称 ip
Centos 7 MySQL Master 192.168.79.134
Centos 7 MySQL Slave 192.168.79.136
Centos 7 MySQLManager 192.168.79.137

 

在上面三台机器上个添加/etc/hosts

192.168.79.134 mydb1
192.168.79.136 mydb2
192.168.79.137 mydb3

任意找一台机器,生成ssh的key,然后复制到其他两台机器上:

[root@localhost ~]# pwd
/root
[root@localhost ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
[root@localhost .ssh]# ssh-keygen -t rsa
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:7xiZZui/S6hW6yaYj+vo8bOYobUrg2Ie65n0Rh5EjuM root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|   .             |
|  +              |
| o o             |
|. o     S        |
| E o  .o +       |
|.=+o..o.B .      |
|B+#*ooo+ +       |
|O&BO=+o.=o.      |
+----[SHA256]-----+
[root@localhost .ssh]# 
[root@localhost .ssh]# 
[root@localhost .ssh]# 
[root@localhost .ssh]# 
[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys

随后赋予权限:

[root@localhost .ssh]# chmod 700 ~/.ssh
[root@localhost .ssh]# chmod 600 ~/.ssh/authorized_keys 
[root@localhost .ssh]# cd ..

将.ssh文件复制到其他2台主机上个:

[root@localhost ~]# scp -r .ssh 192.168.79.136:/root/
The authenticity of host '192.168.79.136 (192.168.79.136)' can't be established.
ECDSA key fingerprint is SHA256:ctYIvL3p2kOgf/2h8VbDCrHTJUqKcDNvNsLV/s+bJYQ.
ECDSA key fingerprint is MD5:76:cf:8a:bb:be:4c:b8:37:73:7c:05:48:11:de:33:2b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.79.136' (ECDSA) to the list of known hosts.
root@192.168.79.136's password: 
id_rsa                                        100% 1675     1.7MB/s   00:00    
id_rsa.pub                                    100%  408   500.0KB/s   00:00    
authorized_keys                               100%  408   493.7KB/s   00:00    
known_hosts                                   100%  176   181.8KB/s   00:00    
[root@localhost ~]# scp -r .ssh 192.168.79.137:/root/
The authenticity of host '192.168.79.137 (192.168.79.137)' can't be established.
ECDSA key fingerprint is SHA256:ctYIvL3p2kOgf/2h8VbDCrHTJUqKcDNvNsLV/s+bJYQ.
ECDSA key fingerprint is MD5:76:cf:8a:bb:be:4c:b8:37:73:7c:05:48:11:de:33:2b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.79.137' (ECDSA) to the list of known hosts.
root@192.168.79.137's password: 
id_rsa                                        100% 1675     1.5MB/s   00:00    
id_rsa.pub                                    100%  408   499.5KB/s   00:00    
authorized_keys                               100%  408   412.5KB/s   00:00    
known_hosts                                   100%  352   399.2KB/s   00:00    
[root@localhost ~]#

测试通过ssh mydb1等测试下要不要密码:

[root@localhost ~]# ssh mydb1
The authenticity of host 'mydb1 (192.168.79.134)' can't be established.
ECDSA key fingerprint is SHA256:ctYIvL3p2kOgf/2h8VbDCrHTJUqKcDNvNsLV/s+bJYQ.
ECDSA key fingerprint is MD5:76:cf:8a:bb:be:4c:b8:37:73:7c:05:48:11:de:33:2b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mydb1,192.168.79.134' (ECDSA) to the list of known hosts.
Last login: Sat Jun  6 10:37:55 2020
[root@localhost ~]# ssh mydb2
Last login: Sat Jun  6 10:50:04 2020 from mydb1
[root@localhost ~]# ssh mydb3
Last login: Sat Jun  6 10:50:51 2020 from mydb2
[root@localhost ~]# 

 

展开阅读全文
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值