Vagrant - vagrant cannot forward the specified ports on this VM - vagrant

When i run vagrant up i get the following error:
Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded
ports! The forwarded port to 3306 is already in use on the host
machine.
To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.forward_port 80, 1234
I checked and i don't have any processes using port 3306 locally.
I also tried vagrant destroy and vagrant up, didn't help.
vagrant suspend followed by vagrant resume didn't help as well.
What else could be the issue?

What's your command to find the port?
Maybe you can see this. Vagrant Port Collision on Port 80, but Port 80 is not Forwarded in the VagrantFile

Turns out there was a process on my machine running on port 3306.
Running lsof -i :3306 didn't show it, only when i used sudo it did.

Related

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.

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.

Laravel Homestead - How to edit homestead.rb when virtualbox not running?

I attempted the top answer on this thread:
Vagrant port forwarding 80 to 8000 with Laravel Homestead
which was to make this change in the homestead.rb file:
config.vm.network "forwarded_port", guest: 80, host: 8000 to
config.vm.network "forwarded_port", guest: 80, host: 80
I am running a newer version of homestead (not sure where to find the exact version), on a mac.
It did not work, when attempting to start up my homestead virtualbox it now says:
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 80 is already in use on the host
machine.
I had edited the homestead.rb file via ssh and vim while the homestead virtualbox was running, but now I can't get it to run and I can't figure out how to undo my changes.
Help with both of these would be greatly appreciated!
How to edit the homestead.rb file when the virtualbox is not running? I believe it is inside either VirtualBox VMs/homestead/homestead.vbox or box-disk1.vmdk, but I do not know how to access inside them.
Help with getting the port to forward correctly to 80 since the solution from the other question did not work.
Homestead 2.+ stores it's files inside your .composer directory inside your User directory. For example, for me it is in:
/Users/noel/.composer/vendor/laravel/homestead
You will find all the configuration and script files that used to reside in the older Homestead 1.0 directories.
Now, since something is holding on to your port 80, it probably means that your local apache installation is running. You can test to see what is holding on to the port by running:
sudo lsof -i :80
To list all processes listening on port 80 (incoming and outgoing).
To stop the local apache you can run:
sudo apachectl stop
That should release the port for you to use with your virtual machine.

Vagrant port forwarding not working on Mavericks

I'm using a vagrant Geodjango box and port forwarding is not working for me.
On the box, I have run:
python manage.py runserver 0.0.0.0:8000
But http://localhost:8000 and http://localhost:4567 both find nothing on the host machine.
On the Vagrant box, curl -v 'http://localhost:8000/' gives the usual:
<h2>Congratulations on your first Django-powered page.</h2>
which suggests that Django is running okay. But on the host machine, trying curl -v 'http://localhost:8000/' gives the following output:
curl: (7) Failed connect to localhost:8000; Connection refused
My Vagrantfile has the following port forwarding set up:
config.vm.forward_port 8000, 4567
Disabling the Mac's firewall does not help and stopping Apache makes no difference. I have tried running lsof -i :8000 on the host machine and there is no output, so I figure nothing is using the port.
Can anyone suggest anything?
I had the same issue on Yosemite and none of the ports were forwarding. Disabling the Firewall filter on the guest machine helped:
sudo service iptables stop
Good to see you figured it out yourself.
Just want to add my 2 cents, in V2 Vagrantfile, the port forwarding code block is like below, try to use the new ones so as to avoid port conflicts (back in v1 I always got confused which is which).
config.vm.forward_port 8000, 4567 is forwarding guest port 8000 to host 4567, not the other way around.
In V2 format, it looks like below, which is clearer from my opinion
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080
end

Two separate Vagrant machines, windows host, PuTTY - how?

I'd like to create two vagrant machines via two vagrant files, and be able to ssh into them via PuTTY.
I thought it might be as simple as port forwarding one of them via, say, port 2223 instead of 2222, and using two PuTTY connections.
Despite my vagrant ssh-config looking like this:
HostName 127.0.0.1
User vagrant
Port 2223
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
IdentitiesOnly yes
LogLevel FATAL
... I seem able to ssh into it via PuTTY on port 2222, which I'm hoping to reserve for access to the other instance which I've not yet set up. I'm new to vagrant and may be making a noob mistake. Help appreciated.
As per Vagrant Base Box specification, the default networking mode is NAT and port forwarding for SSH is enabled (guest 22 => host 2222).
What you've done, changing the sshd_config file within the guest won't work because that only changes the SSH port within the guest to 2223, NOT the host.
For the 2nd vagrant box, you need to do the following:
Get the name or UUID VBoxManage list vms
Use VBoxManage showvminfo VM_Name to get the list of port forwarding rules
Delete its default guestssh NAT port forwarding rule
Add a new port forwarding rule to do guest 22 => host 2223
For example:
By default the rule is named ssh
NIC 1 Rule(1): name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2222, guest ip = , guest port = 22
Delete it
VBoxManage modifyvm "VM_Name" --natpf1 delete "ssh"
Add a new rule
VBoxManage modifyvm base_box --natpf1 "guestssh, tcp,,2223,,22"
NOT DONE yet!!!
Do NOT use vagrant up to start this VM, because it'll add the ssh rule back.
You can use VirtualBox GUI or VBoxManage controlvm to start it. And connect to it using ssh -p 2223 vagrant#localhost, password is vagrant. You can also use the insecure key pair to do public key authentication, doesn't make much sense though.
NOTE: changing, adding and deleting port forwarding rules can be done using the VirtualBox GUI anyway, if it is easier for you.
You can set any port you like by putting this in your vagrantfile:
config.vm.network "forwarded_port", guest: 22, host: 2223
replacing 2223 with your port of choice - different for each VM, obviously.
Note that this is in addition to the standard 2222 port forwarding, which will still be mapped for every VM. One of them "wins" and answers on 2222 as well as whatever custom port you set up.
The procedure in the accepted answer may well work, but it seems a little convoluted.

Resources