1·生成ssh密钥对(如果没有现成的密钥)

本地机器终端并运行以下命令:

ssh-keygen -t ed25519

2·将公钥复制到目标服务器(id_ed25519.pub)

mkdir -p /root/.ssh && touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys && chmod 700 /root/.ssh
echo "ssh公钥" >> /root/.ssh/authorized_keys
echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEKbgop4Jr9ghbH3PdLsM6w4yi57rgrOPJ4VQjLBFL/s yishang@DESKTOP-BMVT1P1" >> /root/.ssh/authorized_keys
  • 注意:也可以直接复制公钥到/root/.ssh/authorized_keys

3·修改ssh配置文件

/etc/ssh/sshd_config
PubkeyAuthentication yes           # 启用公钥认证
AuthorizedKeysFile     .ssh/authorized_keys  # 公钥存储位置
PasswordAuthentication no          # 禁用密码登录

4·重启ssh服务

systemctl restart ssh

5·利用私钥(id_ed25519)登陆