Vagrant
Configure the network
In this tutorial, you will use Vagrant's networking features to provide access to the guest machine from your host machine.
Prerequisites
In the previous tutorial, Provision a Virtual Machine, you deployed a web server with the ability to modify files from your host and have them automatically synced to the guest. We recommend that you complete the previous tutorial, however, you can also get started if you have the following prerequisites.
- Install the latest version of Vagrant.
- Install a virtualization product such as: VirtualBox, VMware Fusion, or Hyper-V.
Configure port forwarding
Port forwarding allows you to specify ports on the guest machine to share via a port on the host machine. This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine.
To set up a forwarded port so you can access Apache on your guest, add the config.vm.network
parameter to your Vagrantfile. Below is the full file with port forwarding.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
Reload so that these changes can take effect.
$ vagrant reload
Access the served files
Once the machine has loaded, you can access http://127.0.0.1:4567
in
your browser. You will find a web page that is being served from
the guest virtual machine.
Next steps
Vagrant also has other forms of networking, allowing you to assign a static IP address to the guest machine, or to bridge the guest machine onto an existing network. If you are interested in other options, read the networking page.
You have successfully configured networking for your virtual machine using Vagrant. Read on to learn about setting up shares with Vagrant.