【问题标题】:SSH from inside container docker to gcp instance in subnetwork从容器 docker 内部 SSH 到子网中的 gcp 实例
【发布时间】:2018-10-24 17:42:47
【问题描述】:

我已经将公钥添加到目标实例的授权密钥。但我仍然无法从容器内部 ssh 到实例。我这样做是因为我在容器中使用詹金斯。但是我可以使用来自主机实例的公钥从外部容器 ssh 到目标实例。容器和主机实例的密钥不同。

[jenkins@4f5159051056 /]$ ssh jenkins@10.64.0.5 -v 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 58: Applying options for * debug1: Connecting to 10.64.0.5 [10.64.0.5] port 22. debug1: Connection established. debug1: identity file /var/jenkins_home/.ssh/id_rsa type 1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /var/jenkins_home/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.4 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4p1 Debian-10+deb9u3 debug1: match: OpenSSH_7.4p1 Debian-10+deb9u3 pat OpenSSH* compat 0x04000000 debug1: Authenticating to 10.64.0.5:22 as 'jenkins' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: curve25519-sha256 need=64 dh_need=64 debug1: kex: curve25519-sha256 need=64 dh_need=64 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2QOdKiyn5cehOBPIfQFaEo8WvZz6BJYq4fc7gNIIJ1g The authenticity of host '10.64.0.5 (10.64.0.5)' can't be established. ECDSA key fingerprint is SHA256:2QOdKiyn5cehOBPIfQFaEo8WvZz6BJYq4fc7gNIIJ1g. ECDSA key fingerprint is MD5:13:03:df:e8:e5:4b:f7:4a:9b:65:82:c3:9d:80:11:f4. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.64.0.5' (ECDSA) to the list of known hosts. debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /var/jenkins_home/.ssh/id_rsa debug1: Authentications that can continue: publickey debug1: Trying private key: /var/jenkins_home/.ssh/id_dsa debug1: Trying private key: /var/jenkins_home/.ssh/id_ecdsa debug1: Trying private key: /var/jenkins_home/.ssh/id_ed25519 debug1: No more authentication methods to try. Permission denied (publickey).

【问题讨论】:

  • 您在为用户“jenkins”创建密钥对后是否尝试过: 1. 在容器的路径中添加私钥(例如 /var/jenkins_home/.ssh/ ),2. 添加通过在metadata 中添加 GCE 实例的公钥 3. 使用私钥文件的 SSH ( ssh -i Privatekeyfilewithpath user@IP)

标签: docker jenkins ssh google-cloud-platform


【解决方案1】:

您是否检查了以下内容

  1. 设置适当的权限:

chmod 700 /var/jenkins_home/.ssh chmod 644 /var/jenkins_home/.ssh/authorized_keys

  1. 在您的情况下,所有者应该是 Jenkins

chown -R jenkins:jenkins /var/jenkins_home/*

另外你正在使用 ssh -v 在客户端检查,你需要在服务器端检查为什么服务器不接受密钥 Permission denied (publickey)。尝试在下面的位置检查

sudo vi /var/log/secure

已编辑

如果遇到以下问题

localhost sshd[36601]: 来自 xxx.xxx.xxx.xxx 的用户 jenkins 不允许,因为 AllowUsers 中没有列出

还有这个

localhost sshd[36601]: input_userauth_request: 无效 用户 myuser [preauth]

将用户 jenkins 添加到以下文件中

vim /etc/ssh/sshd_config

然后重启服务

systemctl 重启 sshd.service

现在应该可以了 :)

【讨论】:

  • 我已经做到了,我总是得到input_userauth_request: invalid user jenkins
  • @PhilipMoniaga 如果这有助于您解决问题,请选择它作为正确答案,以便其他遇到此问题的人知道需要做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
相关资源
最近更新 更多