从 centos/7 official box 构建 vagrant box - 身份验证失败

问题描述 投票:0回答:0

我的目标是构建一个定制的 vagrant box。为此,我配置了一个带有 centos/7 box 的虚拟机。从那里我:

  • 创建用户
  • 为此用户添加一个 .ssh 文件夹
  • 添加一个带有公钥的.ssh/authorized_keys
  • 添加 /etc/sudoers.d/user 文件以将其添加为
  • 在 /etc/ssh/sshd_config 中允许密码验证
  • 重启sshd服务
  • 使用我的新用户的 ssh 密钥和密码测试 SSH 连接

就是这样。

要创建一个盒子,我关闭 VM 并使用

vagrant package --base VM_NAME
生成 package.box 文件,然后我将其添加为带有
vagrant box add package.box --name vm_modele

的盒子

当我尝试使用这个新盒子 Vagrant 提供 VM 时抛出身份验证失败:

➜  virtualbox git:(DNS) ✗ vagrant up mirror                        
Bringing machine 'mirror' up with 'virtualbox' provider...
==> mirror: Importing base box 'keos79'...
==> mirror: Matching MAC address for NAT networking...
==> mirror: Setting the name of the VM: virtualbox_mirror_1681293317592_36609
==> mirror: Fixed port collision for 22 => 2222. Now on port 2200.
==> mirror: Clearing any previously set network interfaces...
==> mirror: Preparing network interfaces based on configuration...
    mirror: Adapter 1: nat
    mirror: Adapter 2: hostonly
==> mirror: Forwarding ports...
    mirror: 22 (guest) => 2200 (host) (adapter 1)
==> mirror: Running 'pre-boot' VM customizations...
==> mirror: Disk cannot be decreased in size. 20480 MB requested but disk is already 40960 MB.
==> mirror: Booting VM...
==> mirror: Waiting for machine to boot. This may take a few minutes...
    mirror: SSH address: 127.0.0.1:2200
    mirror: SSH username: vagrant
    mirror: SSH auth method: private key
    mirror: Warning: Authentication failure. Retrying...
    mirror: Warning: Authentication failure. Retrying...
    mirror: Warning: Authentication failure. Retrying...

这里记录下我使用的vagrant文件和.yaml文件:

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

# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"

# Require YAML module
require 'yaml'

# Read YAML file with box details
servers = YAML.load_file('../environments/local_minimum.yaml')
servers = servers["servers"]

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.provision "file", source: "../resources/ssh_keos_key.pub", destination: "~/.ssh/keos.pub"
    config.vm.provision "shell", inline: <<-SHELL
    cat /home/vagrant/.ssh/keos.pub >> /home/vagrant/.ssh/authorized_keys
    sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
    SHELL
        
    servers.each do |vm_config|
        config.vm.define vm_config["name"] do |srv|
            
            srv.vm.box = vm_config["box_name"]
            srv.disksize.size = vm_config["disk_size"]
            srv.vm.host_name = vm_config["hostname"]
            srv.vm.hostname = vm_config["hostname"]
            srv.vm.network :private_network, ip: vm_config["ip_address"]
            srv.vm.provider :virtualbox do |vb|
                vb.memory = vm_config["memory"]
                vb.cpus = vm_config["cpus"]
            end
        end
    end
end
---
servers:
  - name: mirror
    hostname: mirror
    box_name: "vm_modele"
    cpus: 1
    memory: 1024
    disk_size: "20GB"
    ip_address: 192.168.2.70
vagrant virtualbox vagrantfile
© www.soinside.com 2019 - 2024. All rights reserved.