在 Linux 服务器上一键启用 root 密钥登录。自动生成密钥手动粘贴公钥drop-in 优先
自动下载脚本并执行,适用于已安装 curl 的 Linux 系统。
curl -fL https://sh.guo.work/api/sshkey -o /tmp/sshkey.sh && sudo bash /tmp/sshkey.sh
如果目标系统没有 curl,请先执行 apt install curl 或 dnf install curl 等对应命令。
curl -fL https://sh.guo.work/api/sshkey -o /tmp/sshkey.sh
chmod +x /tmp/sshkey.sh
sudo bash /tmp/sshkey.sh
| 能力 | 说明 |
|---|---|
| 自动检测系统 | 读取 /etc/os-release,识别 Oracle Linux、Ubuntu、Debian、Alpine 等 |
| 密钥生成 | 自动生成 ed25519 密钥对,或手动粘贴已有公钥 |
| 配置写入 | 优先写入 /etc/ssh/sshd_config.d/,不存在时回退主配置 |
| 服务管理 | systemd 与 OpenRC 双兜底 |
| 语法检查 | sshd -t 通过后才重启 |
执行后不要关闭当前 SSH 会话。另开终端用私钥登录验证成功后,再关闭旧会话,避免锁死。
脚本内含交互式 read -rp,必须在真实终端中运行,不能通过管道或非交互环境执行。