vagrant-port
Show Vagrant VM port mappings
TLDR
List all port mappings of machine(s) running in the current directory
List mappings for a specific machine (if Vagrantfile is multi-machine)
Display info for a specific guest port
Display machine-readable output
SYNOPSIS
vagrant port [ <vm-name> ] [ <guest-port> ] [ --guest <GUEST_PORT> ] [ --host <HOST_PORT> ] [ --protocol <PROTOCOL> ] [ --machine-readable ]
PARAMETERS
<vm-name>
The name of the virtual machine to query. If omitted in a multi-VM setup, Vagrant will operate on the primary or default VM. For single-VM environments, it can often be omitted.
<guest-port>
A positional argument that specifies a particular guest port to find its corresponding host port. When used, the output will only show the mapping for this guest port.
--guest <GUEST_PORT>
An option to specify a particular guest port to find its corresponding host port. Similar to the positional <guest-port> argument but explicitly defined as an option.
--host <HOST_PORT>
Filters the output to show mappings where the host port matches the specified value. Less commonly used for lookup, more for filtering.
--protocol <PROTOCOL>
Filters the results by network protocol, such as tcp or udp, if the `Vagrantfile` explicitly defines protocols for port forwards.
--machine-readable
Outputs the port forwarding information in a structured, machine-readable format, which is ideal for scripting and automation purposes.
DESCRIPTION
The `vagrant port` command is a crucial subcommand within the Vagrant ecosystem, a powerful tool designed for building and managing portable development environments. Its primary function is to display the port forwarding configurations for a specified Vagrant virtual machine.
When a `Vagrantfile` configures a forwarded port, it establishes a mapping between a port on the host machine and a port on the guest virtual machine. This mechanism allows services running inside the VM (e.g., a web server on port 80) to be accessed directly from the host machine's network (e.g., via localhost:8080). The `vagrant port` command enables developers to rapidly inspect these mappings without the need to manually parse the `Vagrantfile` or inspect provider-specific settings.
Users can either view all configured forwarded ports for a VM or query for a specific host port associated with a given guest port. This capability is invaluable for debugging network accessibility issues, confirming service exposure, or simply understanding how a development environment is configured. The output typically presents a clear list of guest ports, their corresponding host ports, and the protocol in use (e.g., TCP).
CAVEATS
vagrant port requires Vagrant to be installed and a Vagrant environment (with an initialized Vagrantfile) to be present in the current directory or specified path.
It only displays ports that have been explicitly forwarded in the Vagrantfile. Other networking configurations (e.g., private or public networks) are not directly reported by this command.
In a multi-VM environment, omitting the <vm-name> argument may result in the command operating on an unintended VM. Always specify the VM name for clarity.
The accuracy of the reported port mappings relies on the correct configuration within the Vagrantfile and the underlying virtualization provider's ability to expose this information.
DEFAULT BEHAVIOR
When executed without any arguments, vagrant port will typically list all configured port forwardings for the current Vagrant environment's primary or default virtual machine. This provides a quick overview of all accessible services.
MULTI-VM ENVIRONMENTS
In environments configured with multiple virtual machines, it is essential to specify the target VM's name (e.g., vagrant port webserver --guest 80) to ensure the command queries the correct machine and displays relevant port mappings.
NO PORTS CONFIGURED
If the Vagrantfile for a given environment does not define any forwarded ports, the vagrant port command will usually report no output or an empty list, indicating no explicit port mappings exist.
HISTORY
The `vagrant port` subcommand emerged as a vital component of Vagrant's robust networking capabilities. As Vagrant evolved beyond basic single-VM setups to support complex multi-machine environments and diverse network configurations (including private, public, and forwarded ports), the necessity for an intuitive way to inspect these settings became increasingly apparent. The command provides a straightforward interface to quickly retrieve critical port mapping information, eliminating the need for users to manually parse configuration files or delve into provider-specific network settings. Its development has consistently mirrored the increasing sophistication and flexibility of Vagrant's networking features, ensuring that developers always have clear visibility into how their services are exposed and accessible.


