ssh密钥的生成与使用

一 客户端是linux
1.在客户端生成密钥对linux

[root@localhost ~]# ssh-keygen -t rsa <== 创建密钥对,-t表明类型,有RSA和DSA两种
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   <==密钥文件默认存放位置,按Enter便可
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):     <== 输入密钥锁码,或直接按 Enter 留空
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:K1qy928tkk1FUuzQtlZK+poeS67vIgPvHw9lQ+KNuZ4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|           +.    |
|          o * .  |
|        . .O +   |
|       . *. *    |
|        S =+     |
|    .    =...    |
|    .oo =+o+     |
|     ==o+B*o.    |
|    oo.=EXO.     |
+----[SHA256]-----+

密钥锁码在使用私钥时必须输入,这样就能够保护私钥不被盗用。固然,也能够留空,实现无密码登陆,这里密钥锁码设置为空。完成上述步骤后,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件,其中id_rsa 为私钥,id_rsa.pub 为公钥。web

2.把公钥传输至远程服务器
这里的远程服务器是172.16.8.11shell

[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.8.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.8.11 (172.16.8.11)' can't be established.
ECDSA key fingerprint is SHA256:IpS8Vw0F/Yxece36yQ9KhoIuxHFjSi/Ect/WHGxw//k.
ECDSA key fingerprint is MD5:03:85:10:9a:21:75:d6:f1:7a:6e:fd:a8:08:c5:ef:59.
Are you sure you want to continue connecting (yes/no)? yes     <== 是否继续链接,回答为yes  
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.8.11's password:    <==输入远程主机的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.16.8.11'"
and check to make sure that only the key(s) you wanted were added.

3.测试vim

[root@localhost ~]# ssh 172.16.8.11 

能够看出,无需输入密码便可直接登陆ruby

二,客户端为Windows
这里使用xshell来生成密钥对
1.在客户端生成密钥对
点击Xshell菜单栏的工具,选择新建用户密钥生成向导
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述服务器

2.复制生成的公钥到远程主机里面 ~/.ssh/authorized_keys 文件中。若是没有.ssh目录,须要本身创建一个,并更改目录权限为700dom

[root@localhost ~]# mkdir ~/.ssh
[root@localhost ~]# chmod 700 ~/.ssh/
[root@localhost ~]# vim ~/.ssh/authorized_keys

粘贴公钥的内容,保存退出ssh

3.测试
用xshell新建一个窗口
这里写图片描述
这里写图片描述
这里写图片描述
点击链接便可实现无密码登陆ide