如何为 Vagrant 环境配置 SSH?

如何为 Vagrant 环境配置 SSH?

我用这个 Vagrantfile 准备了一个由 4VM 组成的集群。

hosts = {
"n1" => "192.168.77.10",
"n2" => "192.168.77.11",
"n3" => "192.168.77.12",
"n4" => "192.168.77.13"
}

Vagrant.configure("2") do |config|
# always use Vagrants insecure key
    config.ssh.insert_key = false
# forward ssh agent to easily ssh into the different machines
    config.ssh.forward_agent = true
    check_guest_additions = false
    functional_vboxsf = false
    config.vm.box = "ubuntu/bionic64"

    hosts.each do |name, ip|
     config.vm.define name do |machine|
      machine.vm.network :private_network, ip: ip
      machine.vm.provider "virtualbox" do |v|
       v.name = name
      end
     end
    end
end

现在我有四台正在运行的虚拟机。在 vagrant ssh n4 之后,我的下一步是 ssh 到其他机器,但我无法管理。我试过这个

vagrant ssh [email protected]
The machine with the name '[email protected]' was not found configured for
this Vagrant environment.

我已经发布了关于被拒绝许可的问题

ssh [email protected]
[email protected]: Permission denied (publickey).

这个例子来自《Mastering Kubernetes》一书。(Gigi Sayfan)我不确定我是否有权限问题或其他问题。我找不到类似的例子。

我当时试过了

vagrant@ubuntu-bionic:~$ vagrant ssh vagrant@n1
The machine with the name 'vagrant@n1' was not found configured for
this Vagrant environment.

这也不起作用

vagrant@ubuntu-bionic:~$ ssh vagrant@n1
ssh: Could not resolve hostname n1: Temporary failure in name resolution

我真不明白发生了什么事。

我的配置

vagrant ssh-config
Host n1
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/miki/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Host n2
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/miki/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Host n3
  HostName 127.0.0.1
  User vagrant
  Port 2201
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/miki/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Host n4
  HostName 127.0.0.1
  User vagrant
  Port 2202
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/miki/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

如果我尝试

ssh -vvv vagrant@n1
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/vagrant/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "n1" port 22
ssh: Could not resolve hostname n1: Temporary failure in name resolution

杰拉尔德建议

vagrant ssh n1

输出

The machine with the name 'n1' was not found configured for
this Vagrant environment.

答案1

  1. 在您的vagrant up文件夹中,使用vagrant ssh n1

  2. 看来您正在使用带有config.ssh.insert_key = false设置的默认 ssh 密钥,那么使用可能对您有用。ssh [email protected] -i ~/.vagrant.d/insecure_private_key

相关内容