generate

ssh-keygen -t ed25519 -C "comment"

ssh-copy-id

把本机公钥复制到远程服务器,实现 SSH 免密登录。

ssh-copy-id user@host

如果 SSH 端口不是默认的 22

ssh-copy-id -p 2222 user@host

如果要指定公钥文件:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host

复制完成后测试登录:

ssh user@host

如果远程 SSH 使用非默认端口:

ssh -p 2222 user@host

常见问题

确认远程服务器开启公钥认证:

sudo vim /etc/ssh/sshd_config
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

修改后重启 SSH 服务:

sudo systemctl restart sshd