Copying Files from a VM to Your Local Machine

Contents

Introduction

This post covers the steps from checking the SSH key to transferring files with scp.

Checking the SSH key (preparation)

Run the following in the directory that contains the Vagrantfile.

vagrant ssh-config

You get output like this 👇

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  IdentityFile "C:/path/to/project/.vagrant/machines/default/virtualbox/private_key"

Check these three items.

Item What it is for
HostName SSH destination → 127.0.0.1
Port Port in use → 2222
IdentityFile Path to the private key that SSH / SCP need

Transferring files with scp (the actual step)

Run the following command.

scp -P <port> -i <path-to-private-key> vagrant@<host>:<path-of-file-to-copy-in-VM> <where-to-put-the-copied-file>

Example:

scp -P 2222 -i C:/path/to/project/.vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1:/home/vagrant/sample.txt C:/Users/username/Desktop/

Common errors and fixes

Error Cause Fix
Permission denied (publickey) The private key is not specified Add -i <private_key>
No such file or directory The file path is wrong Check with ls inside the VM

Translated from the Japanese original.

Thanks. I'll look into it.

Spotted a mistake?

Tell me what's wrong and I'll fix it. Only I see what you send.