将文件从一个VM传输到另一个VM

时间:2017-09-25 04:26:02

标签: ruby vagrant

问题在于subconfig1.vm.provision ...,有关如何继续的任何建议?在初始化VM的Vagrant之后告诉我SSH faild来完成这项工作,因为连接被拒绝了。我可以完成这项工作,但只能手动插入密码,我的重要问题是如何覆盖密码以自动生成此工作流程。

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.define "Machine2" do |subconfig2|

    subconfig2.vm.box = "ubuntu/trusty64"
    subconfig2.vm.hostname = "Machine2"
    subconfig2.vm.network :private_network, ip: "172.16.10.101"
    subconfig2.vm.network "forwarded_port", guest: 80, host: 4445, host_ip: "127.0.0.1"

  end

   #config.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant/ssh"


  config.vm.define "Machine1" do |subconfig1|
    subconfig1.vm.box = "ubuntu/trusty64"
    subconfig1.vm.hostname = "Machine1"
    subconfig1.vm.network :private_network, ip: "172.16.10.100"

   # subconfig1.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant"

    subconfig1.vm.provision "shell", privileged: false, inline: <<-SHELL
      touch vagrantvm1-info
      ifconfig | grep "HWaddr" | cut -b 32-55 >> vagrantvm1-info
    #  ssh -o StrictHostKeyChecking=no vagrant@127.0.0.1 uptime

      cp /vagrant/.vagrant/machines/Machine2/virtualbox/private_key ~/.ssh
      scp -P 2222 -i ~/.ssh/private_key vagrant@127.0.0.1:/home/vagrant/vagrantvm1-info /home/vagrant

    #  ssh vagrant@172.16.10.101
    #  scp vagrant@172.16.10.101:/home/vagrant/vagrantvm1-info /home/vagrant

   SHELL
  end




  config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
   vb.gui = true
# Customize the amount of memory on the VM:
   vb.memory = "1024"
  end

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    apt-get install -y avahi-daemon libnss-mdns
    apt-get update
    apt-get install -y apache2
  SHELL

end

1 个答案:

答案 0 :(得分:0)

您需要传递私钥才能成功登录。

  1. 检索使用了哪个私钥
  2. 通常,该文件位于.vagrant/machines/<machine_name>/provider/private_key

    您还可以运行vagrant ssh-config以找出所有计算机使用的私钥:

    fhenri@machine:/Volumes/VM/vagrant/golang (master)$ vagrant ssh-config
    Host default
      HostName 127.0.0.1
      User vagrant
      Port 2222
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile /Volumes/VM/vagrant/golang/.vagrant/machines/default/virtualbox/private_key
      IdentitiesOnly yes
      LogLevel FATAL
    

    在这种情况下,私钥为/Volumes/VM/vagrant/golang/.vagrant/machines/default/virtualbox/private_key

    1. 复制虚拟机中的密钥。
    2. 由于您要将复制VM运行到VM,因此源VM需要知道私钥,以便它将连接到目标VM。

      使用文件配置程序或shell脚本中的副本文件将用于登录目标VM的私钥复制到源VM中。

      1. 使用私钥scp - 请参阅scp man page

        scp -i ~/.ssh/private_key \
        /home/vagrant/vagrantvm1-info \
        vagrant@172.16.10.101:/home/vagrant
        
      2. 我用来测试的更新的Vagrantfile是以下

        # -*- mode: ruby -*-
        # vi: set ft=ruby :
        
        Vagrant.configure("2") do |config|
        
          config.vm.define "Machine2" do |subconfig2|
        
            subconfig2.vm.box = "ubuntu/trusty64"
            subconfig2.vm.hostname = "Machine2"
            subconfig2.vm.network :private_network, ip: "172.16.10.101"
            subconfig2.vm.network "forwarded_port", guest: 80, host: 4445, host_ip: "127.0.0.1"
        
          end
        
           #config.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant/ssh"
        
        
          config.vm.define "Machine1" do |subconfig1|
            subconfig1.vm.box = "ubuntu/trusty64"
            subconfig1.vm.hostname = "Machine1"
            subconfig1.vm.network :private_network, ip: "172.16.10.100"
        
           # subconfig1.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant"
        
            subconfig1.vm.provision "shell", privileged: false, inline: <<-SHELL
              touch vagrantvm1-info
              ifconfig | grep "HWaddr" | cut -b 32-55 >> vagrantvm1-info
            #  ssh -o StrictHostKeyChecking=no vagrant@127.0.0.1 uptime
        
              cp /vagrant/.vagrant/machines/Machine2/virtualbox/private_key ~/.ssh && chmod 0644 ~/.ss/private_key
              # make sure to accept the server keys
              ssh-keyscan 172.16.10.101 >> ~/.ssh/known_hosts
              scp -i ~/.ssh/private_key /home/vagrant/vagrantvm1-info vagrant@172.16.10.101:/home/vagrant
        
            #  ssh vagrant@172.16.10.101
            #  scp vagrant@172.16.10.101:/home/vagrant/vagrantvm1-info /home/vagrant
        
           SHELL
          end
        
        
        
        
          config.vm.provider "virtualbox" do |vb|
        # Display the VirtualBox GUI when booting the machine
           #vb.gui = true
        # Customize the amount of memory on the VM:
           vb.memory = "1024"
          end
        
          config.vm.provision "shell", inline: <<-SHELL
            apt-get install -y avahi-daemon libnss-mdns
            apt-get update
            apt-get install -y apache2
          SHELL
        
        end