Public static ip for vagrant box

I have server (Debian Squeeze) with 1 ethernet card and 2 public static IPs (188.120.245.4 and 188.120.244.5).

What I want: Setup virtual box (Ubuntu) with access via static IP (188.120.244.5).

What I was trying:

  • config.vm.forward_port - good idea: setup interface "eth1:1" with 188.120.244.5 on host-machine, and add to Vagrant file "config.vm.forward_port = hmm..?"
  • config.vm.network :hostonly, "188.120.244.5" - not working. Was created new interface on host-machine with ip "188.120.244.1". Of course 188.120.244.1 IP isn't mine and I can't access my server via this IP.
  • config.vm.network :bridged - I'm confused how this works :)

What I have now: Not working configuration.

Debian-host-machine# cat Vagrantfile
Vagrant::Config.run do |config|
  config.vm.define :gitlab do |box_config|
    box_config.vm.box = "ubuntu"
    box_config.vm.host_name = "ubuntu"
    box_config.vm.network :bridged
    box_config.vm.network :hostonly, "188.120.244.5", :auto_config => false
  end
end

Debian-host-machine# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:15:17:69:71:bb  
          inet addr:188.120.245.4  Bcast:188.120.247.255  Mask:255.255.248.0

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0

vboxnet0  Link encap:Ethernet  HWaddr 0a:00:27:00:00:00  
          inet addr:188.120.244.1  Bcast:188.120.246.255  Mask:255.255.255.0

Ubuntu-virtual-machine# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:ee:8d:0c  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0

eth1      Link encap:Ethernet  HWaddr 08:00:27:45:71:87  

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0

How I can access virtual box via public static IP from network?

I'm using Oracle VM VirtualBox Manager 4.1.18 and Vagrant version 1.0.3.

Thanks in advance for your feedback.


Since release 1.3.0:

Static IP can now be set on public networks. [GH-1745]

you just have to put this configuration in your Vagrantfile (documentation):

config.vm.network "public_network", ip: "192.168.0.200"

This Vagrant thing is really great :-)


After two weeks, I resolved my question this way:

  • Wrote vagrant-boxes cookbook: https://github.com/numbata/cookbook-vagrant
  • Wrote rebuild-iptables with nat support. https://github.com/numbata/rebuild-iptables

Cookbook generates Vagrantfile from template:

Vagrant::Config.run do |config|
  config.vm.define :gitlab do |box_config|
    box_config.vm.box = "mybox"
    box_config.vm.host_name = "mybox"
    box_config.vm.forward_port 80, 4567
    box_config.vm.forward_port 22, 2222
    box_config.vm.network :hostonly, "192.168.5.10"
  end
end

rebuild-iptables needs to generate and apply iptables rules:

# /etc/iptables/general
*filter
:INPUT ACCEPT [0,0]
:FORWARD ACCEPT [0,0]
:OUTPUT ACCEPT [0,0]
# Vagrand boxes forwarding ports
-A FORWARD -p tcp -d 192.168.5.10 --dport 80 -j ACCEPT
-A FORWARD -p tcp -d 192.168.5.10 --dport 22 -j ACCEPT
COMMIT
*nat
:PREROUTING ACCEPT [0,0]
:POSTROUTING ACCEPT [0,0]
:OUTPUT ACCEPT [0,0]
# Nat all traffic to vagrant boxes
# For example, my vagrant box public static ip is 8.8.8.8
-A PREROUTING -d 8.8.8.8 -p tcp -j DNAT --to-destination 192.168.5.10
-A POSTROUTING -j MASQUERADE
COMMIT

And:

echo '1' > /proc/sys/net/ipv4/ip_forward

Now, i can install applications to the box and connect to them over public static ip without "port_forwarding" setup (Like on VPS).