流浪的ssh-config非常慢

时间:2019-07-01 18:01:07

标签: vagrant

vagrant ssh-config命令耗时超过12秒:

host% time vagrant ssh-config
Host default
  HostName 10.0.0.2
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/user/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

1.614u 0.487s 0:12.27 17.0%     0+0k 0+24io 0pf+0w
host% 

这大概是导致vagrant ssh花费12秒的原因(相比之下,ssh -p2222 vagrant@localhost不使用vagrant ssh-config,仅花费1秒)。

我的Vagrantfile是:

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

Vagrant.configure("2") do |config|

  config.vm.box              = "bento/ubuntu-18.10"
  config.vm.box_version      = "201906.18.0"
  config.vm.box_check_update = false
  config.vm.hostname         = "demo"

  config.vm.network :private_network, ip: "10.0.0.2"
  # enables, for example, 'ssh vagrant@10.0.0.2'

  # None of these actually work. Boo.
  #config.ssh.host = "10.0.0.2"
  #config.ssh.port = 22
  #config.ssh.username   = 'root'
  #config.ssh.password   = 'vagrant'
  #config.ssh.insert_key = 'true'

  config.vm.provider "virtualbox" do |vb|
    vb.name   = config.vm.hostname
    vb.memory = "2048"
    vb.cpus   = "2"
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  end

  # config.vm.synced_folder "../data", "/vagrant_data"

  config.vm.provision "shell", inline: <<-SHELL
    echo "Hello, World!"
  SHELL

end

如果我运行vagrant ssh-config --debug,它将在等待约11秒之前打印以下内容:

 INFO runner: Preparing hooks for middleware sequence...
 INFO runner: 1 hooks defined.
 INFO runner: Running action: environment_load #<Vagrant::Action::Builder:0x0000000001e95f60>
DEBUG checkpoint_client: starting plugin check
 INFO cli: CLI: [] "ssh-config" []
DEBUG cli: Invoking command class: VagrantPlugins::CommandSSHConfig::Command []
DEBUG checkpoint_client: waiting for checkpoint to complete...

然后打印到这一点:

 INFO base: VBoxManage path: VBoxManage
 INFO subprocess: Starting process: ["/usr/bin/VBoxManage", "--version"]
 INFO subprocess: Command not in installer, restoring original environment...
DEBUG subprocess: Selecting on IO

并挂起0.5秒,然后再次打印其他内容和上述行,然后再挂起0.5秒,直到完成。

我希望vagrant ssh-config只是在查询Vagrant的数据结构,而且我希望可以有效地实现它,以便最多在几毫秒内完成。相反,它正在做的事情显然很复杂。

我注意到诸如vagrant destroy --debug之类的其他命令具有类似的行为。也就是说,它们在waiting for checkpoint之后停顿了很长时间,而在Selecting on IO之后停顿了很短。

Vagrant命令(例如vagrant ssh-configvagrant destroy)设计得这么慢吗?关于如何使无所事事的命令运行更快的想法?可以绕开慢速零件吗?

1 个答案:

答案 0 :(得分:0)

这不是一个广泛报道的问题,据我所知,没有办法绕过某些部分。如果速度确实让您感到困扰,我将考虑删除所有的Vagrant,包括~/.vagrant.d目录,重新启动计算机,然后从官方Vagrant installers重新安装最新版本的Vagrant。看看是否可以加快速度。运行vagrant global-status来查看是否运行几个盒子是否在阻碍您的速度,这可能也很有用。