Skip to content

Instantly share code, notes, and snippets.

@colindean
Last active March 27, 2025 18:19
Show Gist options
  • Select an option

  • Save colindean/5213685 to your computer and use it in GitHub Desktop.

Select an option

Save colindean/5213685 to your computer and use it in GitHub Desktop.
A quick way to transfer a file to the home directory on a Vagrant VM
#!/bin/sh
OPTIONS=`vagrant ssh-config | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`
scp ${OPTIONS} "$@" || echo "Transfer failed. Did you use 'default:' as the target?"
#!/bin/bash
PORT=2222
if [ ${VAGPORT} ]; then
PORT=${VAGPORT}
fi
VAGRANT_GEM=$(dirname `gem which vagrant`)/..
VAGRANT_KEY=${VAGRANT_GEM}/keys/vagrant
chmod 600 ${VAGRANT_KEY}
ssh -i ${VAGRANT_KEY} -p ${PORT} vagrant@localhost
#need to test this:
#OPTIONS=`vagrant ssh-config | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`;
#ssh ${OPTIONS} localhost
@spyderman4g63

Copy link
Copy Markdown

I couldn't figure out how to use the scp script but I did find this answer about how to transfer files to a vagrant instance: "please note that unless you specifically want scp for some reason, the easiest way to transfer files from the host to the VM is to just put them in the same directory as the Vagrantfile - that directory is automatically mounted under /vagrant in the VM so you can copy or use them directly from the VM."

@geedew

geedew commented Apr 24, 2014

Copy link
Copy Markdown
scp ${OPTIONS} FILES_TO_PUSH USER@SERVER:/PLACE_TO_PUT_EM

works.

@Camalot9

Camalot9 commented Jun 5, 2014

Copy link
Copy Markdown

Thanks @spyderman4g63 that is the easiest solution ever.

@sboday

sboday commented Apr 2, 2015

Copy link
Copy Markdown

@colindean Thanks for the script.
usage: ./vagrant-scp.sh myfile.txt default:

@beneze

beneze commented Jun 18, 2015

Copy link
Copy Markdown

Thanks @spyderman4g63. the shared vagrant folder was all I needed

@djadmin

djadmin commented Jul 19, 2015

Copy link
Copy Markdown

Thank you @spyderman4g63. It works like a charm. Most easiest solution :)

@stuartc

stuartc commented Sep 3, 2015

Copy link
Copy Markdown

@colindean thanks for this. really useful! learn something new every day!

I added NF to skip blank lines, not sure if would be a problem in the long run but you can avoid a trailing -o = %

... IdentitiesOnly=yes -o LogLevel=FATAL -o = %

Also works nicely with rsync:

OPTIONS=`vagrant ssh-config | awk -v ORS=' ' 'NF {print "-o " $1 "=" $2}'`
rsync -avz -e "ssh ${OPTIONS}" default:/tmp/test.txt ~/Downloads/

@dominictobias

Copy link
Copy Markdown

Thanks @geedew easiest answer I've found

@nkuclassical

Copy link
Copy Markdown

Thanks @spyderman4g63 extremely easy way!

@pxwise

pxwise commented May 10, 2016

Copy link
Copy Markdown

@spyderman4g63 👍 thanks!!

@compositor

Copy link
Copy Markdown

I get Host directive not supported as a command-line option error.
Solution is here https://gist.github.com/geedew/11289350

OPTIONS=`vagrant ssh-config | grep -v '^Host ' | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`

@tangrufus

Copy link
Copy Markdown

For those who need sudo privilege, try $ vagrant sudo-rsync.

See: https://github.com/TypistTech/vagrant-sudo-rsync

@silopolis

silopolis commented Mar 27, 2025

Copy link
Copy Markdown

For some reason, while using the vagrant-libvirt docker container with the recommended shell function, I had to tweak this a bit:

OPTIONS="$(vagrant-libvirt ssh-config | tr -d '\r' | tail -n +2 | head -n -1 | awk -v ORS=' ' '{print "-o " $1 "=" $2}')"

Anyway, this trick saved the day as vagrant-scp plugin isn't easily installable in the container.
Thanks a ton for sharing
J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment