如何将我自己的公钥添加到 Vagrant VM? [英] How do I add my own public key to Vagrant VM?
问题描述
我在向 Vagrant VM 添加 ssh 密钥时遇到问题.基本上我在这里的设置工作正常.创建 VM 后,我可以通过 vagrant ssh
访问它们,用户vagrant"存在并且 authorized_keys
文件中有此用户的 ssh 密钥.
I got a problem with adding an ssh key to a Vagrant VM. Basically the setup that I have here works fine. Once the VMs are created, I can access them via vagrant ssh
, the user "vagrant" exists and there's an ssh key for this user in the authorized_keys
file.
我现在想做的是:能够通过 ssh
或使用 scp
连接到这些虚拟机.所以我只需要将我的公钥从 id_rsa.pub
添加到 authorized_keys
- 就像我对 ssh-copy-id
.
What I'd like to do now is: to be able to connect to those VMs via ssh
or use scp
. So I would only need to add my public key from id_rsa.pub
to the authorized_keys
- just like I'd do with ssh-copy-id
.
有没有办法在设置过程中告诉 Vagrant 应该包含我的公钥?如果没有(根据我的谷歌结果,这很可能),有没有办法在 vagrant 设置期间轻松附加我的公钥?
Is there a way to tell Vagrant during the setup that my public key should be included? If not (which is likely, according to my google results), is there a way to easily append my public key during the vagrant setup?
推荐答案
复制所需的公钥将完全属于 供应 阶段.确切的答案取决于您喜欢使用的配置(shell、Chef、Puppet 等).最简单的是密钥的 file
配置器,如下所示:
Copying the desired public key would fall squarely into the provisioning phase. The exact answer depends on what provisioning you fancy to use (shell, Chef, Puppet etc). The most trivial would be a file
provisioner for the key, something along this:
config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/me.pub"
好吧,实际上您需要附加到authorized_keys.使用 shell 配置器,如下所示:
Well, actually you need to append to authorized_keys. Use the the shell provisioner, like so:
config.vm.provision "shell", inline: <<-SHELL
cat /home/vagrant/.ssh/me.pub >> /home/vagrant/.ssh/authorized_keys
SHELL
end
您也可以使用真正的供应商,例如 Puppet.例如,请参阅使用 Puppet 管理 SSH 授权密钥.
You can also use a true provisioner, like Puppet. For example see Managing SSH Authorized Keys with Puppet.
这篇关于如何将我自己的公钥添加到 Vagrant VM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!