Vultr服务器如何添加和删除SSH密钥

使用用户面板更改正在运行的服务器上的 SSH 密钥将重新安装并擦除服务器上的所有数据。您可以按照本快速入门指南手动更改 SSH 密钥,而无需重新安装服务器。

先决条件

  • 要遵循本指南,您必须具有 OpenSSH 格式的 SSH 密钥。请参阅我们的指南以创建 SSH 密钥
  • 本指南适用于 Linux 和 BSD 服务器。
  • 本指南适用于 Linux、Mac 或 Windows系统的电脑。

将 SSH 密钥添加到 Vultr 实例

使用 .ssh-copy-id

Linux 和 Mac

ssh-copy-id 预装在大多数 Linux 发行版上。Mac 工作站可以通过 Homebrew、MacPorts 或使用 Curl 进行安装

使用ssh-copy-id将公钥添加到服务器。

使用 -i [path to public key]  参数指定正确的公钥。

指定用户名和服务器 IP 地址(或域名),如下所示。例如,位于 192.0.2.123 的 root 用户:

 $ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.0.2.123

ssh-copy-id将报告一些基本信息并提示输入您的密码。

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/example_user/.ssh/id_rsa.pub"
  /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  root@192.0.2.123's password:

输入您的密码,实用程序将安装密钥。

Number of key(s) added: 1 
Now try logging into the machine, with: "ssh 'root@192.0.2.123'" and check to make sure that only the key(s) you wanted were added.

Windows

Windows 可以使用 PowerShell 命令模拟 ssh-copy-id 。所示示例通过 SSH 将公钥从当前用户的配置文件传递到 root 用户的 ~/.ssh/authorized_keys

PS> type $env:USERPROFILE\.ssh\id_rsa.pub | ssh root@192.0.2.123 "cat >> .ssh/authorized_keys"

适用于任何操作系统的替代方法

  1. 通过 SSH 连接到服务器。
  2. 编辑 ~/.ssh/authorized_keys
  3. 将公钥附加到文件中。
  4. 保存并退出。

删除 SSH 密钥

没有常见的自动删除公钥的方法,您必须手动删除它。

  1. 通过 SSH 连接到您的服务器。
  2. 编辑 ~/.ssh/authorized_keys
  3. 删除包含密钥的行。
  4. 保存并退出。
分享你的喜爱

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注