How to copy file from a Vagrant machine to localhost

后端 未结 7 524
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 13:53

I want to copy a local file from a Vagrant machine to my localhost, but I am getting an error message:

ssh: connect to host 127.0.0.1

相关标签:
7条回答
  • 2021-01-30 14:01

    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 .
    
    0 讨论(0)
  • 2021-01-30 14:04

    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.

    0 讨论(0)
  • Additional tools like scp or cat may not be necessary. Frederick Henri covered it here.

    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.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-30 14:14

    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 ":".

    0 讨论(0)
  • 2021-01-30 14:14

    This is a handy tool for anyone coming in via Google: Vagrant SCP

    0 讨论(0)
提交回复
热议问题