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

前端 未结 19 1618
暖寄归人
暖寄归人 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:26

    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.

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

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

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

    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

    0 讨论(0)
  • 2020-12-22 15:30
    vagrant upload localfile
    

    that will put localfile in the vagrant user's home dir

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

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

    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.

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