推荐使用 ssh-keygen -t ed25519 -C “[email protected]”(现代通用)或 ssh-keygen -t rsa -b 4096 -C “[email protected]”(兼容老旧设备),避免 rsa-1024。
ssh-keygen 生成密钥对时该用什么参数
默认直接运行 ssh-keygen 会生成 RSA 4096 位密钥(OpenSSH 8.8+ 默认),但老系统或某些设备只支持 RSA 2048 或 ECDSA。如果后续遇到 no mutual signature algorithm 或登录失败,大概率是算法不兼容。
推荐明确指定算法和长度:
- 现代通用场景:
ssh-keygen -t ed25519 -C "[email protected]"(速度快、安全性高、体积小) - 需兼容老旧设备:
ssh-keygen -t rsa -b 4096 -C "[email protected]" - 避免使用
-t rsa -b 1024,已被 OpenSSH 8.7+ 默认禁用
密钥默认保存在 ~/.ssh/id_ed25519 和 ~/.ssh/id_ed25519.pub,不要手动改名,否则 ssh 客户端可能无法自动加载。
ssh-copy-id 失败的常见原因和绕过方法
ssh-copy-id 是最简方式,但它依赖目标主机已开启 SSH 且能正常执行远程命令。失败时先检查:ssh user@host 'echo test' 是否通;~/.ssh/authorized_keys 目录权限是否为 700,文件是否为 600;SELinux 是否拦截(ls -Z ~/.ssh 查看上下文)。
若仍失败,可手动推送: