Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ebukz's avatar

Homestead port forwarding issue with Elasticsearch

So i am using this tutorial to setup elasticsearch on homestead https://bosnadev.com/2014/09/12/install-elasticsearch-on-laravel-homestead/

The issue is that on the VM this request works

 curl -X GET localhost:9200

However outside the VM it fails with connection refused. Is this an issue with port forwarding ?

I have already updated my Homestead.yml file with the current port mapping

ports:
    - send: 62000
      to: 9200
      protocol: udp

What am I doing wrong ?

0 likes
19 replies
boarder212's avatar

Have you reload the VM?

Outside of you VM you should try localhost:62000

noeldiaz's avatar

I think this would work:

ports:
    - send: 9200
      to: 9200
      protocol: tcp

The 9200 port should be free for you on the host. And I think you need tcp rather than udp for protocol. Then restart whole VM or provision it again to get it open.

1 like
ebukz's avatar

I tried your suggestion @noeldiaz but could not get it to work.

I still get

curl: (7) Failed to connect to localhost port 9200: Connection refused

noeldiaz's avatar

Forgot one step. I think by default elastic bind to just the localhost. You need to edit it's settings in "/etc/elasticsearch/elasticsearch.yml". Set "network.host" to your VMs IP like:

network.host: 192.168.10.10

Then restart elastic or the whole VM. Try that.

ebukz's avatar

Hi @noeldiaz . Thanks for the reply. I have made the update now and we have progress.

The connection reaches the VM but data is not returned

curl: (52) Empty reply from server

Any suggestions ?

noeldiaz's avatar

@ebukz Ok, you had me curious so I went through all the process from scratch and it worked. Here are all my steps so you can see what I did in case something is missing from yours:

  1. Got a new homestead VM going. Gave it IP 192.168.20.20. Opened port 9200 to host.
ports:
    - send: 9200
      to: 9200
  1. I installed Java 8.
sudo apt-add-repository ppa:webupd8team/java -y
sudo apt-get update
sudo apt-get install oracle-java8-installer -y
  1. Installed Elastic and set it to auto-start
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
sudo apt-get update
sudo apt-get install elasticsearch -y 
sudo update-rc.d elasticsearch defaults 95 10
  1. Changed the Elastic binding on it's config file to bind to the VMs IP address.
# /etc/elasticsearch/elasticsearch.yml

network.host: 192.168.20.20
  1. Restarted elastic service

  2. Was able to access it from host

$ curl http://192.168.20.20:9200
{
  "name" : "Thunderbolt",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.2.0",
    "build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe",
    "build_timestamp" : "2016-01-27T13:32:39Z",
    "build_snapshot" : false,
    "lucene_version" : "5.4.1"
  },
  "tagline" : "You Know, for Search"
}
1 like
ebukz's avatar

@noeldiaz Thanks. I have complete the process you gave but still having the same issue. Please compare my config yml file to yours


##
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
# path.data: /path/to/data
#
# Path to log files:
#
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.10.10
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true



I think thats where the issue is

-please ignore the IP i'm aware of that

noeldiaz's avatar

@ebukz It's below the sections you copy pasted. Mine look like this (adjust the IP to the IP on your Homestead.yaml file):

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.20.20
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------

Also, you could bind to ALL addresses and not have to worry about that setting anymore. I believe this works:

network.host: 0.0.0.0
ebukz's avatar

Also @noeldiaz a curl request from my machine(host)

curl -v GET localhost:62000

returns this

* Rebuilt URL to: GET/
* Hostname was NOT found in DNS cache
* getaddrinfo(3) failed for GET:80
* Couldn't resolve host 'GET'
* Closing connection 0
curl: (6) Couldn't resolve host 'GET'
* Rebuilt URL to: localhost:62000/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 62000 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost:62000
> Accept: */*
> 
* Empty reply from server
* Connection #1 to host localhost left intact
curl: (52) Empty reply from server
noeldiaz's avatar

For me, I had to use the IP of the VM. So I didn't do "localhost" from the host machine. And I just left it as port "9200" since it is an open port. So for mine, since I changed the VM IP to 192.168.20.20 I can do this now from host:

curl http://192.168.20.20:9200

That works. Try it with the IP of your VM (maybe 192.168.10.10 if you didn't change it)? And add "http" just in case.

noeldiaz's avatar

@ebukz By the way, I confirmed, and with "localhost" from main machine I get same error you do:

$ curl http://localhost:9200
curl: (52) Empty reply from server

But with the IP of the VM it works:

$ curl http://192.168.20.20:9200
{
  "name" : "Super-Nova",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.2.0",
    "build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe",
    "build_timestamp" : "2016-01-27T13:32:39Z",
    "build_snapshot" : false,
    "lucene_version" : "5.4.1"
  },
  "tagline" : "You Know, for Search"
}
1 like
noeldiaz's avatar

@ebukz So it is running fine inside the VM. Your problem has to be either the network setting inside the elasticsearch yaml file (and then restart it properly), or the port settings on your Homestead.yaml file.

I have a feeling it is the elastic one since the IP inside the VM should work if you added it to the settings file and restarted elasticsearch properly. Like I said, you could even add 0.0.0.0 as the IP on that line.

ebukz's avatar

@noeldiaz Yes I have added 0.0.0.0 as the IP in the elasticsearch yaml file but still get the same outcome. However I noticed for other port connections to the VM from the terminal get a connection refused to. For example a node server on port 3000 still does not receive a curl request from the terminal on host, I get connection refused

1 like
fronterace's avatar

@ebukz

Probably you should restart elasticsearch service as well: $ sudo service elasticsearch restart

fx's avatar

It was also a hard problem for me...

I had to add network.bind_host: 0 to my yaml config file, and now curl -XGET localhost:62000 from host works.

Tested on Elastic 2.4.1

trondhuso's avatar

Not sure if this question ever got answered. How ever: On newer Elasticsearch installations there has been added a security layer and thus some security settings the elasticsearch.yml file

If you experience this message, try and set xpack.security.enabled to false

Change this:

# Enable security features
xpack.security.enabled: true

To this:

# Enable security features
xpack.security.enabled: false
1 like

Please or to participate in this conversation.