具有多个vms的Vagrantfile:需要指定ssh端口

时间:2015-07-28 01:59:58

标签: vagrant

我有一种情况,我需要专门为每个流浪的vms指定ssh的端口。我已经达到了我在几个项目之间蹦蹦跳跳的地步,所有项目都是我使用流浪汉。

这是我的Vagrantfile的样子:

Vagrant.configure(2) do |config|

  config.vm.define "inspire" do |inspire|
    inspire.vm.box = "hansode/centos-6.6-x86_64"
    inspire.vm.network "private_network", ip: "55.55.55.2"
  end

  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/trusty64"
    web.vm.network "private_network", ip: "55.55.55.3"
  end

  config.vm.define "api" do |api|
    api.vm.box = "ubuntu/trusty64"
    api.vm.network "private_network", ip: "55.55.55.4"
  end

  config.vm.define "lamp" do |lamp|
    lamp.vm.box = "chef/centos-7.0"
    lamp.vm.network "private_network", ip: "55.55.55.5"
  end
end

我启动的第一个是在127.0.0.1:2222上运行,然后是在127.0.0.1:2200上运行,依此类推。我需要知道是否有一个功能可以永久设置,以便inspire始终在端口2001上运行; web始终在2002上运行; api始终在2003上运行;等等。像这样的东西?

Vagrant.configure(2) do |config|

  config.vm.define "inspire" do |inspire|
    inspire.vm.box = "hansode/centos-6.6-x86_64"
    inspire.vm.network "private_network", ip: "55.55.55.2"
    inspire.vm.port 2001
  end

  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/trusty64"
    web.vm.network "private_network", ip: "55.55.55.3"
    web.vm.port 2002
  end

  config.vm.define "api" do |api|
    api.vm.box = "ubuntu/trusty64"
    api.vm.network "private_network", ip: "55.55.55.4"
    api.vm.port 2003
  end

  ...

end

该功能是否存在?

1 个答案:

答案 0 :(得分:2)

不是machine.vm.port而是machine.ssh.port

Vagrant.configure(2) do |config|

  config.vm.define "inspire" do |inspire|
    inspire.vm.box = "hansode/centos-6.6-x86_64"
    inspire.vm.network "private_network", ip: "55.55.55.2"
    inspire.ssh.port = 2001
  end

所以你将连接到启动端口2001

编辑虽然我原本认为以上工作正常,但似乎有一些discussion以及以下内容可以使用

  config.vm.network :forwarded_port,
    guest: 22,
    host: 2201,
    id: "ssh",
    auto_correct: true

vagrant ssh将连接到VM 并验证我是否正确地ssh到2201端口:

fhenri@machine:~$ ssh vagrant@localhost -p 2201
The authenticity of host '[localhost]:2201 ([127.0.0.1]:2201)' can't be established.
RSA key fingerprint is 6d:21:5c:b3:d9:78:c1:d0:77:a9:79:d5:61:51:e5:bb.
Are you sure you want to continue connecting (yes/no)? yes