Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a forwarded port in Vagrant?

Tags:

port

vagrant

I downloaded a Vagrantfile and am running it on my CentOS 7 box. When I execute vagrant up, the process starts successfully and the machine is booted and ready. I'm able to access the process using the URL:

http://<IP_ADDRESS_OF_BOX>:8080

However, I don't want Vagrant to use port 8080 and would rather use an obscure port like 8601. So, I modified the Vagrantfile to include another entry for config.vm.network.

Before change - Vagrantfile

Vagrant.configure(2) do |config|
    config.vm.box = 'ToraToraTora'
end

After change - Vagrantfile

Vagrant.configure(2) do |config|
    config.vm.box = 'ToraToraTora'
    config.vm.network "forwarded_port", guest: 80, host: 8601
end

Now I'm able to access the process using the new port:

http://<IP_ADDRESS_OF_BOX>:8601

However, the previous port continues to work too:

http://<IP_ADDRESS_OF_BOX>:8080

Executing sudo netstat -tulpn:

[ToraToraTora@andromeda ~]$ sudo netstat -tulpn | grep 26206
tcp        0      0 127.0.0.1:2222          0.0.0.0:*               LISTEN      26206/VBoxHeadless  
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      26206/VBoxHeadless  
tcp        0      0 0.0.0.0:8601            0.0.0.0:*               LISTEN      26206/VBoxHeadless  
udp        0      0 0.0.0.0:40168           0.0.0.0:*                           26206/VBoxHeadless  
[ToraToraTora@andromeda ~]$

Output from running vagrant port:

[ToraToraTora@andromeda app]$ vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2222 (host)
    80 (guest) => 8080 (host)
    80 (guest) => 8601 (host)
[ToraToraTora@andromeda app]$ 

How do I stop the Vagrant process from using port 8080 and ONLY use port 8601?

like image 593
Tora Tora Tora Avatar asked Jan 15 '16 18:01

Tora Tora Tora


1 Answers

You could explicitly disable the 8080 forwarded port...

Vagrant.configure(2) do |config|
    config.vm.box = 'ToraToraTora'
    config.vm.network "forwarded_port", guest: 80, host: 8601
    config.vm.network "forwarded_port", guest: 80, host: 8080, disabled: true
end

If you make that change and do a vagrant reload, it will clear the 8080 forwarded port. At that point, you can remove the 8080 line from your Vagrantfile.

NOTE: Port forwarding in Vagrant can be compared to radio broadcasts. Guest ports are like radio stations while host ports are like radios. In the same way that a radio station can broadcast to any number of radios, a guest port on the Vagrant machine can be forwarded to multiple ports on the host machine. However, each host port can only receive forwarded traffic from one guest port at a time in the same way that a radio can only be tuned to one station at a time.

So in this case, two radios (ports 8601 and 8080 on the host) were tuned to the same station (port 80 on the guest). The solution was simply to switch off the radio at 8080.

like image 182
Patrick Lee Avatar answered Sep 22 '22 14:09

Patrick Lee