2017-09-25 180 views
0

問題出在subconfig1.vm.provision ...上,我應該如何繼續?初始化VM的Vagrant後,告訴我SSH工作正常,因爲連接被拒絕。我可以完成這項工作,但只能手動插入密碼,這裏我最大的問題是如何超越密碼來自動完成此工作流程。將文件從一臺虛擬機傳輸到另一臺虛擬機

# -*- 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 [email protected] uptime 

     cp /vagrant/.vagrant/machines/Machine2/virtualbox/private_key ~/.ssh 
     scp -P 2222 -i ~/.ssh/private_key [email protected]:/home/vagrant/vagrantvm1-info /home/vagrant 

    # ssh [email protected] 
    # scp [email protected]:/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 

回答

0

您需要傳遞私鑰才能成功登錄。

  1. 檢索的私鑰用於

一般來說,文件是.vagrant/machines/<machine_name>/provider/private_key

下也可以運行vagrant ssh-config找出哪個私鑰被用於所有的機器:

[email protected]:/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. 複製VM中的密鑰。

由於您要將副本虛擬機運行到虛擬機,因此原始虛擬機需要知道私鑰才能連接到目標虛擬機。

使用文件配置器或shell腳本中的副本文件將用於登錄到目標虛擬機的私鑰複製到原始虛擬機中。

  • SCP使用私鑰 - 見scp man page

    scp -i ~/.ssh/private_key \ 
    /home/vagrant/vagrantvm1-info \ 
    [email protected]:/home/vagrant 
    
  • 更新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 [email protected] 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 [email protected]:/home/vagrant 
    
        # ssh [email protected] 
        # scp [email protected]:/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 
    
    +0

    我修改了你告訴我的方式,現在唯一的問題是它沒有找到該文件:文件提供者: *文件上傳源文件C:/Users/Cristian/.vagrant/mac hines/Machine2/virtualbox/private_key必須存在 – Cristian

    +0

    只需要'config.vm.provision「文件」,source:「.vagrant/machines/Machine2/virtualbox/private_key」,destination:「/ home/ssh」';並確保'private_key'存在於.vagrant/machines/Machine2/virtualbox/ –

    +0

    如果我將此行標記出來並運行代碼,它可以工作,我使用vagrant ssh-configure,並且我可以在文件夾中看到私鑰,我甚至可以用帽子來裝飾它。但無論我將文件供應器放在vagrantfile的代碼中,我都無法訪問它,vagrant說該文件不存在。 – Cristian

    相關問題