alexkras.com

  • Home
  • Top Posts
  • Resume
  • Projects
  • YouTube
  • Boots to Bytes
  • About
  • Contact

How to Copy One File from Vagrant Virtual Machine to Local Host

February 10, 2017 by Alex Kras 1 Comment

Step 1.

Find what port Vagrant is connected to by running vagrant port

The output Will look as follow:

The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2200 (host)

Indicating that Vagrant is running on port 2200

Step 2.

Get the absolute path for the file that you want to copy i.e. /vagrant/some-file.txt

You can get it by ssh’ing into the machine and locating the needed file

Step 3.

Make sure you are in your host machine and use SCP command to copy over the file

scp -P 2200 vagrant@127.0.0.1:/vagrant/some-file.txt .

Note that:

  • -P points to port from step 1
  • username is vagrant
  • Your password, is most likely vagrant as well
  • IP is 127.0.0.1 which is just your localhost
  • There is a : after the IP and before the path to the file
  • There is a space and . after the path of the file, indicating that you want to save the file into the current directory.

You can use the same trick to ssh into vagrant the machine (instead of the built in vagrant ssh command) by running ssh -p 2200 vagrant@127.0.0.1 with he only difference being that -p for port is lower case.

Filed Under: Tools

I work for Evernote and we are hiring!

Subscribe to this Blog via Email

New posts only. No other messages will be sent.

You can find me on LinkedIn, GitHub, Twitter or Facebook.

This blog is moving to techtldr.com

Comments

    Leave a Reply Cancel reply

  1. TangRufus says

    September 11, 2017 at 11:41 am

    This will fail if you try to scp files on the remote machine that are not accessible by the vagrant user.

    I make vagrant-sudo-rsync to use sudo

    Reply

Copyright © 2021 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in