ssh密钥讲解

Posted yinsedeyinse

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh密钥讲解相关的知识,希望对你有一定的参考价值。

我们用ssh连接机器时候需要输用户名、密码,但是直接写账户文件的时候由于用的是明文,就存在安全的问题了。别人一旦截取了数据就获得了隐私了。这时候就用上ssh的密钥。

ssh的密钥存是成对出现的,一个叫公钥,一个是私钥。公钥是给别人的,私钥存在自己手里。在连接的时候两把钥匙配对成功,就可以建立数据连接。

[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory /root/.ssh.
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:uRVPB4eYb3mXG7j7BcIeOEG83TSS7ApCzyDdU5P1abE [email protected]
The keys randomart image is:
+---[RSA 2048]----+
|     . . o+*ooo  |
|    . + o.=.=+o+ |
|     o + .o=.=E..|
|      . +..B*+oo.|
|       .S.+o=.o.o|
|         o.o + o |
|        .   . . .|
|             .  .|
|              .. |
+----[SHA256]-----+

 

我们在看看这对密钥

[[email protected] ~]# cat /root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA4oRNlaIoefdsSuYmoLUdV7C2JtzKv6hd6UsC1FUEFfgjn/SV
5StzkVqnOWwEed77WOwW2my+n7pW6G3AyXtPAgrdih7Jpn6RG0AW3D0tkOlLN2/j
L3znlfsqGpQ63hBPRPjSswmCMILfRhOnY+KbsAEmeClcQIjUEsljomSMS2BT+zTZ
HyI1QLTdeET/DKxSrOcQPy+qNSSma7dAGDINNT8CVC7tdj62PVj1vgJ/lXJeTR79
wh0aKHuBfTp9zqBDVQRzAhjX4I3xBTwKh+tgdSrX55ltQqhxA0KaqvoB2ZCG5VuE
6U4XI8DUUEDtBY+AmCSUtWH+bDMwOI5oo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xjKfGQKBgQDIOERmoRikK4qihhp6eKaVL8slCKNc+qxBhAWTr6Da4lfXrd8GkiNR
WPH4ZNHlIq6Cv8qZaypqdkrw9CsIHYDPQ3gSj/C3PMmXU24f6c2LkEhauOInK+22
qHHJv0blEfQWIs5Uj0yJC5qetGxyT+6n7kTdxgKzXteTX8ltDtvJqg==
-----END RSA PRIVATE KEY-----
[[email protected] ~]# 
[[email protected] ~]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDihE2Voih592xK5iagtR1XsLYm3Mq/qF3pSwLUVQQV+COf9JXlK3ORWqc5bAR53vtY7BbabL6fulbobcDJe08CCt2KHsmmfpEbQBbcPS2Q6Us3b+MvfOeV+yoalDreEE9E+NKzCYIwgt9GE6dj4puwASZ4KVxAiNQSyWOiZIxLYFP7NNkfIjVAtN14RP8MrFKs5xA/L6o1JKZrt0AYMg01PwJULu12PrY9WPW+An+Vcl5NHv3CHRooe4F9On3OoENVBHMCGNfgjfEFPAqH62B1KtfnmW1CqHEDQpqq+gHZkIblW4TpThcjwNRQQO0Fj4CYJJS1Yf5sMzA4jmig10vn [email protected]
[[email protected] ~]# 

 我们现在启动两台linux,用户名分别是user_on_1和user_on_2

技术分享图片

可以看到各自的ip和用户名。现在我们在user1上用ssh登陆user2

其实在IP后面应该还有个-p端口号,这里省略了。

技术分享图片

发现是需要输入登陆密码的,不是我们要的结果。我们在user1的/etc/.ssh文件夹下查找authorized_keys这个文件,把use2的公钥放进去,重新登陆,应该就没问题了!

对了,要把.ssh文件夹的权限改成700,authorized_keys的权限改为600

chmod 700 .ssh
chmod 600 authorized_keys

ps:我这里说的是应该,因为我也没成功,网上查的资料说StrictModes设置有问题,改了依旧没成功,先记下来,随着Linux的学习留给以后解决吧——20190118.


以上是关于ssh密钥讲解的主要内容,如果未能解决你的问题,请参考以下文章

如何构建密钥对验证的SSH体系

ssh 讲解

SSH远程管理,构建密钥对验证的SSH体系,设置SSH代理功能。

Git配置多ssh密钥

如何避免每次推送或拉取代码时都添加 ssh 密钥

【git】clone代码、ssh、权限问题