ssh免密登录(使用公钥私钥对登录)

=========两台(多台)机器之间实现ssh免密登录(实质上是使用公钥 私钥对)================
关于公钥私钥对
一般ssh免密登录 采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1
 
首先两台机器分别为:
master 
MHA-Manager
 
 ############## master ########################
[root@master ~]# ssh-keygen -t rsa -P ''
#####################################
#-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
#该命令将在/root/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。
######################################
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
7f:aa:7d:27:12:30:d5:61:1c:34:d2:7d:65:34:20:7c root@master
The key's randomart image is:
+--[ RSA 2048]----+
| o**=.o=|
| .+=E..o|
| . . . |
| o |
| So |
| .. |
| ... |
| ..oo . |
| ..oo o |
+-----------------+

 

[root@master ~]# cd /root/.ssh/
[root@master .ssh]# ll
total 8
#这就是master的公钥和私钥对
-rw------- 1 root root 1675 Mar 13 14:22 id_rsa
-rw-r--r-- 1 root root 393 Mar 13 14:22 id_rsa.pub
[root@master .ssh]# more id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD1pf10d8qtoUtDrwfZKYiEcrDAXcmATu9dmHyAwEPSXI+wQgaNI4H4GogTCWymyguIMl9MbsMrNuXNMZgA4
jbySaiuCvcB0a/1IoKzf3hZ9VnF/IxxTja5erJxKk5QOyhBNZJxOFdgqaWzTjlenx/Gm+BK2mbjN3RbD+AUoR06jEV80v8OYTD5j0HzvT0AlJRDcFaNFXDxBg
xQrZsSR482glD9+ukmgU0TSFOAkKbGt+aGhqQVwPjjfiEZ0rKyY908+6cVna720bmNB2WzvQkjThyFHvQ4WXyppedXYOOkjLwsqottK9VGT+6qNN1Sp+Vs68Q
XRI9k79WVFtIeEPCV root@master

 

[root@master .ssh]# cp id_rsa.pub authorized_keys
#把master的公钥发送给MHA-Manager,注意要更改名字,scp到/root/.ssh目录夹下
#第一次传输时,需要输入yes
[root@master .ssh]# scp authorized_keys root@118.25.50.236:/root/.ssh/
The authenticity of host '118.25.50.236 (118.25.50.236)' can't be established.
ECDSA key fingerprint is 22:49:b2:5c:7c:8f:73:56:89:29:8a:bd:56:49:74:66.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '118.25.50.236' (ECDSA) to the list of known hosts.
root@118.25.50.236's password:
authorized_keys 100% 393 0.4KB/s 00:00
 
############ MHA-Manager ##########################
#MHA-Manager这边收到master的公钥,需要更改权限为600
[root@MHA_Manager .ssh]# chmod 600 authorized_keys
#然后生成自己的公钥私钥对
[root@MHA_Manager .ssh]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
53:c9:ce:99:a3:63:37:ae:2a:a6:a5:69:6c:02:37:9a root@MHA_Manager
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . . |
| + |
| + o |
| S * |
|. o o . |
|.= .. + o |
|E ++o . + . |
| ++o ...... |
+-----------------+
[root@MHA_Manager .ssh]# ll
total 12
#以下就是MHA-Manager的公钥 私钥对
-rw------- 1 root root 393 Mar 13 14:25 authorized_keys
-rw------- 1 root root 1679 Mar 13 14:27 id_rsa
-rw-r--r-- 1 root root 398 Mar 13 14:27 id_rsa.pub
[root@MHA_Manager .ssh]# more id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDufx2SVQlykHPTQtenp7nb5jArwc6OjXBIZeYVUR2U7cKfhkW08IJQdotsNOyxgFEv3clIlkFiEOZ1nvLlq
tsRepAr/sy4KGfy+0Ucj3cx6sqvdxQoEIzKc/iUTLxTVJgVZrZQW9g0YyUv5i/cvAFQ9ZjSfQslS/zQvKolMVPo900VEJ1lxesOJ5oaNU3zpXVF4e8tWsePrT
/AxrUxEo0ZuHvf9YfXI8tj0KZsK62MflPLJg4+1ibbRkQtiMJi+i/F+Fk/IY/DKBZ2f7Wy/JCGxIi3Y5rCGbkQQD9XVEHUsmkFIxgiNQ4QFjB5CH7uMl6q47d
lyq24xm+3b9a3DJFf root@MHA_Manager
[root@MHA_Manager .ssh]# vim authorized_keys

#将MHA-Manager的公钥也放在authorized_keys文件中,注意一个公钥就是一行,切不可出现换行符

 
[root@MHA_Manager .ssh]# more authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD1pf10d8qtoUtDrwfZKYiEcrDAXcmATu9dmHyAwEPSXI+wQgaNI4H4GogTCWymyguIMl9MbsMrNuXNMZgA4
jbySaiuCvcB0a/1IoKzf3hZ9VnF/IxxTja5erJxKk5QOyhBNZJxOFdgqaWzTjlenx/Gm+BK2mbjN3RbD+AUoR06jEV80v8OYTD5j0HzvT0AlJRDcFaNFXDxBg
xQrZsSR482glD9+ukmgU0TSFOAkKbGt+aGhqQVwPjjfiEZ0rKyY908+6cVna720bmNB2WzvQkjThyFHvQ4WXyppedXYOOkjLwsqottK9VGT+6qNN1Sp+Vs68Q
XRI9k79WVFtIeEPCV root@master
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDufx2SVQlykHPTQtenp7nb5jArwc6OjXBIZeYVUR2U7cKfhkW08IJQdotsNOyxgFEv3clIlkFiEOZ1nvLlq
tsRepAr/sy4KGfy+0Ucj3cx6sqvdxQoEIzKc/iUTLxTVJgVZrZQW9g0YyUv5i/cvAFQ9ZjSfQslS/zQvKolMVPo900VEJ1lxesOJ5oaNU3zpXVF4e8tWsePrT
/AxrUxEo0ZuHvf9YfXI8tj0KZsK62MflPLJg4+1ibbRkQtiMJi+i/F+Fk/IY/DKBZ2f7Wy/JCGxIi3Y5rCGbkQQD9XVEHUsmkFIxgiNQ4QFjB5CH7uMl6q47d
lyq24xm+3b9a3DJFf root@MHA_Manager

 

将MHA-Manager的authorized_keys这个文件也发送给master
(如果是多台机器的话,同理将每台机器的公钥都统一复制到一个authorized_keys文件中,切记一个公钥是一行,中间不能出现换行符。然后再将authorized_keys这个统一的公钥文件,scp给其他机器)  
############ MHA-Manager ###############
[root@MHA_Manager .ssh]# scp authorized_keys root@111.231.79.212:/root/.ssh/
root@111.231.79.212's password:
authorized_keys 100% 794 0.8KB/s 00:00

############# master ###############

[root@master .ssh]# ll
total 16
-rw------- 1 root root 794 Mar 13 14:37 authorized_keys
-rw------- 1 root root 1675 Mar 13 14:22 id_rsa
-rw-r--r-- 1 root root 393 Mar 13 14:22 id_rsa.pub
-rw-r--r-- 1 root root 175 Mar 13 14:25 known_hosts
[root@master .ssh]# more authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD1pf10d8qtoUtDrwfZKYiEcrDAXcmATu9dmHyAwEPSXI+wQgaNI4H4GogTCWymyguIMl9MbsMrNuXNMZgA4
jbySaiuCvcB0a/1IoKzf3hZ9VnF/IxxTja5erJxKk5QOyhBNZJxOFdgqaWzTjlenx/Gm+BK2mbjN3RbD+AUoR06jEV80v8OYTD5j0HzvT0AlJRDcFaNFXDxBg
xQrZsSR482glD9+ukmgU0TSFOAkKbGt+aGhqQVwPjjfiEZ0rKyY908+6cVna720bmNB2WzvQkjThyFHvQ4WXyppedXYOOkjLwsqottK9VGT+6qNN1Sp+Vs68Q
XRI9k79WVFtIeEPCV root@master
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDufx2SVQlykHPTQtenp7nb5jArwc6OjXBIZeYVUR2U7cKfhkW08IJQdotsNOyxgFEv3clIlkFiEOZ1nvLlq
tsRepAr/sy4KGfy+0Ucj3cx6sqvdxQoEIzKc/iUTLxTVJgVZrZQW9g0YyUv5i/cvAFQ9ZjSfQslS/zQvKolMVPo900VEJ1lxesOJ5oaNU3zpXVF4e8tWsePrT
/AxrUxEo0ZuHvf9YfXI8tj0KZsK62MflPLJg4+1ibbRkQtiMJi+i/F+Fk/IY/DKBZ2f7Wy/JCGxIi3Y5rCGbkQQD9XVEHUsmkFIxgiNQ4QFjB5CH7uMl6q47d
lyq24xm+3b9a3DJFf root@MHA_Manager

 

实现效果如下:
[root@master .ssh]# sshman
Last login: Tue Mar 13 14:35:33 2018 from 111.231.79.212
 
[root@MHA_Manager .ssh]# sshmaster
Last login: Tue Mar 13 14:41:02 2018 from 118.25.50.236

 这里做了别名的配置(写在root里面,只对root有效)

[root@master ~]# egrep 'ssh' ~/.bashrc
#for ssh
alias sshman='ssh root@118.25.50.236'
 
[root@MHA_Manager ~]# egrep 'ssh' ~/.bashrc
#for ssh
alias sshmaster='ssh root@111.231.79.212'

 

总结:以前刚接触公钥私钥的时候,总是分不清。于是就记一下:私钥永远是自己的,把公钥分出去,能匹配我的私钥的,就是小伙伴(拥有公钥的)。

网上关于私钥公钥的文章很多,只是在搭建MHA的时候突然觉得可以把ssh免密写成笔记,也是当做给自己回忆一下linux一些知识点。

 

贴一篇写的很好的MHA详解:
http://www.cnblogs.com/gomysql/p/3675429.html
 

posted on 2018-03-13 15:30  Kid_Zora  阅读(2724)  评论(0编辑  收藏  举报

导航