8

I am new with virtual machine. I want to copy a file from host (Mac) to vagrant virtual machine. I could not share a folder, so I want to use command line. How can I copy a file from host to vagrant virtual machine with command line?

aydinugur
  • 1,208
  • 2
  • 14
  • 21
Mehdi
  • 1,146
  • 1
  • 14
  • 27

3 Answers3

9

To copy a file from the host to vagrant:

scp -P 2222 /path/to/file vagrant@127.0.0.1:.
  • By default The vagrant instance uses port 2222 and ip 127.0.0.1. The password for vagrant is 'vagrant'. The file will be copied to the home of vagrant.

To copy a file from vagrant to host machine:

scp -P 2222 vagrant@127.0.0.1:/path/to/file
aprasanth
  • 1,079
  • 7
  • 20
  • 1
    This is outdated. Ports and IP addresses aren't generally valid. I recommend @gavenkoa's answer.If you want to use ssh, `vagrant ssh --no-tty -- cat '>' '$DST' < "$SRC"` doesn't require knowledge of ports, IP addresses, or passwords. – uncleremus Mar 25 '21 at 08:12
4

one easy way is to let vagrant handles the copy through the File Provisioner :

config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"

If you really want to use a command line, it might be dependant on your VM, assuming you have linux based VM, you could use scp and copy the file through ssh. see https://unix.stackexchange.com/a/106482

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
4

There is an officially supported command vagrant upload:

vagrant upload source [destination] [name|id]
gavenkoa
  • 45,285
  • 19
  • 251
  • 303