[英]How to copy file from a Vagrant machine to localhost
I want to copy a local file from a Vagrant machine to my localhost
, but I am getting an error message:我想将一个本地文件从 Vagrant 机器复制到我的localhost
,但我收到一条错误消息:
ssh: connect to host
127.0.0.1
port22
: Connection refused. ssh:连接到主机127.0.0.1
端口22
:连接被拒绝。
[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.我也尝试使用localhost
但仍然遇到相同的错误。
Another option is cat
the files to something local: 另一种选择是cat
的文件到本地的东西:
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). 这也适用于需要root权限的文件(流浪的SCP插件似乎不支持的东西)。
You should read the manual page for scp
. 您应该阅读scp
的手册页。 The correct syntax is: 正确的语法是:
scp -P 2222 vagrant@127.0.0.1:/home/vagrant/devstack/local.conf .
The uppercase P is for "port". 大写P代表“端口”。 Lowercase is used to preserve modification times. 小写用于保留修改时间。
Get IdentityFile and Port by using 使用获取IdentityFile和Port
vagrant ssh-config
scp -i IdentityFile_file -P Port vagrant@127.0.0.1:/file_dir dist_dir
eg scp -i IdentityFile_file -P Port vagrant@127.0.0.1:/file_dir dist_dir
eg
scp -i /Users/xxxxx/tmp/vagrant/centos_6.5/.vagrant/machines/default/virtualbox/private_key -P 2200 vagrant@127.0.0.1:/tmp/xxx .
对于通过Google进入的任何人来说,这都是一个方便的工具: Vagrant SCP
As @SevenJ mentioned, ssh-config can provide all the info you need. 正如@SevenJ所提到的,ssh-config可以提供您需要的所有信息。 But it's a lot easier to save it to a file and use that file, rather than constructing a complicated scp command. 但是将它保存到文件并使用该文件要容易得多,而不是构建复杂的scp命令。 Eg: 例如:
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". 在这里,我假设你的vagrant文件没有覆盖“default”的机器名。 If it does, replace "default:" with ":". 如果是,请将“default:”替换为“:”。
Another option like Dan linked to:另一个选项,如 Dan 链接到:
vagrant plugin install vagrant-scp
vagrant scp [vm_name]:<remote_path> <local_path
and if you need to do the opposite its如果你需要做相反的事情
vagrant scp <local_path> [vm_name]:<remote_path>
You can use vagrant status
to get the name of the VM您可以使用vagrant status
来获取 VM 的名称
Current machine states:
default running (virtualbox)
Where default is the name of the VM in my case.在我的例子中,default 是虚拟机的名称。
Additional tools like scp or cat may not be necessary.可能不需要其他工具,如 scp 或 cat。 Frederick Henri covered it here .弗雷德里克·亨利 (Frederick Henri) 在这里进行了介绍。
Essentially, cp [file] /var/www/[your vm]/.vagrant
will copy the file to the .vagrant folder at your project root, where you can see and move the file in your desktop OS.本质上, cp [file] /var/www/[your vm]/.vagrant
会将文件复制到项目根目录下的 .vagrant 文件夹中,您可以在其中查看和移动桌面操作系统中的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.