Vagrant IP address setup for port listening
Asked Answered
D

3

9

I run this command inside my vagrant instance:

printf 'HTTP/1.1 302 Moved\r\nLocation: https://www.eff.org/' | nc -l 2345

On my host computer, I want to access <ip of my vagrant server>:2345 and be redirected to https://www.eff.org/. Redirect does not happen, the browser just keeps loading.

My Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/trusty64"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "public_network" 
  
end

How to set up Vagrantfile and determine the IP address of the vagrant Linux server, to use in the browser on the host machine using port 2345?

I have tried:

  • curl ifconfig.me, got: 46.128.200.193
  • hostname -i, got: 2a02:2455:25f:e000:a00:27ff:febd:cd6c%4 10.0.2.15 192.168.33.10 192.168.0.16
  • ifconfig, got:
eth0      Link encap:Ethernet  HWaddr 08:00:27:5f:bb:e6  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe5f:bbe6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:815 errors:0 dropped:0 overruns:0 frame:0
          TX packets:590 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:83632 (83.6 KB)  TX bytes:80621 (80.6 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:c0:4e:f3  
          inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fec0:4ef3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:819 (819.0 B)  TX bytes:2040 (2.0 KB)

eth2      Link encap:Ethernet  HWaddr 08:00:27:bd:cd:6c  
          inet addr:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: 2a02:2455:25f:e000:a00:27ff:febd:cd6c/64 Scope:Global
          inet6 addr: fe80::a00:27ff:febd:cd6c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:412 errors:0 dropped:0 overruns:0 frame:0
          TX packets:103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:31706 (31.7 KB)  TX bytes:8356 (8.3 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:888 (888.0 B)  TX bytes:888 (888.0 B)

On my host machine, I have tried accessing in the browser:

  • 192.168.33.10:2345, got:
GET / HTTP/1.1
Host: 192.168.33.10:2345
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:77.0) Gecko/20100101 Firefox/77.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
  • 10.0.2.15:2345, got nothing
  • 46.128.200.193:2345, got nothing

But no setup of Vagrantfile and combination of ip addresses with port 2345 have redirected me to eff.org so far.

Didache answered 22/6, 2020 at 6:59 Comment(2)
What is your virtual machine provider?Kapellmeister
@VladVolkov Virtual BoxDidache
P
1

How to set up Vagrantfile and determine the IP address of the vagrant Linux server, to use in the browser on the host machine using port 2345?

To access a port in your vagrant machine from your host machine (also known as port-forward), you need to add these lines under your configure("2") section:

config.vm.define "server" do |server|
    server.vm.network "forwarded_port", guest: 2345, host: 2345
end

As for the IP, I'm not sure how you could know it from your host without checking inside the vagrant machine, but I'm pretty sure the IP 10.0.2.15 should be accessible to you from the host (and vice-versa, the IP 10.0.2.2 should be used in the vagrant machine to access the host).

Pohl answered 20/1, 2022 at 15:27 Comment(0)
P
0

I have different approach. I am using python simple http server module.

code is used from other answer

#!/usr/bin/python
import SimpleHTTPServer
import SocketServer

class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):
       self.send_response(301)
       self.send_header('Location','http://www.eff.org')
       self.end_headers()

theport = 1234
Handler = myHandler
pywebserver = SocketServer.TCPServer(("", theport), Handler)

print "Python based web server. Serving at port", theport
pywebserver.serve_forever()

regarding multiple interfaces.

make sure output of

sudo lsof -i -P -n | grep LISTEN

*:1234 i.e. all interfaces are used to serve on this port.

web_fwd.p 2594         dev    3u  IPv4  60354      0t0  TCP *:1234 (LISTEN)
Peloria answered 1/7, 2020 at 14:59 Comment(0)
S
0

Further to @Amit Side's answer, a Vagrant guest/instance has its own virtual network: Making a service available on a guest/instance's port (2345 in your question) is only visible on that guest/instance's virtual network unless you also add port forwarding as suggested in that answer.

Southernly answered 10/10, 2023 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.