I have a use case where I occasionally want to copy a single file from my host machine to the Vagrant guest.
I don\'t want to do so via traditional provisioners (Pup
What I ended up doing was to keep the file within my vagrant directory (automatically mounted as /vagrant/) and copy it over with a shell provisioner:
command = "cp #{File.join('/vagrant/', path_within_repo)} #{remote_file}"
config.vm.provision :shell, :inline => command
There is actually a much simpler solution. See https://gist.github.com/colindean/5213685/#comment-882885:
"please note that unless you specifically want scp for some reason, the easiest way to transfer files from the host to the VM is to just put them in the same directory as the Vagrantfile - that directory is automatically mounted under /vagrant in the VM so you can copy or use them directly from the VM."
As default, the first vagrant instance use ssh port as 2222, and its ip address is 127.0.0.1 (You may need adjust the port with real virtual host)
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
So you can run below command to copy your local file to vagrant instance. password is the same as username which is vagrant
.
scp -P 2222 your_file vagrant@127.0.0.1:.
You can also copy the file back to your local host.
scp -P 2222 vagrant@127.0.0.1:/PATH/filename .
Try this.. vagrant ubuntu 14.04 This worked for me.
scp -r -P 2222 vagrant@localhost:/home .
All the above answers might work. But Below is what worked for me. I had multiple vagrant host: host1, host2. I wanted to copy file from ~/Desktop/file.sh to host: host1 I did:
$vagrant upload ~/Desktop/file.sh host1
This will copy ~/Desktop/file.sh under /home/xxxx where xxx is your vagrant user under host1
vagrant scp plugin works if you know your vagrant box name. check vagrant global-status which will provide your box name then you can run:
vagrant global-status
id name provider state directory
------------------------------------------------------------------------
13e680d **default** virtualbox running /home/user
vagrant scp ~/foobar "name in my case default":/home/"user"/