Can't run Sonar Server caused by Elasticsearch cannot running as root
Asked Answered
E

6

15

I'm trying to install SonarQube : i ve followed those steps :

Setting up SOnarQube Tuto : here

To summarize it :

  • Downloading Sonar and moving it to /opt/sonar
  • adding those coonfig steps to /opt/sonar/conf/sonar.properties :

    sonar.jdbc.username=sonar sonar.jdbc.password=sonar

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

and

sonar.web.host=127.0.0.1
sonar.web.context=/sonar
sonar.web.port=9000
  • implementing sonar as a service:

sudo cp /opt/sonar/bin/linux-x86-64/sonar.sh /etc/init.d/sonar

sudo gedit /etc/init.d/sonar

Insert two new lines:

SONAR_HOME=/opt/sonar
PLATFORM=linux-x86-64
Modify the following lines:

WRAPPER_CMD="${SONAR_HOME}/bin/${PLATFORM}/wrapper"
WRAPPER_CONF="${SONAR_HOME}/conf/wrapper.conf"

...

PIDDIR="/var/run"
Register as a Linux service:

sudo update-rc.d -f sonar remove
sudo chmod 755 /etc/init.d/sonar
sudo update-rc.d sonar defaults

After those steps : i ve tried to run Sonar from : localhost:9000/sonar and after executing : sudo /etc/init.d/sonar start ` Strangely , it didsn't run .

SO when i run `sudo /etc/init.d/sonar status , i discover that it goes steps after few seconds , and it throws some error in it log file , like the following :

es.log:

2017.12.09 18:05:14 ERROR es[][o.e.b.Bootstrap] Exception
java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:123) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) [elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) [elasticsearch-5.6.3.jar:5.6.3]
2017.12.09 18:05:14 WARN  es[][o.e.b.ElasticsearchUncaughtExceptionHandler] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:123) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[elasticsearch-5.6.3.jar:5.6.3]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) ~[elasticsearch-5.6.3.jar:5.6.3]
    ... 6 more

Any suggestions ??

Edmanda answered 9/12, 2017 at 17:17 Comment(3)
I don't know how Sonar starts elasticsearch but for sure elasticsearch can't be run as root.Indebted
Looking at this solution, it may work for you https://mcmap.net/q/662545/-sonarqube-elasticsearch-not-startingIndecipherable
look at this solution, it may work. https://mcmap.net/q/662545/-sonarqube-elasticsearch-not-startingIndecipherable
R
5

Change the root access to the sonar file. Try running in normal user access.

chown <another user>:<user group> sonar.sh
Reduplicate answered 10/10, 2018 at 12:56 Comment(1)
Changing ownership of a file has no impact on the privileges it has at runtime unless setuid is set.Aggravation
U
29

SonarSource has upgraded the Elasticsearch tool used as a part of the latest SonarQube LTS version. As a result SonarQube cannot be run as a root. See this blog and the SonarQube 6.6 upgrade notes and this SO question.

Unbounded answered 9/12, 2017 at 21:54 Comment(1)
Any reason why this answer was suddenly no longer accepted?Unbounded
R
5

Change the root access to the sonar file. Try running in normal user access.

chown <another user>:<user group> sonar.sh
Reduplicate answered 10/10, 2018 at 12:56 Comment(1)
Changing ownership of a file has no impact on the privileges it has at runtime unless setuid is set.Aggravation
E
4

I was facing the same issue and spend good amount of time before getting the solution.

Error was same as

2017.12.09 18:05:14 ERROR es[][o.e.b.Bootstrap] Exception
java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.3.jar:5.6.3]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.3.jar:5.6.3]

Solution Create a ‘sonar’ user using adduser command //I was using linux system chown -R sonar sonar-install-folder/ Edit the sonar.sh file sonar-install-folder/bin/linux-x86-6 in start script and change the #RUN_AS_USER to be RUN_AS_USER=sonar

And then use below commands

./sonar.sh stop

Gracefully stopping SonarQube... SonarQube was not running.

./sonar.sh start

Starting SonarQube... Started SonarQube.

./sonar.sh status

SonarQube is running (22100).

So, basically elastic search doesnt require to be run by root user. By default the RUN_AS_USER was commented in the startup script which was picking my current user which was root, and then some linux permission rules come into picture due to which elasticsearch process was not getting up and which was causing sonar process to go down as well.

It was evident from sonar.log

[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
2020.05.08 06:36:36 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 78
2020.05.08 06:36:36 INFO  app[][o.s.a.SchedulerImpl] Process[es] is stopped
2020.05.08 06:36:36 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped
Eximious answered 26/1, 2021 at 15:24 Comment(0)
N
2

Create a non root user only dedicated for the sonarqube instance

sudo adduser --system --no-create-home --group --disabled-login sonar

Set the permission on the sonarqube folder

sudo chown -R sonar:sonar /opt/sonarqube

Start the process

sudo -Hu sonar /opt/sonarqube/bin/linux-x86-64/sonar.sh start
Nonviolence answered 22/6, 2023 at 17:6 Comment(0)
W
0

Create new user to run the sonarQube or access the SonarQube dashboard

for ubuntu flavour user creation.

#sudo adduser sonar.

after executing the above command it will ask for passwd

#adduser sonar sudo

#adduser sonar admin

#sudo su sonar

chown sonar:sonar sonar.sh

Whensoever answered 14/5, 2022 at 7:2 Comment(2)
Welcome to StackOverflow. To improve readability, please mark your code either with backticks (`code`) for inline code, or by indent a code block by 4 leading spaces. See stackoverflow.com/editing-help#comment-formatting for an overview of SO's Markdown formatting.Garrick
Added formatting adviceGarrick
B
0

#User to start SonarQube

useradd -b /opt/sonarqube -s /bin/bash sonarqube
chown -R sonarqube:sonarqube /opt/sonarqube

#Create service nano /etc/systemd/system/sonarqube.service

[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonarqube
Group=sonarqube
Restart=always
LimitNOFILE=65536
LimitNPROC=4096
[Install]
WantedBy=multi-user.target
systemctl daemon-reload

systemctl start sonarqube.service

systemctl enable sonarqube.service
Bergschrund answered 12/7 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.