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

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

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

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

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


当前回答

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

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

其他回答

最好的方式复制文件从本地到流浪,不需要写任何代码或任何东西或任何配置更改。 1-首先是流浪汉(流浪汉起来) 2-打开cygwin 3- cygwin:去你的文件夹是vagrantfile或从哪里启动vagrant 4- SSH流浪汉 5-现在它将像一个正常的系统。

您可以在~/.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命令查找身份文件的路径。

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

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

我希望这对你们有帮助,

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

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

转到Vagrantfile所在的目录 然后,编辑你的Vagrantfile并添加以下内容:

config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=774','fmode=775']

"."表示您在主机上当前所在的目录 “/vagrant”是指客机(vagrant机)上的“/home/vagrant”。

复制你需要发送到客户机的文件到你的Vagrantfile文件夹 然后打开Git Bash, cd到你的Vagrantfile所在的目录,输入:

vagrant scp config.json XXXXXXX:/home/vagrant/

其中XXXXXXX是您的虚拟机名称。您可以通过运行命令获取虚拟机名称

vagrant global-status