【问题标题】:Can't provision authorized_keys file into VM using Vagrant无法使用 Vagrant 将 authorized_keys 文件配置到 VM 中
【发布时间】:2017-11-05 00:12:43
【问题描述】:

我正在尝试在带有 Windows 7 SP1 主机和 CentOS 7.3 来宾的 VirtualBox 5.1.22 上使用我自己的一对带有 Vagrant 1.9.5 的 RSA SSH-2 密钥。

当我执行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 但它包含 Vagrant 的默认 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 的权限,但似乎没有任何效果,因为它没有连接。我曾尝试在 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 似乎会有所不同,例如尽管我明确设置了自己的私钥,但 Vagrant 仍在那里寻找东西,但并没有使它起作用。 C:\Users\My User Name\ 有空格似乎也有问题,但既然不应该使用它,那就没关系了。

所以问题是如何让 Vagrant 使用我自己的 SSH 密钥对而无需手动调整来宾 VM?

other question 有很多回复,但大部分都是手动将密钥放入authorized_keys 中,这正是我想要避免的。

【问题讨论】:

  • 如果重要的话,这是我的测试 authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAvUzbdG0Ex2fr31DPVt6FKAEP+iqpuuJFyxI0962VmaP+UTP23X9YWOsovDbb6izUru0FmjRbfiLhv8GZZ+fUXC0B/xorR/Bm7Ku2ruZ1x1Fuc59NRmqf9AAwm1zV1C3kCPM5LVMOUVChvX3dqgEf7vSbtcmQRECGS2dDbF6cdVMdMg2m1Zn3E34B6y3cB7Csko3fUW9dbyhZPpcx//vGYrNVTzIOOT8EAzvnJeYpNeIhRk1Qk4i9cxPVjqVvnyaIfUJyGRdr/+rYQkp2i+hOAR7xqzTKnzuDWiIV2RTHD6ImZNfkWDC0wgMSFLdRe4ch/p+eJxhyJZZkteMPJ9QlWQ== CentOrion RSA 2048 SSH-2 login key
  • 您可以使用简单的ssh 进行连接吗?那台服务器上没有运行防火墙吗?
  • 没有防火墙。我正在自己的独立笔记本电脑上构建,甚至完全禁用了 Windows 防火墙。如果我使用vagrant ssh -- -vvv 使用用户+密码强制 ssh 连接到来宾,并将我之前评论中的文本添加到 ~/.ssh/authorized_keys 然后我可以在之后使用我的私钥。问题是,在启动 VM 时,Vagrant 似乎忽略了我的私钥,并且总是将自己的 insecure_private_key 放在来宾的 ~/.ssh/authorized_keys 中,因此当在vagrant up 期间尝试登录以进行配置时,它会失败并进入重试循环.
  • 不是很好,但github.com/mitchellh/vagrant/issues/8058#issuecomment-266193873 应该有助于使用配置脚本来解决这个问题

标签: vagrant ssh-keys vagrantfile vagrant-windows vagrant-provision


【解决方案1】:

根据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

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多