Elasticsearch: Failed to connect to localhost port 9200 - Connection refused
Asked Answered
I

32

152

When I tried connecting to Elasticsearch using the curl http://localhost:9200 it is working fine.

But when I run the curl http://IpAddress:9200 it is throwing an error saying

Failed to connect to localhost port 9200: Connection refused

How to resolve this error?

Interpreter answered 28/7, 2015 at 13:20 Comment(3)
If you already set network.host: localhost in the elasticsearch.yml file then you can try to add the http protocol in the request: curl -X GET "http://localhost:9200"Dingess
At least using Elasticsearch 7.8.0 on Windows 10, just uncommenting network.host in the elasticsearch.yml file works for me.Communitarian
Be sure to check the log (mine was located at /var/log/elasticsearch/elasticsearch.log in Ubuntu).Boredom
T
93

By default it should bind to all local addresses. So, assuming you don't have a network layer issue with firewalls, the only ES setting I can think to check is network.bind_host and make sure it is either not set or is set to 0.0.0.0 or ::0 or to the correct IP address for your network.

Update: per comments in ES 2.3 you should set network.host instead.

Truscott answered 28/7, 2015 at 13:22 Comment(6)
I had to explicitly set the network.bind_host value, leaving it unset was my issue.Isom
Are you sure this is safe? I think the right is access only from local server.Buckler
Well, in most cases this is safe. But yeah, that's why the clause for "correct IP address for your network" is there.Truscott
As stated below "network.bind_host" is now "network.host" for elasticsearch 2.3Ouse
what are some network commands I should use to check my server settings? if I did netstat -plunt | grep 9200 what should it show?Sanctity
@Sanctity it should show you LISTEN in the output. Something like tcp6 0 0 1.1.1.1:9200 :::* LISTEN 12345/java LISTEN means ES is listening and ready to accept connections. The ideal way to check ES cluster status though is using curl $HOSTNAME:9200Collazo
R
99

Edit /etc/elasticsearch/elasticsearch.yml and add the following line:

network.host: 0.0.0.0

This will "unset" this parameter and will allow connections from other IPs.

Recorder answered 8/12, 2015 at 10:48 Comment(6)
This worked for me, thanks! However on my Mac, the config file is located at /Applications/elasticsearch-2.1.1/config/elasticsearch.yml, and the parameter I had to edit was network.host, not network.bind_host.Coraciiform
in elasticsearch 2.2 the parameter is called network.hostDuquette
this /etc/elasticsearch/elasticsearch.yml file should be in Docker container ?Mansur
This solves for me, i'm using elasticsearch-6.0.0 running inside docker container. After the config change, I can now access by curl http://172.17.0.2:9200. You will recognize that is the commonly used docker ip addressing.Repugn
network.host: 0.0.0.0 - do not help, but this exception I am getting from cerebroCraft
For me network.host: 127.0.0.1 and http.port: 9200 is working. on 0.0.0.0 getting errorMencius
T
93

By default it should bind to all local addresses. So, assuming you don't have a network layer issue with firewalls, the only ES setting I can think to check is network.bind_host and make sure it is either not set or is set to 0.0.0.0 or ::0 or to the correct IP address for your network.

Update: per comments in ES 2.3 you should set network.host instead.

Truscott answered 28/7, 2015 at 13:22 Comment(6)
I had to explicitly set the network.bind_host value, leaving it unset was my issue.Isom
Are you sure this is safe? I think the right is access only from local server.Buckler
Well, in most cases this is safe. But yeah, that's why the clause for "correct IP address for your network" is there.Truscott
As stated below "network.bind_host" is now "network.host" for elasticsearch 2.3Ouse
what are some network commands I should use to check my server settings? if I did netstat -plunt | grep 9200 what should it show?Sanctity
@Sanctity it should show you LISTEN in the output. Something like tcp6 0 0 1.1.1.1:9200 :::* LISTEN 12345/java LISTEN means ES is listening and ready to accept connections. The ideal way to check ES cluster status though is using curl $HOSTNAME:9200Collazo
S
34

In my case elasticsearch was started. But still had

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

The following command was unsuccessful

sudo service elasticsearch restart

In order to make it work, I had to run instead

sudo systemctl restart elasticsearch

Then it went all fine.

Stratton answered 25/12, 2016 at 12:21 Comment(4)
I also had to do sudo systemctl enable elasticsearch.Mccluskey
I got the same issue, but could not understand why systemctl works and service does not ?Unquiet
@Unquiet systemctl is powered by systemd and is an entirely different programDevon
I get the error message: sudo: systemct1: command not foundStaid
S
29

Tried everything on this page, and only instructions from here helped.

in /etc/default/elasticsearch, make sure these are un-commented:

START_DAEMON=true
ES_USER=elasticsearch
ES_GROUP=elasticsearch
LOG_DIR=/var/log/elasticsearch
DATA_DIR=/var/lib/elasticsearch
WORK_DIR=/tmp/elasticsearch
CONF_DIR=/etc/elasticsearch
CONF_FILE=/etc/elasticsearch/elasticsearch.yml
RESTART_ON_UPGRADE=true

make sure /var/lib/elasticsearch is owned by elasticsearch user:

chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/
Sulphate answered 24/4, 2017 at 22:58 Comment(1)
Had the same problem and the solution was just chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/ and un-commented the START_DAEMON=trueMacfadyn
C
20

Why don't you start with this command-line:

$ sudo service elasticsearch status

I did it and get:

"There is insufficient memory for the Java Runtime..."

Then I edited /etc/elasticsearch/jvm.options file:

...

################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

#-Xms2g
#-Xms2g

-Xms512m
-Xmx512m

################################################################

...

This worked like a charm.

Calva answered 14/5, 2017 at 17:34 Comment(2)
This appears to give the same information as the 22 March answer bu @Jefferson.macedo. A new answer should not just rewrite existing details, it should provide new and usefil information.About
@About actually this answer provide new information such as the error obtained and comments on the meaning of -Xmxxx lines. I think the "original" answer is less attractive somehow, possibly because the quotes of code are NOT separated from the text, being placed in the middle of the text instead.Blanton
C
18

None of the proposed solutions here worked for me, but what eventually got it working was adding the following to elasticsearch.yml

network:
  host: 0.0.0.0
http:
  port: 9200

After that, I restarted the service and now I can curl it from both within the VM and externally. For some odd reason, I had to try a few different variants of a curl call inside the VM before it worked:

curl localhost:9200
curl http://localhost:9200
curl 127.0.0.1:9200

Note: I'm using Elasticsearch 5.5 on Ubuntu 14.04

Ceuta answered 13/7, 2017 at 2:17 Comment(0)
B
14

Edit elasticsearch.yml and add the following line

http.host: 0.0.0.0

network.host: 0.0.0.0 didn't work for

Bunyip answered 27/6, 2019 at 8:36 Comment(1)
this worked for me as well thanks! this solution ran on ES 7.17.13 and ubuntu 20.04Hogwash
H
13
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)

be sure that the server is started. I've seen this problem when my virtual machine had too litle RAM and es could not start.

sudo systemctl status elasticsearch

the above will show you if es is indeed running.

Hyohyoid answered 1/11, 2016 at 15:43 Comment(1)
Check out #29447934 for help sorting this problem out.Vow
A
11

For this problem, I had to use : sudo /usr/share/elasticsearch/bin/elasticsearch start

to be able to get something on ports 9200/9300 (sudo netstat -ntlp) and a response to:

curl -XGET http://localhost:9200

Aerolite answered 15/2, 2017 at 16:59 Comment(1)
checking the status, the error message doesnt help. only when running the bin file manually it's giving correct error message.Substitution
F
6

I experienced a similar issue.

Here's how I solved it

Run the service command below to start ElasticSearch

sudo service elasticsearch start

OR

sudo systemctl start elasticsearch

If you still get the error

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

Run the service command below to check the status of ElasticSearch

sudo service elasticsearch status

OR

sudo systemctl status elasticsearch

If you get a response (Active: active (running)) like the one below then you ElasticSearch is active and running

● elasticsearch.service - Elasticsearch Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled) Active: active (running) since Sat 2019-09-21 11:22:21 WAT; 3s ago

You can then test that your Elasticsearch node is running by sending an HTTP request to port 9200 on localhost using the command below:

curl http://localhost:9200

Else, if you get a response a different response, you may have to debug further to fix it, but the running the command below, will help you detect what caveats are holding ElasticSearch service from starting.

sudo service elasticsearch status

OR

sudo systemctl status elasticsearch

If you want to stop the ElasticSearch service, simply run the service command below;

sudo service elasticsearch stop

OR

sudo systemctl stop elasticsearch

N/B: You may have to run the command sudo service elasticsearch status OR sudo systemctl status elasticsearch each time you encounter the error, in order to tell the state of the ElasticSearch service.

This also applies for Kibana, run the command sudo service kibana status OR sudo systemctl status kibana each time you encounter the error, in order to tell the state of the Kibana service.

That's all.

I hope this helps.

Fritts answered 21/9, 2019 at 11:48 Comment(0)
J
4

I had the same problem refusing connections on 9200 port. Check elasticsearch service status with the command sudo service elasticsearch status. If it is presenting an error and you read anything related to Java, probably the problem is your jvm memory. You can edit it in /etc/elasticsearch/jvm.options. For a 1GB RAM memory machine on Amazon environment, I kept my configuration on:

-Xms128m
-Xmx128m

After setting that and restarting elasticsearch service, it worked like a charm. Nmap and UFW (if you use local firewall) checking should also be useful.

Jacaranda answered 22/3, 2017 at 0:50 Comment(1)
This is what did it for me. My config had 1g for both, and the AWS instance (t2.micro) couldn't allocate that much memory to it. The start and restart commands failed silently, but the status command detailed the error.Askance
H
3

Open your Dockerfile under elasticsearch folder and update "network.host=0.0.0.0" with "network.host=127.0.0.1". Then restart the container. Check your connection with curl.

$ curl http://docker-machine-ip:9200
{
  "name" : "vI6Zq_D",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "hhyB_Wa4QwSX6zZd1F894Q",
  "version" : {
    "number" : "5.2.0",
    "build_hash" : "24e05b9",
    "build_date" : "2017-01-24T19:52:35.800Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.0"
  },
  "tagline" : "You Know, for Search"
}
Housebound answered 15/2, 2017 at 8:47 Comment(0)
D
3

For versions higher than 6.8 (7.x) you need two things.

1. change the network host to listen on the public interface.

In the configuration file elasticsearch.yml (for debian and derivatives -> /etc/elasticsearch/elasticsearch.yml).

  • set the network.host or network.bind_host to:
...
network.host: 0.0.0.0
...

Or the interface that must be reached

2. Before going to production it's necessary to set important discovery and cluster formation settings.

According to elastic.co:
v6.8 -> discovery settings that should set.
by e.g

...
# roughly means the same as 1
discovery.zen.minimum_master_nodes: -1
...

v7.x -> discovery settings that should set.
by one single node

discovery.type: single-node
#OR set discovery.seed_hosts : 127.0.0.1:9200

at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured.

Dehumidify answered 18/10, 2020 at 3:46 Comment(0)
A
2

For Windows user try,

https://localhost:9200/

It worked for me.

Anthracene answered 16/2, 2023 at 1:13 Comment(1)
This was the reason for my case too. I am using a macEuromarket
H
1

In this case, first of all you need to check the java version using below command:

java -version

after running this command you get something like this:

java version "1.7.0_51" OpenJDK Runtime Environment (rhel-2.4.5.5.el7-x86_64 u51-b31) OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

then use this command:

update-alternatives --config java

and select the below version

*+ 1 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51-2.4.5.5.el7.x86_64/jre/bin/java 2 /usr/java/jdk1.8.0_73/jre/bin/java

Enter to keep the current selection[+], or type selection number: 2

curl -XGET http://127.0.0.1:9200
Hyo answered 25/1, 2017 at 6:4 Comment(0)
T
1

My 2 cents,

I just followed the install procedure on Digital Ocean, apparently the package available in the repos is not up to date, I deleted everything and followed the install procedure direct from Elastic Search and everything is working now, basically the out of the box behaviour is on a localhost pointing to 9200. Same thing/issue found with Kibana, the solution for me was too, to remove everything and just follow their procedure, Hope this saves someone two hours (the time I spent figuring out how to setup ELK!)

en

Taitaichung answered 29/5, 2017 at 16:44 Comment(0)
C
1

Update your jdk to latest minimum version for your elasticsearch.

Colony answered 7/5, 2018 at 12:23 Comment(0)
Y
1

Change the network.bind to 0.0.0.0 and http:port to 9200. The bind address 0.0.0.0 means all IPv4 addresses on the local machine. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

Yonina answered 1/10, 2018 at 10:21 Comment(0)
H
1

If you encounter the Connection refused error, simply run the command below to check the status of ElasticSearch service

sudo service elasticsearch status

This will help you decipher the state of ElasticSearch service and what to do about it.

Hagerty answered 21/9, 2019 at 12:6 Comment(0)
L
1

For those of you installing ELK on virtual machine in GCP (Google Cloud Platform), make sure that you created firewall rule of Ingress type (i.e. for incoming to VM traffic). You can specify in the rule multiple ports at a time by separating them with comma: 5000,5044,5601,9200,9300,9600.

In that rule you may want to specify a tag (pick tag's name as you like, for example docker-elk that will target your VM (Targets column): enter image description here

On VM's settings page assign that tag to your VM:

enter image description here

After doing that I was able to access Elasticsearch in my browser via port 9200. And I didn't have to edit elasticsearch.yml file whatsoever.

Lannielanning answered 15/12, 2020 at 14:59 Comment(0)
C
1

I have run across this problem every time I install or upgrade ES (7.0+). And the solution was ALWAYS just wait for ES to fully start. It takes about a minute for the REST API to be reponsive. No matter what service status says.

service elasticsearch start
*started

*wait for at least a minute

curl now works and returns responses on the port 9200
Cleome answered 16/12, 2021 at 11:54 Comment(0)
S
1

I was facing the same problem on Ubuntu 22.04, but I purged the downloaded elasticsearch directory and then redownloaded so,

First Pipe the output to the gpg --dearmor command so that apt is able to utilise the key to verify downloaded packages

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg

Then, add the Elastic source list to the sources.list.d directory, where apt will search for new sources

echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

Afterwards update package lists and install elasticsearch again

sudo apt update
sudo apt install elasticsearch

When you finish the download, you need to modify main configuration file elasticsearch.yml where most of its configuration options are stored

sudo nano /etc/elasticsearch/elasticsearch.yml

And then navigate to the Netwok section something like

. . .
# ---------------------------------- Network -----------------------------------

#
network.host: Foo.foo.foo
. . .

Elasticsearch listens for traffic from everywhere on port 9200. You will want to restrict outside access to your Elasticsearch instance to prevent outsiders from reading your data or shutting down your Elasticsearch cluster through its [REST API]

So replace the Foo.foo.foo with

. . .
# ---------------------------------- Network -----------------------------------
#
network.host: localhost
. . .

I used localhost so that Elasticsearch listens on all interfaces and bound IPs.

Start elasticsearch and enable it each time the server starts

sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

With all that said you will have to configure the firewall to allow access to the default Elasticsearch HTTP API port (TCP 9200) for the trusted remote host, generally the server you are using in a single-server setup, such as198.51.100.0. To allow access, type the following command:

sudo ufw allow from 198.51.100.0 to any port 9200
sudo ufw enable

Use cURL to test your installation

curl -X GET 'http://localhost:9200'

Response

{
  "name" : "elastic-22",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "DEKKt_95QL6HLaqS9OkPdQ",
  "version" : {
    "number" : "7.17.1",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "e5acb99f822233d62d6444ce45a4543dc1c8059a",
    "build_date" : "2022-02-23T22:20:54.153567231Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

This is what worked for me.

Studley answered 6/10, 2023 at 10:14 Comment(0)
S
0

After utilizing some of the answers above, don't forget that after an apt install, a total reboot might be in order.

Strohbehn answered 9/2, 2018 at 9:0 Comment(0)
C
0

Just to add on this, I've came across many docs through google that said to set network.host to localhost.

Doing so gave me the infamous connection refused. You must use an IP address (127.0.0.1), not a FQDN.

Jeff

Canna answered 25/3, 2018 at 14:18 Comment(0)
P
0

Make sure that port 9200 is open for my case it was an amazon instance so when i opened it in my security group the curl command worked.

Pyuria answered 14/9, 2018 at 6:18 Comment(0)
H
0

Disabling SELinux worked for me, although I don't suggest it - I did that just for a PoC

Halogen answered 23/12, 2018 at 14:27 Comment(0)
M
0

My problem was I could not work with localhost I needed to set it to localhost's IP address

network.bind_host: 127.0.0.1

Mosley answered 15/4, 2019 at 17:21 Comment(0)
N
0

In my case, the problem is with java version, i installed open-jdk 11 previously. Thats creating the issue while starting the service. I changed it open-jdk 8 and it started working

Nedranedrah answered 30/10, 2019 at 5:45 Comment(0)
F
0

you have to edit /etc/elasticsearch/elasticsearch.yml by default all configurations will be commented ,add following configuration

network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: [0.0.0.0]

then restart the service

Fougere answered 10/1, 2022 at 16:4 Comment(0)
S
0

I experienced this on CentOS 7, and the issue was that /etc/hosts had the following:

127.0.0.1 localhost.localdomain

which I updated to include localhost as follows:

127.0.0.1 localhost localhost.localdomain

after that, no issues.

Stagger answered 10/1, 2022 at 20:37 Comment(0)
F
0

I ran into a related situation recently. Here's my take on the subject: Accessing Elastic 5.5 in vagrant guest from host through a private network

TL;DR The settings:

network.host: 0.0.0.0
http.port: 9200

work fine. One just needs to wait enough time for ES to complete its initialization procedure, bind to the network iface and start listening on the port.

Now, from within the guest, curl http://localhost:9200 works and from the host, curl http://192.168.54.2:9200 works as well.

Fevre answered 28/12, 2022 at 12:42 Comment(0)
C
0

In my case, the network host was explicitly set to the IP of my server, so it was something like this in opensearch.yml:

network.host: 127.150.151.0

I had to configure my app to use this IP explicitly. Otherwise, I could also have changed the address in opensearch.yml.

Canzonet answered 31/5, 2023 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.