我在 Google Cloud Platform 上设置了两台虚拟机。server1
使用 Ubuntu 16.04 映像 [g1-small(1 vCPU,1.7 GB 内存)];新创建的虚拟机server2
使用 CoreOS Stable 映像 [f1-micro(1 vCPU,0.6 GB 内存)]。
server2
主要问题是即使我使用相同的 SSH 密钥和用户也无法连接。
我将使用此链接比较 的两个输出结果ssh -v
。
为了确保测试正确,我删除了文件夹中的文件google_compute_engine
、google_compute_engine.pub
和google_compute_known_hosts
内容。known_hosts
/Users/userz/.ssh
然后执行命令gcloud init
,然后gcloud compute config-ssh
,它检测到没有 SSH 密钥,并指导我创建一个新的密钥,同时要求输入密码,不是密码。该新 SSH 密钥是本次测试中使用的密钥。
如果您看到 的第一个链接server1
,ssh 最终会要求输入密码。但是server2
ssh 最终会要求输入密码,无论我输入什么,每次都会显示输入错误。
我尝试的密码是:相同的密码短语、我的 Google 帐户登录密码、本地计算机的管理员密码以及我记得使用过的所有其他密码。但输入错误。
为什么当我使用相同的方法连接到虚拟机时会出现这种差异?我该怎么做才能解决这个问题,因为我不知道也没有在server2
(新创建的)上设置任何密码?
我可以补充一点,通过连接Cloud Shell
需要输入密码并轻松连接。
server1
输出:
ssh -v server1
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/userz/.ssh/config
debug1: /Users/userz/.ssh/config line 51: Applying options for server1
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to XXX.XXX.XX.XX [XXX.XXX.XX.XX] port 22.
debug1: Connection established.
debug1: identity file /Users/userz/.ssh/google_compute_engine type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/userz/.ssh/google_compute_engine-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.2p2 Ubuntu-4ubuntu2.2
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to XXX.XXX.XX.XX:22 as 'userz'
debug1: using hostkeyalias: compute.hostkeyaliasX
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
debug1: using hostkeyalias: compute.hostkeyaliasX
debug1: Host 'compute.hostkeyaliasX' is known and matches the ECDSA host key.
debug1: Found key in /Users/userz/.ssh/google_compute_known_hosts:1
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=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/userz/.ssh/google_compute_engine
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
Enter passphrase for key '/Users/userz/.ssh/google_compute_engine':
server2
输出:
ssh -v server2
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/userz/.ssh/config
debug1: /Users/userz/.ssh/config line 43: Applying options for server2
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to YY.YYY.YYY.YYY [YY.YYY.YYY.YYY] port 22.
debug1: Connection established.
debug1: identity file /Users/userz/.ssh/google_compute_engine type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/userz/.ssh/google_compute_engine-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.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to YY.YYY.YYY.YYY:22 as 'userz'
debug1: using hostkeyalias: compute.hostkeyaliasY
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: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
debug1: using hostkeyalias: compute.hostkeyaliasY
debug1: Host 'compute.hostkeyaliasY' is known and matches the ECDSA host key.
debug1: Found key in /Users/userz/.ssh/google_compute_known_hosts:3
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,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/userz/.ssh/google_compute_engine
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: keyboard-interactive
Password:
答案1
在执行以下操作之前,请备份您的~/.ssh/config
文件。否则,您可能会遇到麻烦。
这是通过删除配置来解决gcloud
的
gcloud compute config-ssh --remove
之后重新安装配置,运行与问题中所述相同的命令:
gcloud compute config-ssh
这会将实例的别名添加到用户 SSH 配置 (~/.ssh/config) 文件并更新项目 SSH 元数据。
答案2
经过这,我修复了这个问题:
export PROB_INSTANCE='your-instance-name'
gcloud compute ssh another-user@$PROB_INSTANCE
~/.ssh/authorized_keys
因为我之前尝试通过 SSH 登录的用户的文件权限设置不正确。
但是,YMMV,这只是对我有用的。
答案3
添加服务帐户用户角色(roles/iam.serviceAccountUser
)分配给我的用户帐户(或者如果我使用服务帐户登录,则分配给我的服务帐户)为我解决了整个问题。
看步骤 4 点2在https://cloud.google.com/compute/docs/instances/managing-instance-access#configure_users
无需写入/删除/重写~/.ssh
值或任何其他内容