Reputation: 3705
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 (Puppet / Chef) because this is often a one-off -- I just want something quick to add to my Vagrantfile.
I don't want to share an entire directory, possibly because I want to overwrite an existing file without nuking an entire directory on the guest.
It also seems a bit overkill to write a shell provisioning script, and deal with potential escaping, when all I want to do is copy a file.
So, what's the easiest way to copy a single file from host to guest?
Upvotes: 317
Views: 233710
Reputation: 45353
Introducing a method that does not require installing any plugins and eliminates the need to provide the private key (which can easily be forgotten):
By default, the first Vagrant instance uses port 2222 for SSH and its IP address is 127.0.0.1. (Please adjust the port if you have created multiple virtual hosts.)
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
With this setup, you can use the following command to copy your local file to the Vagrant instance in any desired path, not just the /vagrant
folder. The password is the same as the username, which is vagrant
scp -P 2222 your_file [email protected]:.
You can also copy files back to your local host using the following command:
scp -P 2222 [email protected]:/PATH/filename
Upvotes: 113
Reputation: 11
If you "cd .." enough times you will find the vagrant folder which contains all your host files and folder
Upvotes: 1
Reputation: 4062
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
Upvotes: 13
Reputation: 108
The best ans for me is to write the file / directory(to be copied) to the vagrant file directory, now any file present there is available to vagrant in path /vagrant.
That's it, no need of scp or any other methods,
similarly you can copy any file from VM to host by pasting in /vagrant directory.
Upvotes: 0
Reputation: 3587
vagrant upload localfile
that will put localfile in the vagrant user's home dir
https://www.vagrantup.com/docs/cli/upload.html
Upvotes: 79
Reputation: 175
If someone wants to transfer file from windows host to vagrant, then this solution worked for me.
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.
You should be able to see directories in your vagrant machine.
Upvotes: 2
Reputation: 2817
An alternative way to do this without installing anything (vagrant-scp
etc.) Note that the name default
needs to be used as is, since vagrant ssh-config
emits that.
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
The function would not be necessary if scp -F =(vagrant ssh-config) ...
would have worked across shells. But since this is not supported by Bash, we have to resort to this workaround.
Upvotes: 2
Reputation: 351
Try this.. vagrant ubuntu 14.04 This worked for me.
scp -r -P 2222 vagrant@localhost:/home .
Upvotes: 1
Reputation: 92
Best way to copy file from local to vagrant, No need to write any code or any thing or any configuration changes. 1- First up the vagrant (vagrant up) 2- open cygwin 3- cygwin : go to your folder where is vagrantfile or from where you launch the vagrant 4- ssh vagrant 5- now it will work like a normal system.
Upvotes: -3
Reputation: 121
Vagrant provides a way to execute a command over ssh instead of logging in, so for Linux host and guest you can use:
cat ~/file_on_host.txt | vagrant ssh -c 'cat - > ~/file_on_guest.txt'
vagrant ssh -c 'cat ~/file_on_guest.txt' > ~/file_on_host.txt
No need for plugins or guest reloads. Just make sure to provide vagrant box ID to 'vagrant ssh' if you are not in the same dir as the Vagrantfile. Tested on Vagrant v1.8.1.
Upvotes: 12
Reputation: 1165
if for some reasons you don't have permission to use
vagrant plugin install vagrant-scp
there is an alternative way :
First vagrant up yourVagrantProject, then write in the terminal :
vagrant ssh-config
you will have informations about "HostName" and "Port" of your virtual machine.
In some case, you could have some virtual machines in your project. So just find your master-machine (in general, this VM has the port 2222 ), and don't pay attention to others machines informations.
write the command to make the copy :
scp -P xxPortxx /Users/where/is/your/file.txt vagrant@xxHostNamexx:/home/vagrant
At this steep you will have to put a vagrant password : by default it's "vagrant"
after that if you look at files in your virtual machine:
vagrant ssh xxVirtualMachineNamexx
pwd
ls
you will have the "file.txt" in your virtual machine directory
Upvotes: 8
Reputation: 3147
Go to the directory where you have your Vagrantfile
Then, edit your Vagrantfile
and add the following:
config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=774','fmode=775']
"." means the directory you are currently in on your host machine
"/vagrant" refers to "/home/vagrant" on the guest machine(Vagrant machine).
Copy the files you need to send to guest machine to the folder where you have your Vagrantfile
Then open Git Bash and cd
to the directory where you have your Vagrantfile
and type:
vagrant scp config.json XXXXXXX:/home/vagrant/
where XXXXXXX is your vm name. You can get your vm name by running
vagrant global-status
Upvotes: 8
Reputation: 311
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"/
Upvotes: 5
Reputation: 1660
Instead of using a shell provisioner to copy the file, you can also use a Vagrant file provisioner.
Provisioner name:
"file"
The file provisioner allows you to upload a file from the host machine to the guest machine.
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end
Upvotes: 144
Reputation: 6607
Since you ask for the easiest way, I suggest using vagrant-scp. It adds a scp command to vagrant, so you can copy files to your VM like you would normally do with scp.
Install via:
vagrant plugin install vagrant-scp
Use it like so:
vagrant scp <some_local_file_or_dir> [vm_name]:<somewhere_on_the_vm>
Upvotes: 309
Reputation: 3420
Here is my approach to the problem:
Step 1 - Find the private key, ssh port and 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
Step 2 - Transfer file using the port and private key as parameters for scp:
scp -P 2222 -i /root/.vagrant.d/insecure_private_key \
someFileName.txt [email protected]:~
I hope it helps,
Upvotes: 65
Reputation: 107
You can add entry in ~/.ssh/config
:
Host vagrant
User vagrant
HostName localhost
Port 2222
IdentityFile /home/user_name/.vagrant.d/insecure_private_key
and the simplescp file vagrant:/path/
. You can find path to identity file using the vagrant ssh-config
command.
Upvotes: 9
Reputation: 1388
If you are restrained from having the files in your directory, you can run this code in a script file from the Host machine.
#!/bin/sh
OPTIONS=`vagrant ssh-config | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`
scp ${OPTIONS} /File/To/Copy vagrant@YourServer:/Where/To/Put/File
In this setup, you only need to change /File/To/Copy
to the file or files you want to copy and then /Where/To/Put/File
is the location on the VM you wish to have the files copied to.
If you create this file and call it copyToServer.sh
you can then run the sh
command to push those files.
sh ./copyToServer.sh
As a final note, you cannot run this code as a provisioner
because that runs on the Guest server, while this code runs from the Host.
Upvotes: 21
Reputation: 4667
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."
Upvotes: 173
Reputation: 3705
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
Upvotes: 27