Easiest way to copy a single file from host to Vagrant guest?

前端 未结 19 1604
暖寄归人
暖寄归人 2020-12-22 15:15

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

相关标签:
19条回答
  • 2020-12-22 15:16

    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
    
    0 讨论(0)
  • 2020-12-22 15:17

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

    0 讨论(0)
  • 2020-12-22 15:17

    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 .
    
    0 讨论(0)
  • 2020-12-22 15:19

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

    scp -r -P 2222 vagrant@localhost:/home .
    
    0 讨论(0)
  • 2020-12-22 15:20

    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

    0 讨论(0)
  • 2020-12-22 15:26

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

    0 讨论(0)
提交回复
热议问题