Trying to figure out where ssh port forwarding is configured in vagrantfile - vagrant

I'm trying to learn Vagrant, Ansible, and Jenkins, so I was super stoked when I came across this repo. A lot of the code is out of date so I've spent my time updating things and it is almost running perfect. The one issue I can't figure out is how/where he is defining the port forwarding for SSH.
For example, when I boot up the nfsserver, I see this line:
nfsserver.sample.com: 22 (guest) => 2222 (host) (adapter 1)
But when I boot up the nfsclient, I see this line:
nfsclient.sample.com: Fixed port collision for 22 => 2222. Now on port 2200.
I've looked through all of his files (even used grep -r) and for the life of me, I can't figure out where he's defining these values.
I found this article that mentions the config.vm.network, but I don't see that called anywhere in the Vagrantfile I'm working with.
I'd appreciate any help in figuring out where this is defined so I can change the values. I (and the others I want to show this to) already have 2222 mapped.

This is just the default behaviour of vagrant, it is not from your configuration file.
since you run multiple VM and for each of the guest VM you want to forward the ssh port (22) on the host, the default value for the host is 2222. The first VM you boot takes this value. But afterwards for following VM which want to forward their 22 port to the 2222 host port, it is not possible as host 2222 is already bound (to the 1st VM) vagrant is then using other values (by default it is using the 2200..2250 range but you can configure that - see link to doc)
This is probably better explained in the Port Collisions and Correction part of the vagrant doc.

Related

No access to web or mysql on vagrant after upgrading to macos monterey

Last week I decided to upgrade the mac to the latest version Monterey. Well. Most things works, except for Vagrant. Well.. it works, except there is almost no connection to the server.
vagrant ssh works.
I have been able to launch virtualbox, but access to http or mysql is not happening.
I know the mysql-server is running. The same goes with the apache server.
Logs have been checked and I cannot see that any traffic going to the server.
Ping is not working.
I have updated virtualbox. I have destroyed the box and upgraded vagrant / homestead. still no luck.
MORE INFO:
When I run traceroute I see that the first hit is the correct IP I have set in hosts file. Then it goes to 192.168.0.1 which isn't going anywhere.
I guess the 192.168.0.1 comes from the mac virtualbox / vagrant is running on.
Any pointers on what to do next are welcome.
Probably the same problem as mine (I couldn't use any longer IP 192.168.10.10). VirtualBox did some changes lately (from VirtualBox 6.1.28 I think) and a new configuration is needed to use your preferred (192.168.0.1) IP address:
On Linux, macOS and Solaris Oracle VM VirtualBox will only allow IP
addresses in 192.168.56.0/21 range to be assigned to host-only
adapters. For IPv6 only link-local addresses are allowed. If other
ranges are desired, they can be enabled by creating
/etc/vbox/networks.conf and specifying allowed ranges there. For
example, to allow 10.0.0.0/8 and 192.168.0.0/16 IPv4 ranges as well as
2001::/64 range put the following lines into /etc/vbox/networks.conf:
* 10.0.0.0/8 192.168.0.0/16
* 2001::/64
You can check the whole information here.
Alternatively (skipping the networks.conf configuration) you can use any IP from the initially supported range like for instance: 192.168.56.10

Vagrant - vagrant cannot forward the specified ports on this VM

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.

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.

I can't start Laravel Homestead on MacOS using VMWare (port collision)

I tried to setup a new Laravel project this afternoon and I must have did something to my Homestead/Vagrant configuration that ruined it. I think the command I used was vagrant reload {id}.
Now when I try to start my machine, I get the following error:
Bringing machine 'homestead-7' up with 'vmware_fusion' provider...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
==> homestead-7: Verifying vmnet devices are healthy...
==> homestead-7: Preparing network adapters...
Vagrant found a port collision for the specified port and virtual machine.
While this port was marked to be auto-corrected, the ports in the
auto-correction range are all also used.
VM: homestead-7
Forwarded port: 80 => 8000
When I run Vagrant global-status, I get this:
id name provider state directory
-----------------------------------------------------------------------
410757f homestead-7 vmware_fusion not running /Users/Me/Homestead
I can't run vagrant reload 410757f as I get the same error above, and I can't provision the machine because it needs to be running.
I'm confused as to whats happening here. There is a networking colission, but I don't have any other vagrant boxes. I currently have 4 other Windows VM's, but I made sure I shut down each machine.
I've even tried destroying and recreating the homestead box (no luck). Any ideas?
Edit To extend on this, I tried looking for the process using sudo lsof -i :8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
vmnet-nat 3943 root 42u IPv4 0x70d4a03b6f2dddd 0t0 TCP *:irdmi (LISTEN)
I killed that using sudo kill -9 3943 and ran sudo lsof -i :8000 again, which gave me nothing, then running homestead up again gave me the same error.
I seemed to solve this by removing and reinstalling VMWare.
Based on my readings online, the issue seemed to be with a cached setting relating to a previous VM on Fusion. Rather than hunt down what that might have been, I thought it was easier to just delete everything and reinstall.
I followed the instructions here and then downloaded and reinstalled it from the VMWare website.
I hope this helps someone in the future!

Vagrant Port Forwarding on Mac OS X Lion

I've been struggling trying to connect to a centos 6.4 vm using Vagrant.
I'm using salt as a provisioning agent and I have installed apache,php,mysql packages successfully.
When i ssh into the box apache is running fine. I added an index.html file in /var/www and I get the contents back when I curl localhost:80
Vagrant.configure("2") do |config|
## Chose your base box
config.vm.box = "centos63"
config.vm.box_url = ""
## For masterless, mount your salt file root
config.vm.synced_folder "salt/roots/", "/srv/"
## Use all the defaults:
config.vm.provision :salt do |salt|
salt.verbose = true
salt.run_highstate = true
salt.minion_config = "salt/minion"
end
end
Vagrant::Config.run do |config|
config.vm.forward_port 80, 8080
config.vm.share_folder "mypath", "/var/www/leo", "."
end
I ran sudo lsof -i :8080 on my local machine and gave me no results. I also cleared the iptable config in the guest machine with iptables -F. When I curl the guest machine
curl -v 'localhost:8080'
* About to connect() to localhost port 8080 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* Connection refused
* Trying fe80::1...
* Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host
Do I need guest additions installed? I looked around on how to install this but I'm not sure if it has to be installed on the host or the guest. Not sure what else to try.
What you are trying to do here isn't possible just using vagrant without running vagrant as root. You can run Vagrant as root i believe but VirtualBox won't agree with that. You can continue to use a port number or if you want or need to use port 80 there is a way.
I had this issue when a client of mine asked me to do a Wordpress Multisite setup. With Wordpress MS you can't have port numbers in the URL b/c some of the URL mapping will not work correctly. I was surprised when I found that out and didn't want to go back to using a program like MAMP.
Anyway here are two ways to achieve this goal (neither are very hard). I'm a Mac user so these are Mac specific Answers, I will see if there is a Windows version and update my answer when I can test it to make sure (see below, there is a way).
Way #1 (Mac IP Firewall Utility):
In your vagrant file
config.vm.forward_port 80, 8080
config.vm.forward_port 443, 8443
Thats pretty normal.
Now open up terminal and you can use the ipfw utility
sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to me 80
sudo ipfw add 101 fwd 127.0.0.1,8443 tcp from any to me 443
Now that cmd is not permanent so you would have to re-run the cmd if you restarted your machine. You can make it permanent though and I am including a link below that will explain the rest of way #1.
Web Development on Port 80 and 443 in Vagrant
Way #2 (Mac POW and Anvil):
If you don't have Pow yet, get it! It's a really cool app.
Install Pow and Anvil, you can find Anvil there and you can find Pow there.
You can read the docs on how to set those up but don't pay attention to the "static" and "rack" sites part, you need this part.
You will be using Port Proxying through Pow to take the incoming traffic from mycoolsite.dev and forward it to the virtual machine like mycoolsite.dev:8080 and then the virtual machine will forward 8080 to 80 and back up the line your content will come.
After you install Anvil/Pow and get them set up run this line:
echo 8080 > ~/.pow/mycoolsite
Then click Anvil in the task bar (you may have to refresh it or close and reopen) and turn the site on, thats it, what?? Really? Pow and Anvil rock!!
So there are two ways I have found, I'm sure that there are some things you can do with your Hosts file and I used to do that a bunch. However, these other ways that are available really make it easy to forget about that pesky hosts file.
Note for Windows Users (and Mac users that don't like the first 2 ways): You can use Vagrant Host Manager, you can find out how to set it up here on github. It is a vagrant plugin and basically will edit your hosts file for you, all you do is your vagrantfile config and you are good to go after that. I just tested it on Windows 7 and it worked there so it should be good, if you have any issues just look through the docs on github or file an issue for the Vagrant Host Manager team to review.
I changed the guest port to 5656 and it worked. When running lsof -i :8080 I didn't see any results so i figured nothing was using that port maybe I was wrong.

Resources