【问题标题】:I can't able to SSH into remote server from Jenkins node using public and private Key我无法使用公钥和私钥从 Jenkins 节点通过 SSH 连接到远程服务器
【发布时间】:2021-01-09 04:20:15
【问题描述】:

我正在尝试从 Jenkins 节点远程 ssh 到远程 linux 机器。我在 Jenkins 节点中创建了公钥和私钥,并将公钥复制到远程机器上的 authorized_keys 中。但我无法远程进入远程机器。请检查以下日志。

/home/us-jenkins/jenkins/workspace/engineering/dex/college-dex@tmp/durable-9222db0d/script.sh: line 8: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
+ ssh -vvv ****@208.53.147.236
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 62: Applying options for *
Pseudo-terminal will not be allocated because stdin is not a terminal.
debug1: Executing proxy command: exec /usr/bin/sss_ssh_knownhostsproxy -p 22 208.53.147.236
debug1: permanently_drop_suid: 1990977432
debug1: identity file /home/us-jenkins/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/us-jenkins/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
ssh_exchange_identification: Connection closed by remote host
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE

如果需要任何详细信息,请告诉我。任何帮助将不胜感激。

【问题讨论】:

  • ssh_exchange_identification: Connection closed by remote host 这行表示与SSH服务器的连接在打开后立即关闭。 “执行代理命令”行表明您正在尝试通过另一个 ssh 连接代理连接到 208.53.147.236。问题可能是与 208.53.147.236 的连接失败。在尝试通过它代理连接之前,请确保它正常工作。
  • @Kenster 如果我需要检查 Jenkins 节点或远程服务器中的任何文件,请您详细解释一下。
  • @Kenster 我在 ~/.ssh 文件夹中没有找到任何 ssh_config 文件。执行此代理命令的位置。
  • 尝试在 ssh_config #ProxyCommand /usr/bin/sss_ssh_knownhostsproxy -p %p %h 中注释该行
  • @Kenster 我找不到 ssh_config 请在 /usr/bin -rwxr-xr-x 中找到以下详细信息。 1 根 774568 2019 年 6 月 26 日 ssh -rwxr-xr-x。 1 根 360920 2019 年 6 月 26 日 ssh-add-rwxr-xr-x。 1 根 10469 2019 年 6 月 26 日 ssh-copy-id -rwxr-xr-x。 1 根 419208 2019 年 6 月 26 日 ssh-keygen -rwxr-xr-x。 1 根 441024 2019 年 6 月 26 日 ssh-keyscan -rwxr-xr-x。 1 根 23920 2019 年 10 月 9 日 sss_ssh_authorizedkeys -rwxr-xr-x。 1 个根 28000 2019 年 10 月 9 日 sss_ssh_knownhostsproxy

标签: linux jenkins ssh jenkins-pipeline


【解决方案1】:

关注SSH Credentials Management with Jenkins,确保您拥有:

  • 在 jenkins 中将私钥注册为 SSH 凭据
  • 使用无密码私钥,或使用SSH Agent plugin
  • 在您的流水线步骤中使用这两个元素

Example:

stage ('Deploy') {
    steps{
        sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins']) {

作为noted in the commentsJenkins uses a proxy command,这意味着它的执行用户必须有一个ssh.config(如/etc/ssh/ssh_config),它指示任何SSH调用通过该代理。

如果所述代理失败,则会解释错误消息。

【讨论】:

  • 嗨 @VonC 我在 Jenkins 中将私钥注册为 Ssh 凭据,并在 Jenkins 中使用了 sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins'])管道阶段。但我仍然收到同样的错误。
  • @RaghuRam 您的私钥是否受密码保护?
  • 为了确保密钥工作正常,我将私钥复制到我的 Mac 机器并尝试通过 SSH 连接到远程服务器,该服务器成功运行。但是当从 Jenkins 做 SSH 时,我得到了这个错误。
  • 嗨@VonC 我的私钥不受密码保护。
  • @RaghuRam “正在执行代理命令”:起初我没有看到那部分。这将解释用户会话和 Jenkins 会话之间的区别。
【解决方案2】:

问题可以关闭。问题是我们的 jenkins 服务器处于受限网络中,因此 SSH 无法工作。我们取消了对防火墙的限制,问题已得到解决。

【讨论】:

  • @VonC 感谢您提出解决问题的建议。
猜你喜欢
  • 2022-07-11
  • 2011-05-21
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 2013-08-10
  • 2017-12-29
相关资源
最近更新 更多