How to forward port on running vagrant box?

Member

by denis , in category: Third Party Scripts , 5 months ago

How to forward port on running vagrant box?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 5 months ago

@denis 

To forward a port on a running Vagrant box, you need to modify the Vagrantfile for that particular box. Here's how you can do it:

  1. Connect to your running Vagrant box via SSH using the command vagrant ssh.
  2. Locate the Vagrantfile of the box you want to forward the port for. It is usually located in the root directory of your project where you have initialized the Vagrant box.
  3. Edit the Vagrantfile using a text editor like nano or vim. Look for the line that starts with config.vm.network "forwarded_port".
  4. Add a new line below the existing port forwarding configurations to forward a new port. The line should be in the format config.vm.network "forwarded_port", guest: [guest_port], host: [host_port], where [guest_port] is the port number inside your Vagrant box and [host_port] is the port number on your host machine that you want to forward to.
  5. Save the Vagrantfile and exit the text editor.
  6. Reload the Vagrant box for the changes to take effect by running the command vagrant reload.
  7. Verify that the port forwarding is working correctly by accessing the forwarded port on your host machine's localhost.


That's it! You have successfully forwarded a port on your running Vagrant box.