Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you access a VM using Vagrant private network on different ports?

I'm running a private network Vagrant environment with a specified ip address and hostname. I can currently access the http version on port 80, but I'd also like to be able to access it on port 8080.

I added something like this:

config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true

But, when I visit my site on port 8080, I get an unavailable page error. Port 80 still works. how can I properly access my VM on a different port if the forwarded port doesn't work for private network?

like image 792
jcjcjcjc Avatar asked Mar 31 '14 20:03

jcjcjcjc


People also ask

How do I access Vagrant from another computer?

I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh . And connected to another computer by typing in ssh [email protected] where 192.168. x.x is the local address to the computer.

What port does Vagrant use?

It is common when running multiple Vagrant machines to unknowingly create forwarded port definitions that collide with each other (two separate Vagrant projects forwarded to port 8080, for example). Vagrant includes built-in mechanism to detect this and correct it, automatically.

What is Vagrant private network?

Vagrant private networks allow you to access your guest machine by some address that is not publicly accessible from the global internet. In general, this means your machine gets an address in the private address space.

Which network should the network bridge to Vagrant?

Default Network InterfaceIf more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.

What are the network options available in a vagrant VM?

Vagrant offers the following three network options: 1. Configure port forwarding By default, we can access Vagrant VMs via ssh using vagrant ssh command. When we access a VM via SSH, Vagrant forwards port 22 from the guest machine to an open port in the host machine. This is called port forwarding.

How to configure private or host-only networking in Vagrant?

Each VM in the private network will get a IP address from this IP space. So, we can access the VMs directly using the IP from the host system. To configure private or host-only networking in Vagrant with static IP address, open the Vagrantfile, find the following line and uncomment it.

How to access a vagrant VM via SSH?

By default, we can access Vagrant VMs via ssh using vagrant ssh command. When we access a VM via SSH, Vagrant forwards port 22 from the guest machine to an open port in the host machine.

How to use port forwarding in Vagrant?

By default, we can access Vagrant VMs via ssh using vagrant ssh command. When we access a VM via SSH, Vagrant forwards port 22 from the guest machine to an open port in the host machine. This is called port forwarding. Vagrant automatically handles this port forwarding process without any user intervention.


1 Answers

Remember Vagrant Port forwarding ONLY works for the default NAT networking (using VirtualBox NAT), not private or public.

So if you want to access the service via private IP on port 8080, you either listen it on port 8080 or use iptables to forward packets.

e.g.

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

like image 133
Terry Wang Avatar answered Oct 08 '22 21:10

Terry Wang