0

我想在VirtualBox 5.1.22與Windows 7 SP1主機和CentOS 7.3來賓使用我自己的一對RSA SSH-2密鑰與Vagrant 1.9.5。無法配置authorized_keys文件到VM使用Vagrant

當我執行vagrant up我得到:

Waiting for machine to boot. This may take a few minutes... 
SSH address: 127.0.0.1:2222 
SSH username: vagrant 
SSH auth method: private key 
Warning: Connection aborted. Retrying... 
Warning: Connection reset. Retrying... 
Warning: Connection aborted. Retrying... 
Warning: Connection reset. Retrying... 
Warning: Connection aborted. Retrying... 
Warning: Connection reset. Retrying... 
Warning: Connection aborted. Retrying... 
... 

我發現,原因是失敗,因爲所需要的密鑰沒有被添加到的〜/ .ssh/authorized_keys中連接到客人,但它包含了流浪的默認insecure_private_key

這是我Vagrantfile

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

VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 

    config.vm.boot_timeout = 120 
    config.ssh.insert_key = false 
    config.ssh.private_key_path = ["vagrant-setup/keys/my_openssh.key"] 
    # This is not copying authorized_keys to the guest 
    config.vm.provision "file", source: "vagrant-setup/.ssh/authorized_keys", destination: "~/.ssh/autorized_keys" 
    # Setting forward_agent to true and adding the key to Pageant doesn't make any difference 
    config.ssh.forward_agent = false 

    config.vm.define "MyMachineName" do |vs| 

    vs.vm.box = "vagrant-centos-73-x86_64-puppet" 
    vs.vm.box_url = "https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.3/vagrant-centos-7.3.box" 

    # The shell script that will execute once just after the VM is created 
    vs.vm.provision "shell", path: "vagrant-setup/setup.sh" 

    # Create a private network, which allows host-only access to the machine using a specific IP. 
    config.vm.network "private_network", ip: "192.168.101.110" 

    vs.vm.provider "virtualbox" do |vb| 
     # Enable the GUI of VirtualBox and see whether the VM is waiting for input on startup 
     vb.gui = false 
    end 
    end 

end 

我一直在使用vm.provision "shell"cp從客戶試圖複製autorized_keys。我試圖在複製之前更改guest虛擬機上autorized_keys的權限,但似乎沒有任何效果,因爲它沒有連接。我試圖在MyMachineName中執行拷貝,如vs.vm.provision "file", ...

如果我使用vagrant ssh用戶名+密碼登錄一次,然後手動寫入authorized_keys,然後我可以使用SSH密鑰登錄並且沒有密碼。

vagrant ssh-config報告

Host MyMachineName 
    HostName 127.0.0.1 
    User vagrant 
    Port 2222 
    UserKnownHostsFile /dev/null 
    StrictHostKeyChecking no 
    PasswordAuthentication no 
    IdentityFile C:/MyMachineName/vagrant-setup/keys/my_openssh.key 
    IdentitiesOnly yes 
    LogLevel FATAL 

把私鑰到C:\Users\My User Name\.ssh\id_rsa似乎讓一些差異,比如,如果流浪還在尋找一些有儘管我明確地把我自己的私鑰,但不會使其工作。它似乎也有問題C:\Users\My User Name\有空間,但因爲它不應該使用,那應該不重要。

所以問題是如何讓Vagrant使用我自己的一對SSH密鑰工作,而無需手動調整訪客虛擬機?

在這個other question有大量的回覆,但其中大部分歸結爲把鑰匙放在authorized_keys手中,這正是我想要避免的。

+0

在如此重要的情況下,這是我測試的authorized_keys' SSH-RSA AAAAB3NzaC1yc2EAAAABJQAAAQEAvUzbdG0Ex2fr31DPVt6FKAEP + iqpuuJFyxI0962VmaP + UTP23X9YWOsovDbb6izUru0FmjRbfiLhv8GZZ + fUXC0B/XORR/Bm7Ku2ruZ1x1Fuc59NRmqf9AAwm1zV1C3kCPM5LVMOUVChvX3dqgEf7vSbtcmQRECGS2dDbF6cdVMdMg2m1Zn3E34B6y3cB7Csko3fUW9dbyhZPpcx // vGYrNVTzIOOT8EAzvnJeYpNeIhRk1Qk4i9cxPVjqVvnyaIfUJyGRdr/+ rYQkp2i + hOAR7xqzTKnzuDWiIV2RTHD6ImZNfkWDC0wgMSFLdRe4ch/p + eJxhyJZZkteMPJ9QlWQ == CentOrion RSA 2048 SSH-2登錄key' –

+0

可以連接使用簡單'ssh'?該服務器上沒有運行防火牆嗎? – Jakuje

+0

沒有防火牆。我建立在我自己的獨立筆記本電腦上,甚至Windows防火牆都完全禁用。如果我強制使用'vagrant ssh - -vvv'通過用戶名+密碼連接到guest虛擬機,並且將我之前評論中的文本添加到〜/ .ssh/authorized_keys,那麼我可以使用我的私鑰。問題是,當把虛擬機啓動時,Vagrant似乎忽略了我的私鑰,並且總是把它自己的insecure_private_key放在guest虛擬機的〜/ .ssh/authorized_keys中,所以當在'vagrant up'期間,它會嘗試登錄以設置失敗並進入重試循環。 –

回答

0

基於Frédéric Henri評論,這是對我才能登錄只能用我自己的鑰匙,而不是不安全的關鍵也不是用戶+密碼什麼工作:

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

VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 
    config.ssh.insert_key = false 
    rsakey = File.read("vagrant-setup/keys/authorized_keys") 
    config.vm.provision "shell", inline: <<-EOC 
    echo '#{rsakey}' >> /home/vagrant/.ssh/authorized_keys 
    sed --in-place=.bak -r 's/^#?(PermitRootLogin|PermitEmptyPasswords|PasswordAuthentication|X11Forwarding) yes/\1 no/' /etc/ssh/sshd_config 
    sed --in-place=.bak '/== vagrant insecure public key$/d' /home/vagrant/.ssh/authorized_keys 
    EOC 

    config.vm.define "MyMachine" do |vs| 
    vs.vm.box = "vagrant-centos-73-x86_64-puppet" 
    vs.vm.box_url = "https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.3/vagrant-centos-7.3.box" 

    # SSH settings 
    vs.ssh.private_key_path = ['~/.vagrant.d/insecure_private_key', "vagrant-setup/keys/my_openssh.key"] 

    # The shell script that will execute once just after the VM is created 
    vs.vm.provision "shell", path: "vagrant-setup/my_own_custom_setup_stuff.sh" 

    # Create a private network, which allows host-only access to the machine using a specific IP. 
    config.vm.network "private_network", ip: "192.168.101.110" 
    end 

end