How to forward a port to vagrant guest if this port already listened by host machine - vagrant

For example I have an app which run vagrant machines dynamically and expect some info from them will be sent through http to specific host machine port. So, my app listening specified port (http server) and I can't forward that port:
C:\node-vagrant-test-task>vagrant reload
==> default: Clearing any previously set forwarded ports... Vagrant cannot forward the specified ports on this VM, since they would
collide with some other application that is already listening on these
ports. The forwarded port to 8080 is already in use on the host
machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 8080, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

If you do not want to mess with forwarding port, the best is to use a static IP for the guest
you can do either private or public networks with a static IP
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
or
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.50.4"
end
so you dont need to forward the 8080 port and can access your app directly on http://192.168.50.4:8080
If you need to then access the host machine for this guest, you can access it via 192.168.50.1

Related

Vagrant : Connect from client to local https site

I am working on a chef project which is running in Vagrant. I have installed chef server on my local machine and I need to connect it from vagrant instance. I am getting a connection refused. I have added port-forwarding, but vagrant wont let me forward port-443 as it's already occupied. My chef server is running on https i.e port-443.
Error log :
[2019-05-03T10:21:00+00:00] INFO: Client key /etc/chef/client.pem is not present - registering
[2019-05-03T10:21:00+00:00] ERROR: Connection refused connecting to https://my-system-product-name/organizations/internetdevelopment/clients, retry 1/5
[2019-05-03T10:21:05+00:00] ERROR: Connection refused connecting to https://my-system-product-name/organizations/internetdevelopment/clients, retry 2/5
VagrantFile :
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 443, host: 8443
config.vm.network "forwarded_port", guest: 80, host: 8080
i would offer you to assign a static ip address to your vagrant virtual machine (vm) rather than use a port forwarding.
You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
It is up to the users to make sure that the static IP does not collide with any other machines on the same network.
While you can choose any IP you would like, you should use an IP from the reserved private address space. These IPs are guaranteed to never be publicly routable, and most routers actually block traffic from going to them from the outside world.

How configure Vagrant so doesnt block forwarded ports on host when on private network

I want a vagrant VM to behave like a box not hosted on my host machine.
I want to be able to do
curl <vagrant private ip> #nginx on the vm
and have that work from the host machine
curl localhost #e.g. iis
and have that work from the host machine
but it seems it's not possible as when you do port forwarding it always occupies the host port too on the loopback/or IP and you cant restrict it to just the private IP of the VM, basically, you cant have the VM running and start iis.
If that is the case ok, that's that.
currently, my Vagrant file has
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "private_network", ip: "192.168.33.11
I know the forwarded_port has guest_ip and host_ip settings but I didn't find they did anything when I tried to set to the private network IP.
Any suggestion of easy workaround also welcome.

vagrant ports report in use after reload

I have a few vagrant VMs (both on Mac and windows, all running linux). I have port forwarding defined like this in the Vagrant file:
config.vm.network "forwarded_port", guest: 80, host: 9007
The first time I do a vagrant up it works fine. But then when I do a vagrant reload or vagrant halt followed by vagrant up I get this message:
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 9007 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 80, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
Then I have to change the port to bring up the VM, which is a pain. I have checked with netstat and lsof, and nothing seems to be using the port. I though it was perhaps a timing issue, and after a while I could use the port, but even after waiting many hours I still get the error.
This happens in both the Mac and Windows environments. Is there some setting that would allow me to reuse the ports?
I generally use static IP to avoid issues with port forwarding. Vagrant has an auto_correct feature to help fix that.
from vagrant forwarded port docs
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.
Port collision detection is always done. Vagrant will not allow you to
define a forwarded port where the port on the host appears to be
accepting traffic or connections.
Port collision auto-correction must be manually enabled for each
forwarded port, since it is often surprising when it occurs and can
lead the Vagrant user to think that the port was not properly
forwarded. Enabling auto correct is easy:
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080,
auto_correct: true
end
The final :auto_correct parameter set to true tells Vagrant to auto
correct any collisions. During a vagrant up or vagrant reload, Vagrant
will output information about any collisions detections and auto
corrections made, so you can take notice and act accordingly.

Vagrant: port forwarding is working oddly

I've got this in my Vagrantfile:
web.vm.network "private_network", ip: "192.168.33.20"
web.vm.network "forwarded_port", guest: 80, host: 8080
but on my Mac Host I'm able to access the web VM using 192.168.33.20:80 but NOT 192.168.33.20:8080.
Why is this not working as expected?
vagrant Port forwarding is used if you dont have static IP and you want to reach a specific port for a process bound to this specific port running on the VM, so you will access it using http://localhost:8080 and it will forward to port 80 on the VM.
When you have a static IP, you reach directly the network interface to this VM so it tries to connect you on the port 8080 directly in the VM, if you dont have anything running there, it wont show up.
Rules: if you use static IP (wether private or public network) you dont need port forwarding

How can I access a vagrant guest from another virtualbox guest?

The scenario is that my dev environment is on a Vagrant box on my laptop (host) and I would like to do browser testing in a vitualbox vm, so I need to see one vm from another.
The vagrant box's port is :8080 which is forwarded to the host on the same port :8080. So I can see the server from the host at localhost:8080
Which address should I be using for the browser testing vm?
The testing vm's default gateway?
The vagrant vm's ip?
The host's virtual network ip?
And should I be using a NAT or host only adapter on the browser testing vm?
That makes for a lot of combinations, all of which I believe I have tried. What else do I need to understand here?
In your use case, you should be using Bridged networking (Public Network in Vagrant). If the VMs reside on the same host, you can even use internal (Private Network in Vagrant).
If using Public Network, the VM's 2nd NIC will be able to obtain an IP address from the DHCP server in your network (e.g. your home router).
Simply add the following code block in your Vagrantfile and do a vagrant reload
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
You should be able to get the IP address by using vagrant ssh and ifconfig / ip addr show.
In case you don't want to go with public_network just like me then you should do the steps below using private_network:
Open Vagrantfile from your project root
Search for config.vm.network
Add this line config.vm.network "private_network", ip: "192.168.33.10". Remember this is not the IP of your base machine it's a virtual-box IP address and your machine IP should be different. You can say it's a fake IP address so change it to anything else like 192.168.30.20.
Reload your vagrant using vagrant reload.
Now go to your other virtual guest in my case it's the Windows Guest 2. My base is Linux Mint Vagrant box is on Ubuntu Guest 1. Open C:\Windows\System32\drivers\etc\hosts file as admin and do the above IP's entry in there like 192.168.33.10 local.youralias.com. And save the file, after that you can now browse the site now at http://local.youralias.com/.
In case your guest 2 is also Linux just edit this file sudo vi /etc/hosts, and add this line at top of it 192.168.33.10 local.youralias.com. Now save and exit and browse the URL :)
Enjoy! Happy coding.
Adding to accepted answer, you can actually set IP and specify which network interface to use.
My setup on linux box via wifi and static IP:
You can find your wifi interface name by running ifconfig command.
Vagrant.configure("2") do |config|
config.vm.network "public_network", :bridge => 'wlp8s0', ip: "192.168.1.199"
end
This may have many source cause. In my case, I use vagrant fedora boxe.
I tried:
First using the private_network that I attached to a host only adapter and launched httpd service to test the connection between guest and host
config.vm.network "private_network", type: "dhcp", name: "vboxnet2"
config.vm.network "forwarded_port", guest:80, host:7070
but I was not able to ping my guest machine from the host and could no telnet the httpd service opened
Second using public_network and launched httpd service to test connectivity
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", use_dhcp_assigned_default_route: true
I could ping my guest from my host but I could not telnet the httpd service.
For this two use case, the issue was that the port 80 on the fedora guest host was blocked by the firewall. Here is what fixed the issue and get all working for both privat_network and public_ntwork:
firewall-cmd --permanent --add-port 80/tcp #open the port permanently
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --list-port # list to check if the port was opened
systemctl stop firewalld # stop and open the firewall service
systemctl start firewalld
Old question, new answer: [disclaimer: i am not a vagrant expert]
both solutions might work but the solution in the "vagrant way of thinking" is that some component in your guest (rinetd?) should forward any requests to unknown ports to the host. From the host the request could then be mapped (via vagrant port forwarding) to a services that is running in the other guest.
So, to resume:
1.in guest-1 we do localhost:1234. Guest-1 will detect that this port is not available and forward to host
2. the host will check the vagrant port forwarding and forward to guest-2
3. in guest-2 we have some nice service listening to post 1234
4. done.

Resources