English 中文(简体)
Easiest way to copy a single file from host to Vagrant guest?
原标题:
  • 时间:2013-05-23 01:07:08
  •  标签:
  • vagrant

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?

最佳回答

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
问题回答

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>

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."

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 vagrant@127.0.0.1:.

You can also copy files back to your local host using the following command:

scp -P 2222 vagrant@127.0.0.1:/PATH/filename
vagrant upload localfile

that will put localfile in the vagrant user s home dir

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

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 vagrant@127.0.0.1:~

I hope it helps,

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

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.

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 provides a way to execute a command over ssh instead of logging in, so for Linux host and guest you can use:

  • from host to guest:

cat ~/file_on_host.txt | vagrant ssh -c cat - > ~/file_on_guest.txt

  • from guest to host:

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.

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.

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

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

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"/

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.

Try this.. vagrant ubuntu 14.04 This worked for me.

scp -r -P 2222 vagrant@localhost:/home .

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.

If you "cd .." enough times you will find the vagrant folder which contains all your host files and folder

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.





相关问题
Easiest way to copy a single file from host to Vagrant guest?

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 ...

Importing Mysql database using Ruby/Chef Recipe for Vagrant

I am writing a chef script to automate setting dev environments. I can get a database created and grant privileges but I am trying to find out a way to import a mysql dump file into the database that ...

Problem installing PEAR with a chef recipe for vagrant

I am using Vagrant to create a development server locally. I am writing my own Chef recipe to install everything I need but I am running into problems. Pear will not install as I think it is trying ...

Vagrant s port forwarding not working [closed]

I m running into a small problem at the end of the Getting Started guide for vagrant. I m working on a CentOS basebox that has Apache2 running (provisioning via Puppet). I ve set up port forwarding ...

Installing multiple packages via Vagrant + Chef

I ve just discovered Vagrant + Chef and I m trying to create a simple recipe to install multiple packages on the node. I thought something like this could work (I m completely new tu ruby): # (From ...

Autotest notifications on Ubuntu virtual environment

I am having trouble getting Rails autotest notifications to work on the Engine Yard Vagrant environment. On the Mac, I normally get the notifications via Growl. However, on the virtual environment (...

Setting up Vagrant to mirror a heroku instance

I am ramping up on vagrant and was wondering if there was a walkthrough / recipe to get a vagrant instance up and running to mirror a heroku bamboo stack (lenny 5.0 ree-1.8.7) I am stuck on the first ...

热门标签