如何使用私钥通过 ssh 连接到远程服务器?

Aiv*_*ler 99 ssh openssh

我有两台服务器。两台服务器都在 CentOS 5.6 中。我想使用我拥有的私钥(OpenSSH SSH-2 私钥)从服务器 1 SSH 到服务器 2。

我不知道如何通过 unix 做到这一点。但是我在 Windows 上使用 Putty 所做的是将我的 OpenSSH 私钥提供给 putty-gen 并生成 PPK 格式的私钥。

但是,我将从服务器 1 创建一个 bash 脚本,该脚本将通过 SSH 在服务器 2 上执行一些命令。

如何使用来自服务器 1 的私钥文件通过 SSH 连接到服务器 2?

ech*_*hox 82

您需要您的 SSH 公钥,您将需要您的 ssh 私钥。可以使用ssh-keygen. 私钥必须保存在服务器 1 上,公钥必须保存在服务器 2 上。

这在openssh的联机帮助页中有完整的描述,所以我会引用很多。您应该阅读“身份验证”部分。此外,openSSH 手册应该非常有帮助:http : //www.openssh.org/manual.html

请小心使用 ssh,因为这会影响您服务器的安全性。

来自man ssh

 ~/.ssh/identity
 ~/.ssh/id_dsa
 ~/.ssh/id_rsa
     Contains the private key for authentication.  These files contain
     sensitive data and should be readable by the user but not acces-
     sible by others (read/write/execute).  ssh will simply ignore a
     private key file if it is accessible by others.  It is possible
     to specify a passphrase when generating the key which will be
     used to encrypt the sensitive part of this file using 3DES.

 ~/.ssh/identity.pub
 ~/.ssh/id_dsa.pub
 ~/.ssh/id_rsa.pub
     Contains the public key for authentication.  These files are not
     sensitive and can (but need not) be readable by anyone.
Run Code Online (Sandbox Code Playgroud)

这意味着您可以将您的私钥存储在 .ssh 的主目录中。另一种可能性是通过-i参数开关告诉 ssh使用特殊的身份文件。也来自man ssh

 -i identity_file
     Selects a file from which the identity (private key) for RSA or
     DSA authentication is read.  The default is ~/.ssh/identity for
     protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro-
     tocol version 2.  Identity files may also be specified on a per-
     host basis in the configuration file.  It is possible to have
     multiple -i options (and multiple identities specified in config-
     uration files).
Run Code Online (Sandbox Code Playgroud)

这是用于私钥。现在您需要在服务器 2 上引入您的公钥。再次引用来自man ssh

  ~/.ssh/authorized_keys
         Lists the public keys (RSA/DSA) that can be used for logging in
         as this user.  The format of this file is described in the
         sshd(8) manual page.  This file is not highly sensitive, but the
         recommended permissions are read/write for the user, and not
         accessible by others.
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最简单方法是将文件复制到服务器 2 并将其附加到 authorized_keys 文件中:

scp -p your_pub_key.pub user@host:
ssh user@host
host$ cat id_dsa.pub >> ~/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)

必须允许 ssh 守护程序通过公钥进行授权,请参阅man ssh_config。通常这可以通过在配置文件中添加以下语句来完成:

PubkeyAuthentication yes
Run Code Online (Sandbox Code Playgroud)

  • 嗨,谢谢你的努力,但我只需要这个。`ssh -i 密钥文件` 谢谢! (19认同)
  • 生成密钥后,在服务器上安装它的最简单且推荐的方法是使用 `ssh-copy-id`:`ssh-copy-id -i ~/.ssh/foo.id_rsa remote.example.com`。 (11认同)
  • 有趣的是,每个人都忘记提及您需要在您连接的计算机上创建密钥后运行“ssh-add”。这就是大多数人头痛的原因。 (6认同)
  • 重要说明:客户端可以拥有多个私钥,并根据其私有 ~/.ssh/config 文件中的任意名称进行选择,其中 Host= 给出任意名称,HostName 给出名称或 IP 地址,Port= 目标端口, User 是目标用户名,ItentityFile= 指向私钥文件。此功能集经常被忽视,并且是许多配置问题的解决方案,例如具有多个密钥对,否则它们会在名称空间中发生冲突。 (3认同)

小智 31

我使用带有 -i 选项的 ssh 在此处添加您的密钥。

如果要将 arg1,arg2 与 .sh 文件一起传递,只需在 .sh 文件之后传递它并使用空格分隔它。

ssh -i home/avr/new.pem ar@231.221.54.8 "/var/www/beta/betatolive.sh mmin 30"

  • 不要忘记设置正确的权限:`chmod 600 home/avr/new.pem` (3认同)

小智 22

您需要做的第一件事是确保您已运行 keygen 命令来生成密钥:

ssh-keygen -t rsa
Run Code Online (Sandbox Code Playgroud)

然后使用此命令将密钥推送到远程服务器,修改它以匹配您的服务器名称。

cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'
Run Code Online (Sandbox Code Playgroud)

  • 让我们改为调用`ssh-copy-id user@hostname` (8认同)

Kev*_*vin 8

id_[rd]sa.pub您的源计算机(您从中~/.ssh/authorized_keys进行 ssh 连接)的公钥 ( )附加到您要 ssh 到的用户名的目标服务器的文件中。如果您丢失了公钥,则需要使用ssh-keygen. 对于大多数用途,使用默认参数应该没问题。如果您需要更详细的说明,您可以谷歌搜索数千个教程。


Sid*_*hou 8

ssh-copy-id -- 使用本地可用的密钥来授权远程机器上的登录

ssh-copy-id在服务器 1 上使用,假设您有密钥对(使用 生成ssh-keygen):

ssh-copy-id -i ~/.ssh/id_rsa user@server2_hostname
Run Code Online (Sandbox Code Playgroud)

现在您应该可以使用私钥通过 ssh ssh 进入服务器 2

ssh -i ~/.ssh/id_rsa user@server2_hostname
Run Code Online (Sandbox Code Playgroud)

事实上,如果您检查cat ~/.ssh/authorized_keys服务器 2,您会看到已为您附加了公钥。