本文抄自:https://github.com/nkypy/blog/issues/17
正文前先对比一下:
3072 的 RSA 的安全性 =256 的 ECC(包括 ECDSA 和 25519(EdDSA))
25519 相对于传统 NIST P-256 曲线的 ECDSA 效率与速度更快且为确定性签名。
一、本地配置
1.1 在 Windows PowerShell 中运行命令
ssh-keygen -t ed25519 -C "oracledubai" # 随便起个名称, 本例以 oracleduba
Enter passphrase (empty for no passphrase):
对密钥加个私人密码,输入密码或回车留空。
Enter same passphrase again:
# 确认密码或回车。
显示
Generating public/private ed25519 key pair.Enter file in which to save the key (/home/lenovo/.ssh/id_ed25519): #文件保存位置, 一般默认即可。
显示
Enter passphrase (empty for no passphrase): #对密钥加个私人密码,输入密码或回车留空。
Enter same passphrase again:# 确认密码或回车。
显示
Your identification has been saved in /home/lenovo/.ssh/id_ed25519 #生成的私钥
Your public key has been saved in /home/lenovo/.ssh/id_ed25519.pub #生成的公钥
The key fingerprint is:
SHA256:PdwszW0JXELbXMuQXDGN+xfKfCMd6YQWEB4 oracledubai #指纹
The key's randomart image is:
+--[ED25519 256]--+
| Eo.+ . .o ..ooo|
| . .= = . + . +=|
| ..= = o o o . o|
|o = o o o B = . |
|o+ + S = B + |
|oo+ o . |
|= o |
|..o |
|.. |
+----[SHA256]-----+
- id_ed25519
是私钥,
id_ed25519.pub 是公钥。 - 保存在 C:\Users\lenovo.ssh 路径中。lenovo 为计算机名,不知道是啥的看上面路径 home 后面的名称。
二、VPS 端操作
2.1 以 root 登录 vps
mkdir /root/.ssh/
2.2 上传公钥
将 id_ed25519.pub 上传到 /root/.ssh/ 路径下,并运行
cat /root/.ssh/id_ed25519.pub >> /root/.ssh/authorized_keys
2.3 修改密钥项
vim /etc/ssh/sshd_config
将下列两行的井号去掉
#PubkeyAuthentication yes #允许密钥认证
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 #默认公钥位置
service sshd restart #重启 sshd
2.4 以密钥方式重新登录
注意:以密钥方式重新登录
2.5 关闭密码登录
运行的前提是:能够密钥登录 VPS
vim /etc/ssh/sshd_config
将后面的 PasswordAuthentication yes
修改为:PasswordAuthentication no
service sshd restart #重启 sshd
三、常见的几种密钥生成方式对比
DSA: 它是不安全的,甚至从 OpenSSH 第 7 版开始就不再支持,你需要升级它!
RSA: 这取决于密钥大小。如果它有 3072 或 4096 位的长度,那么你就很好。低于这个长度,你可能要升级它。1024 位的长度甚至被认为是不安全的。
ECDSA:这取决于你的机器能产生一个随机数的程度,这个随机数将被用来创建签名。在 ECDSA 使用的 NIST 曲线上也有一个可信度问题。