16

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.

1

3 Answers 3

20

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 :-)

1
  • So I notice that my DHCP server doesn't seem to think that my vagrant box is really at that ip :\ Sep 6, 2014 at 3:34
2

After two weeks, I resolved my question this way:

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).

0

What you really need is a bridged interface with a static IP. Unfortunately, vagrant does not yet support this (see discussion here).

Firstly, set box_config.vm.network :bridged as you need virtualbox to create a bridged interface, and you can remove the box_config.vm.network :hostonly.

You can use shell provisioning to re-configure the network with your IP, but you cannot modify the /etc/network/interfaces file or the VM will not be able to come back up if you do a vagrant halt (doing vagrant up will try and reconfigure the network interfaces, and it dies if you've modified that file).
So, the alternative is creating a script in /etc/network/if-up.d/ that re-sets the IP for the interface. Not ideal, but I haven't come up with a better solution yet!


See relevant sections of config below. On the bash script you should set the IP/netmask, gateway and DNS (and interface number, if yours is different, it should be eth1 though if you've just got bridged set in Vagrantfile).
So, the first time you do vagrant up and it creates the VM it creates a script /etc/network/if-up.d/custom-network-config that sets the IP config, routing, and restarts apache (you will need to modify this if you're using other services that rely on the networking config), and it sets DNS.
Then if you do vagrant up again when the VM already exists (eg after doing vagrant halt) all it does is restart the network interfaces so the script gets run and the interface is configured (for some reason, I found the script wasn't kicked off when the VM was turned on and interfaces came up initially - not sure why).

Vagrantfile

config.vm.network :bridged
config.vm.provision :shell, :path => "vagrant-setup.sh"

vagrant-setup.sh

#!/bin/bash

ip="188.120.244.5/24"
gateway=""
dns="8.8.8.8"


#####################
# NEW VM
#####################
if [ ! -f /etc/network/if-up.d/custom-network-config ]; then

cat >/etc/network/if-up.d/custom-network-config <<EOL
#!/bin/bash
if [ "\$IFACE" != "eth1" ]; then
exit 0
fi
ifconfig eth1 down
ifconfig eth1 ${ip} up
route del default
route add default gw ${gateway} dev eth1
service apache2 restart
EOL

cat >/etc/resolv.conf <<EOL
nameserver ${dns}
EOL

chmod +x /etc/network/if-up.d/custom-network-config
/etc/init.d/networking restart


#####################
# EXISTING VM
#####################
else

/etc/init.d/networking restart

fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .