How to set up gateway of public network in vagrant
Asked Answered
K

4

6

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?

Kinchinjunga answered 10/2, 2014 at 12:11 Comment(0)
S
5

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.

Succoth answered 13/12, 2015 at 17:49 Comment(1)
You also might want to specify which network interface you want: sudo route add default gw 10.0.0.1 eth1Radiogram
N
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.

Nefen answered 10/2, 2014 at 14:45 Comment(0)
H
2

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
Hazardous answered 15/5, 2018 at 18:22 Comment(0)
C
0

Adding a solution for ubuntu with netplan to set the default gateway to the public network. persistent across reboots.

config.vm.provision "shell", inline: <<-SHELL
  cat <<EOF >> /etc/netplan/60-override.yaml
  ---
  network:
    version: 2
    renderer: networkd
    ethernets:
      eth0:
        dhcp4: yes
        dhcp4-overrides:
          use-routes: false
      eth1:
        dhcp4: true
   EOF
   netplan apply
   sleep 5
 SHELL
Catawba answered 19/12, 2020 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.