问题
I want to copy a local file from a Vagrant machine to my local host, but I am getting an error message:
ssh: connect to host 127.0.0.1 port 22: Connection refused
[user@localhost ceil]$ scp -p 2222 vagrant@127.0.0.1:/home/vagrant/devstack/local.conf .
cp: cannot stat ‘2222’: No such file or directory
ssh: connect to host 127.0.0.1 port 22: Connection refused
I also tried using using localhost
but still got the same error.
回答1:
You should read the manual page for scp
. The correct syntax is:
scp -P 2222 vagrant@127.0.0.1:/home/vagrant/devstack/local.conf .
The uppercase P is for "port". Lowercase is used to preserve modification times.
回答2:
Another option is cat
the files to something local:
vagrant ssh -c "sudo cat /home/vagrant/devstack/local.conf" > local.conf
This should also work for files that require root permissions (something the vagrant SCP plugin doesn't seem to support).
回答3:
Get IdentityFile and Port by using
vagrant ssh-config
scp -i IdentityFile_file -P Port vagrant@127.0.0.1:/file_dir dist_dir
e.g.
scp -i /Users/xxxxx/tmp/vagrant/centos_6.5/.vagrant/machines/default/virtualbox/private_key -P 2200 vagrant@127.0.0.1:/tmp/xxx .
回答4:
This is a handy tool for anyone coming in via Google: Vagrant SCP
回答5:
As @SevenJ mentioned, ssh-config can provide all the info you need. But it's a lot easier to save it to a file and use that file, rather than constructing a complicated scp command. E.g.:
vagrant ssh-config > config.txt
scp -F config.txt default:/path/to/file .
Here I'm assuming your vagrant file doesn't override the machine name from "default". If it does, replace "default:" with ":".
来源:https://stackoverflow.com/questions/32410299/how-to-copy-file-from-a-vagrant-machine-to-local-host