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

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

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

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

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


当前回答

以上所有答案都可能管用。但下面对我来说是有效的。我有多个流浪主机:host1, host2。 我想从~/Desktop/file.sh复制文件到主机:host1 我做了:

    $vagrant upload ~/Desktop/file.sh host1

这将复制~/Desktop/file.sh在/home/xxxx下,其中xxx是你的流浪用户在host1下

其他回答

您可以在~/.ssh/config中添加条目:

Host vagrant
    User vagrant
    HostName localhost
    Port 2222
    IdentityFile /home/user_name/.vagrant.d/insecure_private_key

和simplescp文件vagrant:/path/。可以使用vagrant ssh-config命令查找身份文件的路径。

如果有人想将文件从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.

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

我最终做的是将文件保存在我的vagrant目录中(自动挂载为/vagrant/),并使用shell供应程序复制它:

command = "cp #{File.join('/vagrant/', path_within_repo)} #{remote_file}"
config.vm.provision :shell, :inline => command

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

步骤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:~

我希望这对你们有帮助,

对我来说最好的办法是把文件/目录(要复制)写到流浪文件目录,现在任何文件都可以在路径/vagrant中找到。

就是这样,不需要scp或任何其他方法,

类似地,您可以通过粘贴/vagrant目录从虚拟机复制任何文件到主机。