VPS采用Ed25519密钥登录

Ed25519 是目前最推荐的公钥算法,相比 RSA 更安全且效率更高。


前言

本文介绍如何在 VPS 上配置 Ed25519 密钥登录,相比传统密码登录更安全。

安全性对比

算法 安全性 备注
DSA 不安全 OpenSSH 7.0+ 已废弃
RSA 3072/4096 位安全 1024 位不安全
ECDSA 一般 存在信任问题
Ed25519 最安全 推荐使用

3072 位 RSA 的安全性 ≈ 256 位 ECC(包括 Ed25519)


一、本地配置(Windows)

1.1 生成 Ed25519 密钥

在 PowerShell 中执行:

1
ssh-keygen -t ed25519 -C "your_comment"

按提示操作:

  • 选择保存位置(默认 ~/.ssh/
  • 设置密钥密码(可留空)

1.2 密钥说明

  • id_ed25519 - 私钥(保密)
  • id_ed25519.pub - 公钥(可公开)

Windows 密钥保存在:C:\Users\你的用户名\.ssh\


二、VPS 端操作

2.1 创建 SSH 目录

1
2
3
# 以 root 登录 VPS
mkdir -p /root/.ssh
chmod 700 /root/.ssh

2.2 上传公钥

将本地生成的 id_ed25519.pub 上传到 VPS,然后执行:

1
2
cat /root/.ssh/id_ed25519.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

2.3 修改 SSH 配置

1
vim /etc/ssh/sshd_config

确保以下配置启用:

1
2
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

重启 SSH 服务:

1
service sshd restart

2.4 验证密钥登录

使用密钥重新登录 VPS:

1
ssh -i ~/.ssh/id_ed25519 user@your_vps_ip

2.5 关闭密码登录

前提:确保密钥登录正常!

1
vim /etc/ssh/sshd_config

修改:

1
PasswordAuthentication no

重启 SSH:

1
service sshd restart

三、常见密钥算法对比

算法 推荐 说明
DSA 不安全,已废弃
RSA ⚠️ 建议 3072/4096 位
ECDSA 存在信任问题
Ed25519 最佳推荐