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

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

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

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

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


当前回答

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

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

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

其他回答

vagrant upload localfile

这将把localfile放在流浪者用户的主目录

https://www.vagrantup.com/docs/cli/upload.html

如果有人想将文件从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-scp等)请注意,需要按原样使用名称default,因为vagrant ssh-config会发出该名称。

vg_scp() {
  tmpfile=$(mktemp /tmp/vagrant-ssh-config.XXXX)
  vagrant ssh-config > $tmpfile
  scp -F $tmpfile "$@"
  rm $tmpfile
}

# Copy from local to remote
vg_scp somefile default:/tmp

# Copy from remote to local
vg_scp default:/tmp/somefile ./

# Copy a directory from remote to local
vg_scp -r default:/tmp ./tmp

如果scp -F =(vagrant ssh-config)…会在壳层上起作用。但是由于Bash不支持这一点,所以我们必须采用这种解决方法。

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

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

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

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

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

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

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