I have a use case where I occasionally want to copy a single file from my host machine to the Vagrant guest.
I don\'t want to do so via traditional provisioners (Pup
An alternative way to do this without installing anything (vagrant-scp
etc.) Note that the name default
needs to be used as is, since vagrant ssh-config
emits that.
vg_scp() {
tmpfile=$(mktemp /tmp/vagrant-ssh-config.XXXX)
vagrant ssh-config > $tmpfile
scp -F $tmpfile "$@"
rm $tmpfile
}
# Copy from local to remote
vg_scp somefile default:/tmp
# Copy from remote to local
vg_scp default:/tmp/somefile ./
# Copy a directory from remote to local
vg_scp -r default:/tmp ./tmp
The function would not be necessary if scp -F =(vagrant ssh-config) ...
would have worked across shells. But since this is not supported by Bash, we have to resort to this workaround.
Vagrant provides a way to execute a command over ssh instead of logging in, so for Linux host and guest you can use:
cat ~/file_on_host.txt | vagrant ssh -c 'cat - > ~/file_on_guest.txt'
vagrant ssh -c 'cat ~/file_on_guest.txt' > ~/file_on_host.txt
No need for plugins or guest reloads. Just make sure to provide vagrant box ID to 'vagrant ssh' if you are not in the same dir as the Vagrantfile. Tested on Vagrant v1.8.1.
Here is my approach to the problem:
Step 1 - Find the private key, ssh port and IP:
root@vivi:/opt/boxes/jessie# vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /root/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
Step 2 - Transfer file using the port and private key as parameters for scp:
scp -P 2222 -i /root/.vagrant.d/insecure_private_key \
someFileName.txt vagrant@127.0.0.1:~
I hope it helps,
Best way to copy file from local to vagrant, No need to write any code or any thing or any configuration changes. 1- First up the vagrant (vagrant up) 2- open cygwin 3- cygwin : go to your folder where is vagrantfile or from where you launch the vagrant 4- ssh vagrant 5- now it will work like a normal system.
The best ans for me is to write the file / directory(to be copied) to the vagrant file directory, now any file present there is available to vagrant in path /vagrant.
That's it, no need of scp or any other methods,
similarly you can copy any file from VM to host by pasting in /vagrant directory.
You can add entry in ~/.ssh/config
:
Host vagrant
User vagrant
HostName localhost
Port 2222
IdentityFile /home/user_name/.vagrant.d/insecure_private_key
and the simplescp file vagrant:/path/
. You can find path to identity file using the vagrant ssh-config
command.