简体   繁体   English

如何将文件从 Vagrant 机器复制到本地主机

[英]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 port 22 : 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.

相关问题 如何将文件从本地无业游民复制到本地系统? - How to copy file from local vagrant to local system? 将文件从本地机器复制到远程机器 - Copy file from local machine to remote machine 如何将文件从远程Linux机器复制到JSP Web目录 - How to copy file from remote linux machine to JSP web directory 将文件复制到无所事事的远程环境 - copy file to a vagrant remote environment SCP 命令不起作用 - 需要将文件从 Windows localhost 复制到 Linux - SCP command not working - need to copy file from Windows localhost to Linux 使用staf将文件从linux复制到远程Windows机器 - Copy file from linux to remote windows machine using staf 如何将在无业游民的计算机中运行的Firefox转换为本地计算机? - How to forword firefox running in vagrant machine to local machine? 使用Java将文件从Windows计算机复制到Linux计算机的最佳方法 - Best way to copy a file from Windows machine to Linux machine using java 我该如何计划一个脚本,使用CRON和SCP每小时使用时间戳将文件从远程服务器复制到本地计算机? - How can I schedule a script to copy a file from a remote server to my local machine every hour with a time stamp using CRON & SCP? 如何通过扩展名将文件从本地计算机复制到Docker容器? - How to copy files by extension from Local Machine to Docker Container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM