Run elastic search as root user Run elastic search as root user elasticsearch elasticsearch

Run elastic search as root user


Elasticsearch can't be run an root user. Elasticsearch itself restricts this. A new user named elasticsearch and group named elasticsearch is automatically created when we install elasticsearch. Can check entries by using following commands

$ sudo less /etc/passwd | grep "elasticsearch"$ sudo less /etc/group | grep "elasticsearch"

We need to change ownership of all elasticsearch related files. Please follow the steps mentioned below.

Steps:

1.Change owership of all ES related files from root to elasticsearch using example cmd below.

$ sudo chown elasticsearch:elasticsearch -R /usr/share/elasticsearch$ sudo chown elasticsearch:elasticsearch -R /var/log/elasticsearch$ sudo chown elasticsearch:elasticsearch -R /var/lib/elasticsearch$ sudo chown elasticsearch:elasticsearch -R /etc/default/elasticsearch$ sudo chown elasticsearch:elasticsearch -R /etc/elasticsearch

2.Open /etc/default/elasticsearch file and do the following things

  a)JAVA_HOME=your/java/home/path  b)add the following entries at the end      i)   START_DAEMON=true      ii)  ES_USER=elasticsearch      iii) ES_GROUP=elasticsearch

3.Now enable elasticsearch service and start

  $ sudo systemctl enable elasticsearch  $ sudo systemctl start elasticsearch  $ sudo systemctl status elasticsearch

4.Test elasticsearch by using curl. Say your host ip is 192.168.5.194 and ES running on port 9200

$ curl -X GET ‘192.168.5.194:9200’

DONE!!

Ref. : https://stackoverflow.com/a/48390311/1445978


Root cause of this issue is:ElasticSearch is not allowed to run from root owner. There is another possiblity for this issue "Java path is set for root user only not for all other users". Solution of this issue:

Step 1: Change the ownership of elasticSearch directory from root to other user by command.$sudo chown -R current_User:Group_Name elasticsearch-5.5.0

Setp 2: Check Java set in classpath for current user[not only for root]. If command : $java -version or echo $JAVA_HOME command giving empty result. That means we should set Java in classpath [system env varible] for current user then Follow Step 3. otherwise start elasticsearch service.

Step 3: Edit /etc/profile and add two lines as per your system direxport JAVA_HOME="Java dir location"

export PATH=$JAVA_HOME/bin:$PATHRun$source source /etc/profile

After this run elasticSearch service. It worked for me perfectly.


There are two ways to solve this problem :

Method1:download zip file n unzip then start by following command

bin/elasticsearch -Des.insecure.allow.root=true -d

Method2:

vi bin/elasticsearch

Add property for allow root:

ES_JAVA_OPTS="-Des.insecure.allow.root=true"

Save and close.You can start by root now.