环境:
VM A : 我想在没有密码的情况下访问的机器
VM B : 我想从机器上访问没有密码的 VM A
问题描述:
由于我想在没有密码的情况下从 VM B 访问 VM A,我想首先将 VM B 的公钥注入 VM A,但如果我使用 ssh-copy,我仍然需要密码,所以我尝试通过挂载注入文件。
步骤:
- 从 LiveCD 启动 VM A
- 挂载VM A的根盘(包含操作系统)
- 在 ${mount_point}/root/.ssh/ 下创建一个 authorized_keys 文件
- 将 VM B 的公钥放入 authorized_keys 文件中
- 停止 VM A 并移除 LiveCD,然后再次启动 VM A
通过命令从 VM B 访问 VM A
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no root@9.112.224.130
结果:
debug2: we did not send a packet, disable method
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug3: no such identity: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug3: Wrote 368 bytes for a total of 1645
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /root/.ssh/id_dsa
debug3: no such identity: /root/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
但是在我执行了以下命令之后,它就可以工作了(文件模式都是 600 并且与 diff 命令没有区别)
mv authorized_keys authorized_keys_bak
cp authorized_keys_bak authorized_keys
所以我尝试再次注入它,但是这次,我在从LiveCD启动之前创建了一个空的authorized_keys文件,并跳过了第3步,其他步骤都一样,这次VM B可以在没有密码的情况下访问VM A
问题:
authorized_keys 和 authorized_keys_bak 的属性都是一样的,为什么一个有效,一个无效?
我的authorized_keys 只能在我创建文件而不是从LiveCD 挂载时工作?