Copy files from a Virtual Box managed by Vagrant

Vagrant is a very useful tool for setting up a development environment. With vagrant, you can manage Virtual Box easily. However, if you have not set up the shared directory correctly, getting access to the file in the virutal box from the local host is tricky. Below is a solution using the scp command.

vagrant ssh-config

Which gives you the identity key used by vagrant as well as the port number of the virtual box.

  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /yourhome/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

That’s all you need to put into the scp command:

scp -i /yourhome/.vagrant/machines/default/virtualbox/private_key -p 2222 -r vagrant@ local_dir

Notice here after “-i” is the key file reported by “vagrant ssh-config” command. Similarly the port number 2222 also came from there.

To avoid type “-i” option in the future, one can use the command below to permanently add the private key to SSH configuration so that only the port number is used in the future.

ssh-add /yourhome/.vagrant/machines/default/virtualbox/private_key

Leave a Reply

Your email address will not be published.