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 [email protected]
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.
[email protected]'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.
[email protected]'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 ~]# 

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/106584910