我有一个用例,我偶尔想从我的主机复制一个文件到Vagrant客户机。

我不想这样做通过传统的供应(木偶/厨师),因为这往往是一次性的-我只是想快速添加到我的Vagrantfile。

我不想共享整个目录,可能是因为我想覆盖现有文件,而不破坏客户机上的整个目录。

当我想要做的只是复制一个文件时,编写一个shell配置脚本并处理潜在的转义似乎有点过分。

那么,将单个文件从主机复制到客户机的最简单方法是什么?


当前回答

其实有一个更简单的解决方案。看到https://gist.github.com/colindean/5213685/评论- 882885:

"请注意,除非您出于某种原因特别想要scp,否则将文件从主机传输到VM的最简单方法是将它们放在与Vagrantfile相同的目录中-该目录会自动挂载在VM的/vagrant下,因此您可以直接从VM复制或使用它们。"

其他回答

如果有人想将文件从windows主机传输到vagrant,那么这个解决方案对我来说是有效的。

1. Make sure to install **winscp** on your windows system
2. run **vagrant up** command
3. run **vagrant ssh-config** command and note down below details
4. Enter Hostname, Port, Username: vagrant, Password: vagrant in winscp and select **SCP**, file protocol 
5. In most cases, hostname: 127.0.0.1, port: 2222, username: vagrant, password: vagrant.

您应该能够在您的流浪机器中看到目录。

除了使用shell提供程序复制文件外,还可以使用Vagrant文件提供程序。

提供者名称:“file” 文件提供程序允许您将文件从主机上传到客户计算机。

Vagrant.configure("2") do |config|
  # ... other configuration

  config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end

下面是我解决这个问题的方法:

步骤1 -找到私钥,ssh端口和IP:

root@vivi:/opt/boxes/jessie# vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /root/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

步骤2 -使用端口和私钥作为scp的参数传输文件:

  scp -P 2222 -i /root/.vagrant.d/insecure_private_key \
  someFileName.txt vagrant@127.0.0.1:~

我希望这对你们有帮助,

如果你“cd ..”足够多的时间,你会发现vagrant文件夹,其中包含你所有的主机文件和文件夹

既然您要求最简单的方法,我建议使用vagrant-scp。它向vagrant添加了一个scp命令,因此您可以像通常使用scp一样将文件复制到您的VM。

安装通过:

vagrant plugin install vagrant-scp

像这样使用它:

vagrant scp <some_local_file_or_dir> [vm_name]:<somewhere_on_the_vm>