一、公钥与秘钥


 

比如,张三平常使用密码连接到服务器A的root账户,现在可以利用公钥,免密码连接到服务器A的root账户,首先,张三要生成一对密钥,私钥与公钥,私钥是自己保留的,一定不要泄露给它人,公钥是给别人用的,张三把公钥发给自己的朋友,朋友们就能用张三的公钥加密信息或者验证身份,当张三准备好了私钥与公钥,只要把公钥交给"服务器A的root账户",当张三再次连接服务器A的root账户时,root账户就会使用张三的公钥验证张三的身份,此时,张三只要拿出自己的私钥,即可通过身份验证,成功的连接到服务器A的root账户中,当然,张三还可以把公钥交给服务器B的zsy账户,也可以把公钥交给服务器C的think账户,或者任意一个张三想要连接的服务器账户,都能够通过上述同样的方法通过认证,当然,如果有些心怀不轨的人窃取了张三的私钥,那么这个人就能冒充张三,因为拥有张三公钥的服务器"只认密钥不认人",所以,我们一定要保管好自己的私钥,非对称加密并不是此处总结的重点,此处主要总结一下与ssh相关的一些命令,我们使用ssh-keygen命令即可生成私钥与公钥。

二、创建秘钥


 

[root@k8s-master ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  
#因为当前系统账户为root,所以,默认生成密钥路径为/root/.ssh/id_rsa,如果不指定其他路径,直接回车即可;
#/root/.ssh/id_rsa already exists. Overwrite (y/n)?
#如果对应目录下已经存在了同名的密钥文件,还会提示你是否覆盖,在没有搞清楚是谁的密钥之前,最好不要覆盖,否则可能会导致私钥的丢失
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:d*0123456Ls root@k8s-master The key's randomart image is: +---[RSA 2048]----+ | o o+.. | | ..o o+ . | |.o o . +. | |o +..+ .+... | | * ++.o.S+. . | | Bo=.o o. . | | Eo*.o. | |.o.+=. | | o*+ | +----[SHA256]-----+

完成上述步骤后,即在用户家目录的.ssh目录中生成了一对密钥文件,私钥文件id_rsa与公钥文件id_rsa.pub

从文件名称可以看出,我们生成的是rsa密钥,我们也可以选择其他算法,从而生成其他类型的密钥文件,但是我们在生成秘钥时并未指定算法类型,所以,默认生成了RSA类型的密钥,现在大家都在版本2的ssh协议,在版本2的ssh协议中我们可以使用的密钥类型有: "dsa", "ecdsa", "ed25519", "rsa"

三、公钥交换


 

通过如下命令,可以将本机上指定的的公钥交给192.168.1.82的root账户

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.82

使用"-i"选项指定要传输的公钥,然后指明账户与IP地址,如下图所示,传输公钥时,要求我们输入root账户的密码,因为我们必须要知道密码,才能合法的连接到root账户啊,当然,这种密码验证只需要进行一次即可。

[root@k8s-master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.82
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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@192.168.1.82's password: 

Number of key(s) added: 1
#提示我们已经添加了1个密钥,并且提示我们尝试使用'ssh root@192.168.1.82'进行验证。
Now try logging into the machine, with:   "ssh 'root@192.168.1.82'"
and check to make sure that only the key(s) you wanted were added.

连接ssh

[root@k8s-master ~]# ssh root@192.168.1.82
Enter passphrase for key '/root/.ssh/id_rsa': 
#因为建立秘钥是设置了密码,这里需要输入秘钥的密码;
Last
login: Mon Sep 28 15:04:00 2020 from 192.168.1.81 Welcome to Alibaba Cloud Elastic Compute Service ! [root@k8s-worker01 ~]#

公司中的sshd服务不会使用默认端口,所以,在使用ssh-copy-id命令时,可能需要指定sshd服务端的端口号。

但是在centos6与centos7中,使用ssh-copy-id指定sshd服务器端口的方法略有不同,假设对方sshd服务器端口号为22222,示例如下

当前系统为centos6:ssh-copy-id -i ~/.ssh/id_rsa.pub "zsy@192.168.1.82 -p 9922"

当前系统为centos7:ssh-copy-id -i ~/.ssh/id_rsa.pub zsy@192.168.1.82 -p 9922

 

我们已经把公钥交给了192.168.1.82的root账户,那么,root账户把我们的公钥放在了哪里呢?其实,用户会把别人的公钥放在家目录的.ssh/authorized_keys文件中

我们来一起看一下,root账户的authorized_keys中的内容,如下图

[root@k8s-worker01 ~]# cat /root/.ssh/authorized_keys 
ssh-rsa AA*3+Q4*3wN+9o*5ig/2bu*Fwq+uMa*4BoeKr root@k8s-master

 

原文链接:http://www.zsythink.net/archives/2375

 

posted on 2020-09-28 15:16  Mr_星火  阅读(187)  评论(0编辑  收藏  举报