Adding port forwarding on an existing vagrant box - vagrant

My running vagrant box (ubuntu, on a OS X Mavericks host) has been running fine.
I am trying to set up pgAdmin from the host, and am having trouble opening a new port for the sql traffic.
I added a line to my Vagrantfile (the last one):
config.vm.network "forwarded_port", guest: 3000, host: 8080 # http
config.vm.network :forwarded_port, guest: 35729, host: 35729
config.vm.network "forwarded_port", guest: 5432, host: 7001 # postgres
I ran vagrant provision and bounced the Vagrant box several times. When it reboots the new port forwarding is not listed:
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 3000 => 8080 (adapter 1)
default: 35729 => 35729 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
Curl gives a negative response also:
➜ ~ curl -v 'http://localhost:7001/'
* Adding handle: conn: 0x7fde1a004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fde1a004400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 7001 (#0)
* Trying ::1...
* Trying 127.0.0.1...
* Trying fe80::1...
* Failed connect to localhost:7001; Connection refused
* Closing connection 0
curl: (7) Failed connect to localhost:7001; Connection refused
Related posts:
Vagrant Port Forwarding not working
Cannot connect to Postgres running on VM from host machine using MD5 method

Another way to set up a temporary terminal, besides opening up the VirtualBox settings, is to use vagrant ssh, with additional arguments given after the --:
vagrant ssh -- -L 3000:localhost:3000
This will forward port 3000 on the host to port 3000 on the guest.
The first number is for the host. To forward port 7001 on the host to the default postgresql port on the guest:
vagrant ssh -- -L 7001:localhost:5432
This will only last as long as your ssh session. If ssh gets disconnected, run it again. To make it persist after restarts, add it to your Vagrantfile.

Since the 5432<-->7001 port mapping is not listed in the Vagrant up sequence, it's not happening.
I would try a vagrant reload which is supposed to reload those part of the Vagrantfile again.
If that doesn't work, you could also try manually adding the port mapping, at least to confirm the connection to your application. The accepted answer for Change Vagrant port forwarding on a running system explains how to do that in the VirtualBox UI.

Related

when i vagrant up, port 80 is not forwarded

i get this output when i vagrant up, port 80 is not forwarding so how i will be able to access the site via web browser
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Setting the name of the VM: Homestead_default_1472822347316_22641
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
help me ,thanks
The default Vagrantfile, generated by vagrant init, includes an example of how to forward port 80:
# config.vm.network "forwarded_port", guest: 80, host: 8080
Simply uncomment this line and adjust the ports as necessary.
Vagrant can't forward host ports < 1024.
So if you do config.vm.network "forwarded_port", guest: 80, host: 80 it won't work.
You can use a local SSH tunnel to listen on port 80 on your host and forward the traffic to port 80 on your guest.
sudo ssh -p 2222 -gNfL 80:localhost:80 vagrant#localhost -i ~/.vagrant.d/insecure_private_key

Laravel homestead and vagrant setup not working

I followed the original documentation for vagrant and homestead but every site.app:8000 you visit it just keeps loading and nothing happens. Is there an error within this configuration files?
Homestead.yaml
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Developer/laravel
to: /home/vagrant/laravel
sites:
- map: hello-world.local
to: /home/vagrant/laravel/hello-world/public
databases:
- homestead
hosts
192.168.10.10 hello-world.local
vagrant up log
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 80 (guest) => 8000 (host) (adapter 1)
default: 443 (guest) => 44300 (host) (adapter 1)
default: 3306 (guest) => 33060 (host) (adapter 1)
default: 5432 (guest) => 54320 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.0.20
default: VirtualBox Version: 5.1
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/nikla/Developer/homestead
default: /home/vagrant/laravel => /Users/nikla/Developer/laravel
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
nmap log
Starting Nmap 7.12 ( https://nmap.org ) at 2016-07-29 22:55 ART
Nmap scan report for 192.168.10.10
Host is up (0.00063s latency).
Not shown: 994 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https
3306/tcp open mysql
5432/tcp open postgresql
9000/tcp open cslistener
Nmap done: 1 IP address (1 host up) scanned in 13.19 seconds
I have check your "vagrant up" log and I see all things ok. I have compared your log with my machine's log and is similar.
Try to open the IP in the web browser (change the IP with your Vagrant VM IP):
http://192.168.10.10/
Do you have some firewall, antivirus or another software that could disable the communication?
Try to execute a nmap to see wich ports are open. I paste my nmap, from my host machine to my virtual machine, so you can see wich ports are open.
$ nmap 192.168.10.10
Starting Nmap 6.47 ( http://nmap.org ) at 2016-07-30 01:48 Hora de verano romance
Nmap scan report for homestead.app (192.168.10.10)
mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers
Host is up (0.0019s latency).
Not shown: 993 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
111/tcp open rpcbind
443/tcp open https
3306/tcp open mysql
5432/tcp open postgresql
9000/tcp open cslistener
MAC Address: 08:00:27:CD:99:8C (Cadmus Computer Systems)
Nmap done: 1 IP address (1 host up) scanned in 21.83 seconds
Check:
The id_rsa.pub and the id_rsa are in this folder.
The /Users/nikla/Developer/laravel folder exists in your host machine.
The /home/vagrant/laravel exists in your Vagrant VM machine. You can check it login in the machine using the command "vagrant ssh".
Try to access to the http://test.app without the 8000 port.
I don't see the database in your Homestead.yaml file. You should have the next text in this file
databases:
- homestead

Port forwarding on Vagrant 1.8.1

I have a vagrant installed on Windows7 PC. I have jenkins, puppet other stuff installed on my centos7 vagrant box. When I do vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 8082 (guest) => 8080 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions 5.0.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => C:/vagrant/projects/jenkins
default: /vagrant_data => C:/Devops
==> default: Machine already provisioned. Run `vagrant provision` or use the `-
provision`
==> default: flag to force provisioning. Provisioners marked to run always will
still run.
Now when I do curl -v
C:\>curl -v http://127.0.0.1:8082
* Rebuilt URL to: http://127.0.0.1:8082/
* timeout on name lookup is not supported
* Trying 127.0.0.1...
* connect to 127.0.0.1 port 8082 failed: Connection refused
* Failed to connect to 127.0.0.1 port 8082: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 8082: Connection refused
Needless to say http://127.0.0.1:8082 doesnt fetch anything in the browser wither. What am I missing? BTW my vagrant and virtual box 5 is running on Windows7.
Thanks in advance
R

forwarding the ssh port fails when running two vagrant instances from the same host?

Trying to run two vagrant instances on the same host. The vagrant files on the first instance does nothing special with the SSH ports:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 8080, host: 9080
config.vm.network "forwarded_port", guest: 8081, host: 9081
config.vm.network "forwarded_port", guest: 9990, host: 9090
config.vm.network "forwarded_port", guest: 8983, host: 8983
config.vm.network "forwarded_port", guest: 8985, host: 8985
config.vm.network "forwarded_port", guest: 2181, host: 2181
config.vm.network "forwarded_port", guest: 8002, host: 8002
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true
And this instance spins up as expected:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!
The second instance forwards port guest 22 to host 2221:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 8080, host: 10080
config.vm.network "forwarded_port", guest: 80, host: 45678
config.vm.network "forwarded_port", guest: 9999, host: 10999
config.vm.network "forwarded_port", guest: 9000, host: 10900
config.vm.network "forwarded_port", guest: 22, host: 2221
But when it spins up, I'm still getting conflicts on port 22222:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise32' is up to date...
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 2222 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.network :forwarded_port, guest: 22, 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.
So I had this working, with the same two vagrant files 3 months ago. It seems like there has been an update to the underlying 'hashicorp/precise32' boxes without a corresponding documentation change, and modifications to the ssh port forrwarding are no longer allowed? Updating the config.ssh.port would seem to require a change to the underlying image.
Thoughts?
==================================================
Update:
Launching the 2nd instance (forwards 22 => 2221), then reloading produces the following output:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!
Elizabeth#Work /cygdrive/c/Gui-again
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 8080 => 10080 (adapter 1)
default: 80 => 45678 (adapter 1)
default: 9999 => 10999 (adapter 1)
default: 9000 => 10900 (adapter 1)
default: 22 => 2221 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
default: /vagrant => C:/OSC/PBD-Gui-again
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run
So somewhere the ssh port is being forced to 2222. This isn't in the vagrant file itself, where else to look?
You can try adding an 'id ssh' to the end
config.vm.network :forwarded_port, guest: 22, host: 2221, id: 'ssh'
Or maybe this works for you, to first disable the ssh port (Depends on the Vagrant version)
config.vm.network :forwarded_port, guest: 22, host: 2221, id: "ssh", disabled: true
config.vm.network :forwarded_port, guest: 22, host: 2221, auto_correct: true
Source:
https://github.com/mitchellh/vagrant/issues/3232

Windows + Vagrant port forwarding not working

I am using vagrant 1.6.2 on Windows 7, to run a Ubuntu 12.04 Virtual Machine.
I have enabled this on my Vagrantfile:
config.vm.network :forwarded_port, guest: 3000, host: 3000
config.vm.network :forwarded_port, guest: 27017, host: 27017, auto_correct: true
config.vm.network :forwarded_port, guest: 27018, host: 27018, auto_correct: true
config.vm.network :forwarded_port, guest: 9494, host: 9494
In the box, I can see that Mongodb is running,
~ $ mongo
MongoDB shell version: 2.0.4
connecting to: test
> exit
bye
However, from my Windows Host, I am unable to connect to the Mongodb instance, using localhost:27017.
The auto_correct: true was something I read on one of the forums, and tried it out.
On vagrant up this is what I get on the console:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 3000 => 3000 (adapter 1)
default: 27017 => 27017 (adapter 1)
default: 27018 => 27018 (adapter 1)
default: 9494 => 9494 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
GuestAdditions 4.3.12 running --- OK.
And if I do a rackup -p 3000, I am able to hit http://localhost:3000. Port 9494 also works, it is just the Mongo ports that aren't working.
And I had Mongo installed in my host (Windows), I uninstalled it, still the same problem.
Any hints?
It seems your Vagrant port forwarding is working fine. Your issue is probably because MongoDB is setup to only listen on 127.0.0.1. Check your bindip setting.

Resources