How to set up gateway of public network in vagrant

11,069

Solution 1

According to the (new) documentation, setting up a gateway, can be done by instructing the following, inside the Vagrantfile:

# default router
config.vm.provision "shell",
  run: "always",
  inline: "route add default gw 192.168.0.1"

Read the complete example titled Default Router at http://docs.vagrantup.com/v2/networking/public_network.html.

Solution 2

Unfortunately Vagrant doesn't support this at the moment.

But there are requests for adding it, for example GH-2832 and GH-2389. The latter one also has some shell provisioner examples you could use to work around it.

Solution 3

Vagrant 2.0.1 has a configuration option for this which is explained in the documentation:

Vagrant → Networking → Public Networks → Using the DHCP Assigned Default Route

For your convenience, here is the relevant section (retrieved 2018-05-15):

Using the DHCP Assigned Default Route

Some cases require the DHCP assigned default route to be untouched. In these cases one may specify the use_dhcp_assigned_default_route option. As an example:

Vagrant.configure("2") do |config|
  config.vm.network "public_network",
   use_dhcp_assigned_default_route: true
end
Share:
11,069
deDishari
Author by

deDishari

Updated on June 04, 2022

Comments

  • deDishari
    deDishari almost 2 years

    I am setting up my Virtualbox Centos VM with vagrant. I am setting up with a public network.

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

    How do I setup the GATEWAY along with this?

  • Peter Tseng
    Peter Tseng almost 8 years
    You also might want to specify which network interface you want: sudo route add default gw 10.0.0.1 eth1