公钥登录,很多时候也说public key认证,公钥登录的原理:首先用户将自己的公钥存储在需要登录的远程机器上面,然后登录的时候,远程主机会向用户发送一段随机字符串,接着用户使用自己的私钥加密字符串,并发给远程主机。最后,远程主机使用存储的公钥进行解密,若解密成功,则说明用户可信,准许登录,不在提示输入密码。
一、生成公钥和私钥
ssh-keygen命令,可以生成公钥和私钥,默认生成到用户的家目录下的.ssh文件夹里面
私钥默认会被保存在 /root/.ssh/id_rsa
公钥默认会被保存在 /root/.ssh/id_rsa.pub
注意生成密钥之前必须切换root用户,不然无法进入root文件夹已经给root文件夹的.ssh中的密钥chmod 777 权限!!!
切换到/root用户
su root
切换到root目录
cd /root/
新建.ssh
文件夹(如果存在可以不用新建)
mkdir .ssh
生成密钥命令的格式如下:
ssh-keygen -f fileName -t rsa -C "dequan@example.com"
参数解析:
-t type:指定要生成的密钥类型,有rsa1(SSH1),dsa(SSH2),ecdsa(SSH2),rsa(SSH2)等类型,较为常用的是rsa类型
-C comment:提供一个新的注释(C必须是大写)
-b bits:指定要生成的密钥长度 (单位:bit),对于RSA类型的密钥,最小长度768bits,默认长度为2048bits。DSA密钥必须是1024bits
-f filename:指定生成的密钥文件名字
二、实战生成密钥案例
例子1:
ssh-keygen -f HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
输出:
[root@localhost .ssh]# ssh-keygen -f HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in HonKer_Red_Linux_8_SSH.
Your public key has been saved in HonKer_Red_Linux_8_SSH.pub.
The key fingerprint is:
SHA256:gUDsfNg0GPwtkiDrBcNaOSF/B74fax1wFYJn4uPCLXE honkerdynamo@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|o .*=o .. o. |
|.*=.+o=.oo |
|.++=oB+*o |
|o oBoEo.. |
|. ...B.oS |
| . +.o+ . |
| o+ . |
| . |
| |
+----[SHA256]-----+
翻译:
[root@localhost .ssh]# ssh-keygen -f HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
生成公钥/私钥 rsa 密钥对。
输入密码(空表示没有密码):
再次输入相同的密码:
您的身份信息已保存在 HonKer_Red_Linux_8_SSH 中。
您的公钥已保存在 HonKer_Red_Linux_8_SSH.pub 中。
关键指纹是:
SHA256:gUDsfNg0GPwtkiDrBcNaOSF/B74fax1wFYJn4uPCLXE honkerdynamo@gmail.com
钥匙的 randomart 图像是:
+---[RSA 3072]----+
|o .*=o .. o. |
|.*=.+o=.oo |
|.++=oB+*o |
|o oBoEo.. |
|. ...B.oS |
| . +.o+ . |
| o+ . |
| . |
| |
+----[SHA256]-----+
例子2:
命令:
ssh-keygen -f /root/.ssh/HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
输出:
[root@localhost .ssh]# ssh-keygen -f /root/.ssh/HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/HonKer_Red_Linux_8_SSH.
Your public key has been saved in /root/.ssh/HonKer_Red_Linux_8_SSH.pub.
The key fingerprint is:
SHA256:6td/A3JuNJPKXAYgXyo+fo6+CzzyEmeKxRt/kWFPWsk honkerdynamo@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
| |
| . . . |
| + = |
| + E . |
| . o OS . . |
| =.o *.. . X |
| o.O+..o + O + |
|. +o.+o.o = o o |
| ..o**. o.. . |
+----[SHA256]-----+
翻译:
[root@localhost .ssh]# ssh-keygen -f /root/.ssh/HonKer_Red_Linux_8_SSH -t rsa -C "honkerdynamo@gmail.com"
生成公钥/私钥 rsa 密钥对。
输入密码(空表示没有密码):
再次输入相同的密码:
您的身份信息已保存在 /root/.ssh/HonKer_Red_Linux_8_SSH 中。
您的公钥已保存在 /root/.ssh/HonKer_Red_Linux_8_SSH.pub 中。
关键指纹是:
SHA256:6td/A3JuNJPKXAYgXyo+fo6+CzzyEmeKxRt/kWFPWsk honkerdynamo@gmail.com
钥匙的 randomart 图像是:
+---[RSA 3072]----+
| |
| . . . |
| + = |
| + E . |
| . o OS . . |
| =.o *.. . X |
| o.O+..o + O + |
|. +o.+o.o = o o |
| ..o**. o.. . |
+----[SHA256]-----+