2013-09-03 18:08:28 +00:00
|
|
|
---
|
2016-01-19 18:08:53 +00:00
|
|
|
layout: "docs"
|
2013-09-06 16:50:43 +00:00
|
|
|
page_title: "Forwarded Ports - Networking"
|
2013-09-03 18:08:28 +00:00
|
|
|
sidebar_current: "networking-fp"
|
2016-01-19 18:08:53 +00:00
|
|
|
description: |-
|
|
|
|
Vagrant forwarded ports allow you to access a port on your host machine and
|
|
|
|
have all data forwarded to a port on the guest machine, over either TCP or
|
|
|
|
UDP.
|
2013-09-03 18:08:28 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Forwarded Ports
|
|
|
|
|
2013-09-06 16:50:43 +00:00
|
|
|
**Network identifier: `forwarded_port`**
|
2013-09-03 18:08:28 +00:00
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
Vagrant forwarded ports allow you to access a port on your host machine and have
|
2013-09-03 18:08:28 +00:00
|
|
|
all data forwarded to a port on the guest machine, over either TCP or UDP.
|
|
|
|
|
|
|
|
For example: If the guest machine is running a web server listening on port 80,
|
|
|
|
you can make a forwarded port mapping to port 8080 (or anything) on your host
|
|
|
|
machine. You can then open your browser to `localhost:8080` and browse the
|
|
|
|
website, while all actual network data is being sent to the guest.
|
|
|
|
|
|
|
|
## Defining a Forwarded Port
|
|
|
|
|
|
|
|
The forwarded port configuration expects two parameters, the port on the
|
|
|
|
guest and the port on the host. Example:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.network "forwarded_port", guest: 80, host: 8080
|
2013-09-03 18:08:28 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-05-01 14:22:05 +00:00
|
|
|
This will allow accessing port 80 on the guest via port 8080 on the host.
|
2013-09-03 18:08:28 +00:00
|
|
|
|
2015-11-19 02:23:57 +00:00
|
|
|
For most providers, forwarded ports by default bind to all interfaces. This
|
|
|
|
means that other devices on your network can access the forwarded ports.
|
|
|
|
If you want to restrict access, see the `guest_ip` and `host_ip` settings
|
|
|
|
below.
|
|
|
|
|
2013-11-26 06:53:20 +00:00
|
|
|
## Options Reference
|
|
|
|
|
|
|
|
This is a complete list of the options that are available for forwarded
|
|
|
|
ports. Only the `guest` and `host` options are required. Below this section,
|
|
|
|
there are more detailed examples of using these options.
|
|
|
|
|
|
|
|
* `guest` (int) - The port on the guest that you want to be exposed on
|
|
|
|
the host. This can be any port.
|
|
|
|
|
|
|
|
* `guest_ip` (string) - The guest IP to bind the forwarded port to. If
|
2015-08-30 23:42:42 +00:00
|
|
|
this is not set, the port will go to every IP interface. By default,
|
2013-11-26 06:53:20 +00:00
|
|
|
this is empty.
|
|
|
|
|
|
|
|
* `host` (int) - The port on the host that you want to use to access the
|
|
|
|
port on the guest. This must be greater than port 1024 unless Vagrant
|
|
|
|
is running as root (which is not recommended).
|
|
|
|
|
|
|
|
* `host_ip` (string) - The IP on the host you want to bind the forwarded
|
|
|
|
port to. If not specified, it will be bound to every IP. By default,
|
|
|
|
this is empty.
|
|
|
|
|
|
|
|
* `protocol` (string) - Either "udp" or "tcp". This specifies the protocol
|
2014-09-16 15:45:31 +00:00
|
|
|
that will be allowed through the forwarded port. By default this is "tcp".
|
2013-11-26 06:53:20 +00:00
|
|
|
|
2013-10-08 14:58:32 +00:00
|
|
|
## Forwarded Port Protocols
|
|
|
|
|
|
|
|
By default, any defined port will only forward the TCP protocol. As an optional
|
2013-11-26 06:53:20 +00:00
|
|
|
third parameter, you may specify `protocol: 'udp'` in order to pass UDP
|
|
|
|
traffic. If a given port needs to be able to listen to the same port on both
|
|
|
|
protocols, you must define the port twice with each protocol specified, like
|
2013-10-08 14:58:32 +00:00
|
|
|
so:
|
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
```ruby
|
2013-10-08 14:58:32 +00:00
|
|
|
Vagrant.configure("2") do |config|
|
2016-01-19 18:08:53 +00:00
|
|
|
config.vm.network "forwarded_port", guest: 2003, host: 12003, protocol: "tcp"
|
|
|
|
config.vm.network "forwarded_port", guest: 2003, host: 12003, protocol: "udp"
|
2013-10-08 14:58:32 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-09-03 18:08:28 +00:00
|
|
|
## Port Collisions and Correction
|
|
|
|
|
|
|
|
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
|
2016-01-19 18:08:53 +00:00
|
|
|
user to think that the port was not properly forwarded. Enabling auto correct
|
2013-09-03 18:08:28 +00:00
|
|
|
is easy:
|
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
```ruby
|
2013-09-03 18:08:28 +00:00
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.network "forwarded_port", guest: 80, host: 8080,
|
2013-09-03 18:08:28 +00:00
|
|
|
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.
|